diff --git a/Cargo.lock b/Cargo.lock index 4953257b1..b12a47635 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1247,9 +1247,9 @@ dependencies = [ name = "opcua-simple-client" version = "0.8.0" dependencies = [ - "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "opcua-client 0.8.0", "opcua-console-logging 0.8.0", + "pico-args 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1400,6 +1400,11 @@ name = "percent-encoding" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "pico-args" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pkg-config" version = "0.3.17" @@ -2832,6 +2837,7 @@ dependencies = [ "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" +"checksum pico-args 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ad1f1b834a05d42dae330066e9699a173b28185b3bdc3dbf14ca239585de8cc" "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" "checksum pretty_env_logger 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ed8d1e63042e889b85228620629b51c011d380eed2c7e0015f8a644def280c28" diff --git a/samples/simple-client/Cargo.toml b/samples/simple-client/Cargo.toml index 6c226ebbf..8569bffdd 100644 --- a/samples/simple-client/Cargo.toml +++ b/samples/simple-client/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Adam Lock "] edition = "2018" [dependencies] -clap = "2.33" +pico-args="0.3" [dependencies.opcua-client] path = "../../client" diff --git a/samples/simple-client/src/main.rs b/samples/simple-client/src/main.rs index 5a49f195c..9de1b705d 100644 --- a/samples/simple-client/src/main.rs +++ b/samples/simple-client/src/main.rs @@ -5,42 +5,48 @@ //! 3. Subscribe to values and loop forever printing out their values use std::sync::{Arc, RwLock}; -use clap::{App, Arg}; - use opcua_client::prelude::*; -fn main() { +struct Args { + help: bool, + url: String, +} + +fn main() -> Result<(), Box> { // Read command line arguments - let m = App::new("Simple OPC UA Client") - .arg(Arg::with_name("url") - .long("url") - .help("Specify the OPC UA endpoint to connect to") - .takes_value(true) - .default_value("opc.tcp://localhost:4855") - .required(false)) - .get_matches(); - let url = m.value_of("url").unwrap().to_string(); + let mut args = pico_args::Arguments::from_env(); + let args = Args { + help: args.contains(["-h", "--help"]), + url: args.opt_value_from_str("--url")?.unwrap_or(String::from("opc.tcp://localhost:4855")), + }; - // Optional - enable OPC UA logging - opcua_console_logging::init(); + if args.help { + println!(r#"Simple Client"#); + println!(r#"Usage: simple-client --url [url]"#); + } + else { + // Optional - enable OPC UA logging + opcua_console_logging::init(); - // Make the client configuration - let mut client = ClientBuilder::new() - .application_name("Simple Client") - .application_uri("urn:SimpleClient") - .trust_server_certs(true) - .create_sample_keypair(true) - .session_retry_limit(3) - .client().unwrap(); + // Make the client configuration + let mut client = ClientBuilder::new() + .application_name("Simple Client") + .application_uri("urn:SimpleClient") + .trust_server_certs(true) + .create_sample_keypair(true) + .session_retry_limit(3) + .client().unwrap(); - if let Ok(session) = client.connect_to_endpoint((url.as_ref(), SecurityPolicy::None.to_str(), MessageSecurityMode::None, UserTokenPolicy::anonymous()), IdentityToken::Anonymous) { - if let Err(result) = subscribe_to_variables(session.clone()) { - println!("ERROR: Got an error while subscribing to variables - {}", result); - } else { - // Loops forever. The publish thread will call the callback with changes on the variables - let _ = Session::run(session); + if let Ok(session) = client.connect_to_endpoint((args.url.as_ref(), SecurityPolicy::None.to_str(), MessageSecurityMode::None, UserTokenPolicy::anonymous()), IdentityToken::Anonymous) { + if let Err(result) = subscribe_to_variables(session.clone()) { + println!("ERROR: Got an error while subscribing to variables - {}", result); + } else { + // Loops forever. The publish thread will call the callback with changes on the variables + let _ = Session::run(session); + } } } + Ok(()) } fn subscribe_to_variables(session: Arc>) -> Result<(), StatusCode> { diff --git a/types/src/url.rs b/types/src/url.rs index e0f836afe..8d46ba798 100644 --- a/types/src/url.rs +++ b/types/src/url.rs @@ -19,7 +19,7 @@ fn opc_url_from_str(s: &str) -> Result { url }) .map_err(|err| { - error!("Cannot parse url {}, error = {:?}", s, err); + error!("Cannot parse url \"{}\", error = {:?}", s, err); }) } @@ -37,10 +37,10 @@ pub fn url_matches(url1: &str, url2: &str) -> bool { if let Ok(url2) = opc_url_from_str(url2) { return url1 == url2; } else { - error!("Cannot parse url {}", url2); + error!("Cannot parse url \"{}\"", url2); } } else { - error!("Cannot parse url {}", url1); + error!("Cannot parse url \"{}\"", url1); } false } @@ -56,10 +56,10 @@ pub fn url_matches_except_host(url1: &str, url2: &str) -> bool { return url1 == url2; } } else { - error!("Cannot parse url {}", url2); + error!("Cannot parse url \"{}\"", url2); } } else { - error!("Cannot parse url {}", url1); + error!("Cannot parse url \"{}\"", url1); } false }