use clap::App; use preftool_clap::ClapConfigExt; use preftool_clap_derive::ClapConfig; #[derive(ClapConfig)] pub struct Server { #[preftool_clap(help = "Server host")] host: String, #[preftool_clap(help = "Server port")] port: usize, #[preftool_clap(help = "Server certificate (enables TLS)")] cert: Option, } #[derive(ClapConfig)] pub struct Settings { server: Server, } #[test] fn sets_host() { let args = vec!["test", "--server-host=localhost"]; let result = Settings::from_cli_args(App::new("test"), args).unwrap(); assert_eq!(result.len(), 1); assert_eq!(result.get("server:host").unwrap(), "localhost"); assert_eq!(result.get("server:port"), None); } #[test] fn sets_port() { let args = vec!["test", "--server-port", "8080"]; let result = Settings::from_cli_args(App::new("test"), args).unwrap(); assert_eq!(result.len(), 1); assert_eq!(result.get("server:port").unwrap(), "8080"); assert_eq!(result.get("server:host"), None); } #[test] fn sets_both() { let args = vec!["test", "--server-port", "8080", "--server-host=otherhost"]; let result = Settings::from_cli_args(App::new("test"), args).unwrap(); assert_eq!(result.len(), 2); assert_eq!(result.get("server:port").unwrap(), "8080"); assert_eq!(result.get("server:host").unwrap(), "otherhost"); }