From cbd378e5398861526d8365d68843194d060268b4 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 12 Jul 2018 20:03:04 -0400 Subject: [PATCH 1/6] refactor: changes validator signature to match v3 --- tests/custom-string-parsers.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/custom-string-parsers.rs b/tests/custom-string-parsers.rs index 535e646..d0c6090 100644 --- a/tests/custom-string-parsers.rs +++ b/tests/custom-string-parsers.rs @@ -88,7 +88,7 @@ fn test_parse_hex() { fn custom_parser_1(_: &str) -> &'static str { "A" } fn custom_parser_2(_: &str) -> Result<&'static str, u32> { Ok("B") } fn custom_parser_3(_: &OsStr) -> &'static str { "C" } -fn custom_parser_4(_: &OsStr) -> Result<&'static str, OsString> { Ok("D") } +fn custom_parser_4(_: &OsStr) -> Result<&'static str, String> { Ok("D") } #[derive(Clap, PartialEq, Debug)] struct NoOpOpt { @@ -172,7 +172,11 @@ struct Occurrences { #[clap(short = "r", parse(from_occurrences))] little_unsigned: u8, - #[clap(short = "c", long = "custom", parse(from_occurrences = "foo"))] + #[clap( + short = "c", + long = "custom", + parse(from_occurrences = "foo") + )] custom: Foo, } @@ -205,7 +209,11 @@ fn test_custom_bool() { struct Opt { #[clap(short = "d", parse(try_from_str = "parse_bool"))] debug: bool, - #[clap(short = "v", default_value = "false", parse(try_from_str = "parse_bool"))] + #[clap( + short = "v", + default_value = "false", + parse(try_from_str = "parse_bool") + )] verbose: bool, #[clap(short = "t", parse(try_from_str = "parse_bool"))] tribool: Option, From 4695d71fdba243cb41feb85c1e7cf74e8f8134ec Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 12 Jul 2018 20:31:20 -0400 Subject: [PATCH 2/6] refactor: fixes use statements to stop traits from being imported multiple times --- src/derives/clap.rs | 6 ++++-- src/derives/from_argmatches.rs | 2 ++ src/derives/into_app.rs | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/derives/clap.rs b/src/derives/clap.rs index ad05323..a7c20da 100644 --- a/src/derives/clap.rs +++ b/src/derives/clap.rs @@ -242,8 +242,6 @@ fn clap_impl_for_struct( let parse_fns = gen_parse_fns(name); quote! { - use ::clap::{FromArgMatches, IntoApp}; - #[allow(unused_variables)] impl ::clap::Clap for #name { } @@ -315,10 +313,12 @@ pub fn derive_clap(input: &syn::DeriveInput) -> proc_macro2::TokenStream { fn gen_parse_fns(name: &syn::Ident) -> proc_macro2::TokenStream { quote! { fn parse() -> #name { + use ::clap::{FromArgMatches, IntoApp}; #name::from_argmatches(&#name::into_app().get_matches()) } fn try_parse() -> ::std::result::Result<#name, ::clap::Error> { + use ::clap::{FromArgMatches, IntoApp}; Ok(#name::from_argmatches(&#name::into_app().get_matches_safe()?)) } @@ -326,6 +326,7 @@ fn gen_parse_fns(name: &syn::Ident) -> proc_macro2::TokenStream { where I: ::std::iter::IntoIterator, T: Into<::std::ffi::OsString> + Clone { + use ::clap::{FromArgMatches, IntoApp}; #name::from_argmatches(&#name::into_app().get_matches_from(itr)) } @@ -333,6 +334,7 @@ fn gen_parse_fns(name: &syn::Ident) -> proc_macro2::TokenStream { where I: ::std::iter::IntoIterator, T: Into<::std::ffi::OsString> + Clone { + use ::clap::{FromArgMatches, IntoApp}; Ok(#name::from_argmatches(&#name::into_app().get_matches_from_safe(itr)?)) } } diff --git a/src/derives/from_argmatches.rs b/src/derives/from_argmatches.rs index 4df4ee3..8b39b83 100644 --- a/src/derives/from_argmatches.rs +++ b/src/derives/from_argmatches.rs @@ -48,6 +48,7 @@ pub fn gen_from_argmatches_impl_for_struct( impl<'a> From<::clap::ArgMatches<'a>> for #name { fn from(m: ::clap::ArgMatches) -> Self { + use ::clap::FromArgMatches; ::from_argmatches(&m) } } @@ -153,6 +154,7 @@ pub fn gen_from_argmatches_impl_for_enum(name: &syn::Ident) -> proc_macro2::Toke impl<'a> From<::clap::ArgMatches<'a>> for #name { fn from(m: ::clap::ArgMatches) -> Self { + use ::clap::FromArgMatches; ::from_argmatches(&m) } } diff --git a/src/derives/into_app.rs b/src/derives/into_app.rs index dd384fc..9c61999 100644 --- a/src/derives/into_app.rs +++ b/src/derives/into_app.rs @@ -45,6 +45,7 @@ pub fn gen_into_app_impl_for_struct( impl<'a, 'b> Into<::clap::App<'a, 'b>> for #name { fn into(self) -> ::clap::App<'a, 'b> { + use ::clap::IntoApp; <#name as ::clap::IntoApp>::into_app() } } @@ -83,6 +84,7 @@ pub fn gen_into_app_impl_for_enum( impl<'a, 'b> Into<::clap::App<'a, 'b>> for #name { fn into(self) -> ::clap::App<'a, 'b> { + use ::clap::IntoApp; <#name as ::clap::IntoApp>::into_app() } } From 09b39eac0a81a6a07492f4c496d5b02ffda858f8 Mon Sep 17 00:00:00 2001 From: Kevin K Date: Thu, 12 Jul 2018 20:32:28 -0400 Subject: [PATCH 3/6] tests: fixes function calls to use the convenience functions and correct trait tests: changes function calls to convenience functions and correct trait --- tests/arguments.rs | 26 ++--------- tests/author_version_about.rs | 23 ++++++++-- tests/custom-string-parsers.rs | 40 ++++++---------- tests/doc-comments-help.rs | 10 +++- tests/flags.rs | 79 ++++++++------------------------ tests/flatten.rs | 8 +--- tests/nested-subcommands.rs | 28 ++++++------ tests/options.rs | 83 +++++++--------------------------- tests/raw_attributes.rs | 26 ++++++----- tests/subcommands.rs | 52 ++++++++------------- 10 files changed, 133 insertions(+), 242 deletions(-) diff --git a/tests/arguments.rs b/tests/arguments.rs index a8595f7..f524498 100644 --- a/tests/arguments.rs +++ b/tests/arguments.rs @@ -24,12 +24,8 @@ fn required_argument() { arg: i32, } assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "42"])); - assert!(Opt::into_app().get_matches_from_safe(&["test"]).is_err()); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "42", "24"]) - .is_err() - ); + assert!(Opt::try_parse_from(&["test"]).is_err()); + assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); } #[test] @@ -40,11 +36,7 @@ fn optional_argument() { } assert_eq!(Opt { arg: Some(42) }, Opt::parse_from(&["test", "42"])); assert_eq!(Opt { arg: None }, Opt::parse_from(&["test"])); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "42", "24"]) - .is_err() - ); + assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); } #[test] @@ -56,11 +48,7 @@ fn argument_with_default() { } assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "24"])); assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "42", "24"]) - .is_err() - ); + assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); } #[test] @@ -72,11 +60,7 @@ fn argument_with_raw_default() { } assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "24"])); assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "42", "24"]) - .is_err() - ); + assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); } #[test] diff --git a/tests/author_version_about.rs b/tests/author_version_about.rs index 930537c..010f214 100644 --- a/tests/author_version_about.rs +++ b/tests/author_version_about.rs @@ -19,6 +19,8 @@ use clap::Clap; #[test] fn no_author_version_about() { + use clap::IntoApp; + #[derive(Clap, PartialEq, Debug)] #[clap(name = "foo", about = "", author = "", version = "")] struct Opt {} @@ -30,8 +32,25 @@ fn no_author_version_about() { assert!(output.starts_with("foo \n\nUSAGE:")); } +static ENV_HELP: &str = "clap_derive 0.3.0 +Guillaume Pinot , Kevin K. , hoverbear +Parse command line argument by defining a struct, derive crate. + +USAGE: + clap_derive + +FLAGS: + -h, --help + Prints help information + + -V, --version + Prints version information +"; + #[test] fn use_env() { + use clap::IntoApp; + #[derive(Clap, PartialEq, Debug)] #[clap()] struct Opt {} @@ -39,7 +58,5 @@ fn use_env() { let mut output = Vec::new(); Opt::into_app().write_long_help(&mut output).unwrap(); let output = String::from_utf8(output).unwrap(); - assert!(output.starts_with("structopt 0.2.")); - assert!(output.contains("Guillaume Pinot , others")); - assert!(output.contains("Parse command line argument by defining a struct.")); + assert_eq!(output, ENV_HELP); } diff --git a/tests/custom-string-parsers.rs b/tests/custom-string-parsers.rs index d0c6090..467adbc 100644 --- a/tests/custom-string-parsers.rs +++ b/tests/custom-string-parsers.rs @@ -17,7 +17,7 @@ extern crate clap; use clap::Clap; -use std::ffi::{OsStr, OsString}; +use std::ffi::OsStr; use std::num::ParseIntError; use std::path::PathBuf; @@ -53,10 +53,10 @@ fn test_path_opt_simple() { option_path_1: None, option_path_2: Some(PathBuf::from("j.zip")), }, - PathOpt::from_argmatches(&PathOpt::into_app().get_matches_from(&[ + PathOpt::parse_from(&[ "test", "-p", "/usr/bin", "-v", "/a/b/c", "-v", "/d/e/f", "-v", "/g/h/i", "-q", "j.zip", - ])) + ]) ); } @@ -72,16 +72,14 @@ struct HexOpt { fn test_parse_hex() { assert_eq!( HexOpt { number: 5 }, - HexOpt::from_argmatches(&HexOpt::into_app().get_matches_from(&["test", "-n", "5"])) + HexOpt::parse_from(&["test", "-n", "5"]) ); assert_eq!( HexOpt { number: 0xabcdef }, - HexOpt::from_argmatches(&HexOpt::into_app().get_matches_from(&["test", "-n", "abcdef"])) + HexOpt::parse_from(&["test", "-n", "abcdef"]) ); - let err = HexOpt::into_app() - .get_matches_from_safe(&["test", "-n", "gg"]) - .unwrap_err(); + let err = HexOpt::try_parse_from(&["test", "-n", "gg"]).unwrap_err(); assert!(err.message.contains("invalid digit found in string"), err); } @@ -111,9 +109,7 @@ fn test_every_custom_parser() { c: "C", d: "D" }, - NoOpOpt::from_argmatches( - &NoOpOpt::into_app().get_matches_from(&["test", "-a=?", "-b=?", "-c=?", "-d=?"]) - ) + NoOpOpt::parse_from(&["test", "-a=?", "-b=?", "-c=?", "-d=?"]) ); } @@ -141,7 +137,7 @@ fn test_parser_with_default_value() { integer: 9000, path: PathBuf::from("src/lib.rs"), }, - DefaultedOpt::from_argmatches(&DefaultedOpt::into_app().get_matches_from(&[ + DefaultedOpt::parse_from(&[ "test", "-b", "E²=p²c²+m²c⁴", @@ -149,7 +145,7 @@ fn test_parser_with_default_value() { "9000", "-p", "src/lib.rs", - ])) + ]) ); } @@ -190,9 +186,9 @@ fn test_parser_occurrences() { little_unsigned: 4, custom: Foo(5), }, - Occurrences::from_argmatches(&Occurrences::into_app().get_matches_from(&[ + Occurrences::parse_from(&[ "test", "-s", "--signed", "--signed", "-l", "-rrrr", "-cccc", "--custom", - ])) + ]) ); } @@ -221,17 +217,9 @@ fn test_custom_bool() { bitset: Vec, } - assert!(Opt::into_app().get_matches_from_safe(&["test"]).is_err()); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-d"]) - .is_err() - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-dfoo"]) - .is_err() - ); + assert!(Opt::try_parse_from(&["test"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-d"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-dfoo"]).is_err()); assert_eq!( Opt { debug: false, diff --git a/tests/doc-comments-help.rs b/tests/doc-comments-help.rs index 4d7f35b..9a7a96a 100644 --- a/tests/doc-comments-help.rs +++ b/tests/doc-comments-help.rs @@ -19,6 +19,8 @@ use clap::Clap; #[test] fn commets_intead_of_actual_help() { + use clap::IntoApp; + /// Lorem ipsum #[derive(Clap, PartialEq, Debug)] struct LoremIpsum { @@ -38,12 +40,17 @@ fn commets_intead_of_actual_help() { #[test] fn help_is_better_than_comments() { + use clap::IntoApp; /// Lorem ipsum #[derive(Clap, PartialEq, Debug)] #[clap(name = "lorem-ipsum", about = "Dolor sit amet")] struct LoremIpsum { /// Fooify a bar - #[clap(short = "f", long = "foo", help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES")] + #[clap( + short = "f", + long = "foo", + help = "DO NOT PASS A BAR UNDER ANY CIRCUMSTANCES" + )] foo: bool, } @@ -58,6 +65,7 @@ fn help_is_better_than_comments() { #[test] fn empty_line_in_doc_comment_is_double_linefeed() { + use clap::IntoApp; /// Foo. /// /// Bar diff --git a/tests/flags.rs b/tests/flags.rs index 9233487..6dfa3bb 100644 --- a/tests/flags.rs +++ b/tests/flags.rs @@ -25,38 +25,13 @@ fn unique_flag() { alice: bool, } - assert_eq!( - Opt { alice: false }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) - ); - assert_eq!( - Opt { alice: true }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a"])) - ); - assert_eq!( - Opt { alice: true }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--alice"])) - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-i"]) - .is_err() - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a", "foo"]) - .is_err() - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a", "-a"]) - .is_err() - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a", "--alice"]) - .is_err() - ); + assert_eq!(Opt { alice: false }, Opt::parse_from(&["test"])); + assert_eq!(Opt { alice: true }, Opt::parse_from(&["test", "-a"])); + assert_eq!(Opt { alice: true }, Opt::parse_from(&["test", "--alice"])); + assert!(Opt::try_parse_from(&["test", "-i"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-a", "-a"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-a", "--alice"]).is_err()); } #[test] @@ -69,36 +44,22 @@ fn multiple_flag() { bob: u8, } - assert_eq!( - Opt { alice: 0, bob: 0 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) - ); - assert_eq!( - Opt { alice: 1, bob: 0 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a"])) - ); + assert_eq!(Opt { alice: 0, bob: 0 }, Opt::parse_from(&["test"])); + assert_eq!(Opt { alice: 1, bob: 0 }, Opt::parse_from(&["test", "-a"])); assert_eq!( Opt { alice: 2, bob: 0 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a", "-a"])) + Opt::parse_from(&["test", "-a", "-a"]) ); assert_eq!( Opt { alice: 2, bob: 2 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a", "--alice", "-bb"])) + Opt::parse_from(&["test", "-a", "--alice", "-bb"]) ); assert_eq!( Opt { alice: 3, bob: 1 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-aaa", "--bob"])) - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-i"]) - .is_err() - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a", "foo"]) - .is_err() + Opt::parse_from(&["test", "-aaa", "--bob"]) ); + assert!(Opt::try_parse_from(&["test", "-i"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-a", "foo"]).is_err()); } #[test] @@ -116,41 +77,41 @@ fn combined_flags() { alice: false, bob: 0 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) + Opt::parse_from(&["test"]) ); assert_eq!( Opt { alice: true, bob: 0 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a"])) + Opt::parse_from(&["test", "-a"]) ); assert_eq!( Opt { alice: true, bob: 0 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a"])) + Opt::parse_from(&["test", "-a"]) ); assert_eq!( Opt { alice: false, bob: 1 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-b"])) + Opt::parse_from(&["test", "-b"]) ); assert_eq!( Opt { alice: true, bob: 1 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--alice", "--bob"])) + Opt::parse_from(&["test", "--alice", "--bob"]) ); assert_eq!( Opt { alice: true, bob: 4 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-bb", "-a", "-bb"])) + Opt::parse_from(&["test", "-bb", "-a", "-bb"]) ); } diff --git a/tests/flatten.rs b/tests/flatten.rs index 5440476..02814e5 100644 --- a/tests/flatten.rs +++ b/tests/flatten.rs @@ -35,12 +35,8 @@ fn flatten() { }, Opt::parse_from(&["test", "42"]) ); - assert!(Opt::into_app().get_matches_from_safe(&["test"]).is_err()); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "42", "24"]) - .is_err() - ); + assert!(Opt::try_parse_from(&["test"]).is_err()); + assert!(Opt::try_parse_from(&["test", "42", "24"]).is_err()); } #[test] diff --git a/tests/nested-subcommands.rs b/tests/nested-subcommands.rs index 83bfac2..74e158c 100644 --- a/tests/nested-subcommands.rs +++ b/tests/nested-subcommands.rs @@ -47,7 +47,7 @@ struct Opt2 { #[test] fn test_no_cmd() { - let result = Opt::into_app().get_matches_from_safe(&["test"]); + let result = Opt::try_parse_from(&["test"]); assert!(result.is_err()); assert_eq!( @@ -56,7 +56,7 @@ fn test_no_cmd() { verbose: 0, cmd: None }, - Opt2::from_argmatches(&Opt2::into_app().get_matches_from(&["test"])) + Opt2::parse_from(&["test"]) ); } @@ -68,7 +68,7 @@ fn test_fetch() { verbose: 3, cmd: Sub::Fetch {} }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-vvv", "fetch"])) + Opt::parse_from(&["test", "-vvv", "fetch"]) ); assert_eq!( Opt { @@ -76,7 +76,7 @@ fn test_fetch() { verbose: 0, cmd: Sub::Fetch {} }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--force", "fetch"])) + Opt::parse_from(&["test", "--force", "fetch"]) ); } @@ -88,7 +88,7 @@ fn test_add() { verbose: 0, cmd: Sub::Add {} }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "add"])) + Opt::parse_from(&["test", "add"]) ); assert_eq!( Opt { @@ -96,19 +96,19 @@ fn test_add() { verbose: 2, cmd: Sub::Add {} }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-vv", "add"])) + Opt::parse_from(&["test", "-vv", "add"]) ); } #[test] fn test_badinput() { - let result = Opt::into_app().get_matches_from_safe(&["test", "badcmd"]); + let result = Opt::try_parse_from(&["test", "badcmd"]); assert!(result.is_err()); - let result = Opt::into_app().get_matches_from_safe(&["test", "add", "--verbose"]); + let result = Opt::try_parse_from(&["test", "add", "--verbose"]); assert!(result.is_err()); - let result = Opt::into_app().get_matches_from_safe(&["test", "--badopt", "add"]); + let result = Opt::try_parse_from(&["test", "--badopt", "add"]); assert!(result.is_err()); - let result = Opt::into_app().get_matches_from_safe(&["test", "add", "--badopt"]); + let result = Opt::try_parse_from(&["test", "add", "--badopt"]); assert!(result.is_err()); } @@ -150,9 +150,7 @@ fn test_subsubcommand() { cmd: Sub3::Quux {} } }, - Opt3::from_argmatches( - &Opt3::into_app().get_matches_from(&["test", "--all", "foo", "lib.rs", "quux"]) - ) + Opt3::parse_from(&["test", "--all", "foo", "lib.rs", "quux"]) ); } @@ -188,10 +186,12 @@ enum Stash { #[test] fn sub_sub_cmd_with_option() { fn make(args: &[&str]) -> Option { + use clap::{FromArgMatches, IntoApp}; + SubSubCmdWithOption::into_app() .get_matches_from_safe(args) .ok() - .map(|m| SubSubCmdWithOption::from_clap(&m)) + .map(|m| SubSubCmdWithOption::from_argmatches(&m)) } assert_eq!( Some(SubSubCmdWithOption::Remote { cmd: None }), diff --git a/tests/options.rs b/tests/options.rs index ff2b3f4..3ffbed0 100644 --- a/tests/options.rs +++ b/tests/options.rs @@ -24,24 +24,11 @@ fn required_option() { #[clap(short = "a", long = "arg")] arg: i32, } - assert_eq!( - Opt { arg: 42 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a42"])) - ); - assert_eq!( - Opt { arg: 42 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a", "42"])) - ); - assert_eq!( - Opt { arg: 42 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--arg", "42"])) - ); - assert!(Opt::into_app().get_matches_from_safe(&["test"]).is_err()); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a42", "-a24"]) - .is_err() - ); + assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "-a42"])); + assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "-a", "42"])); + assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test", "--arg", "42"])); + assert!(Opt::try_parse_from(&["test"]).is_err()); + assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); } #[test] @@ -51,19 +38,9 @@ fn optional_option() { #[clap(short = "a")] arg: Option, } - assert_eq!( - Opt { arg: Some(42) }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a42"])) - ); - assert_eq!( - Opt { arg: None }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a42", "-a24"]) - .is_err() - ); + assert_eq!(Opt { arg: Some(42) }, Opt::parse_from(&["test", "-a42"])); + assert_eq!(Opt { arg: None }, Opt::parse_from(&["test"])); + assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); } #[test] @@ -73,19 +50,9 @@ fn option_with_default() { #[clap(short = "a", default_value = "42")] arg: i32, } - assert_eq!( - Opt { arg: 24 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a24"])) - ); - assert_eq!( - Opt { arg: 42 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a42", "-a24"]) - .is_err() - ); + assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "-a24"])); + assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); + assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); } #[test] @@ -95,19 +62,9 @@ fn option_with_raw_default() { #[clap(short = "a", raw(default_value = r#""42""#))] arg: i32, } - assert_eq!( - Opt { arg: 24 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a24"])) - ); - assert_eq!( - Opt { arg: 42 }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) - ); - assert!( - Opt::into_app() - .get_matches_from_safe(&["test", "-a42", "-a24"]) - .is_err() - ); + assert_eq!(Opt { arg: 24 }, Opt::parse_from(&["test", "-a24"])); + assert_eq!(Opt { arg: 42 }, Opt::parse_from(&["test"])); + assert!(Opt::try_parse_from(&["test", "-a42", "-a24"]).is_err()); } #[test] @@ -117,17 +74,11 @@ fn options() { #[clap(short = "a", long = "arg")] arg: Vec, } - assert_eq!( - Opt { arg: vec![24] }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a24"])) - ); - assert_eq!( - Opt { arg: vec![] }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test"])) - ); + assert_eq!(Opt { arg: vec![24] }, Opt::parse_from(&["test", "-a24"])); + assert_eq!(Opt { arg: vec![] }, Opt::parse_from(&["test"])); assert_eq!( Opt { arg: vec![24, 42] }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-a24", "--arg", "42"])) + Opt::parse_from(&["test", "-a24", "--arg", "42"]) ); } diff --git a/tests/raw_attributes.rs b/tests/raw_attributes.rs index f6bf3d4..457ccc8 100644 --- a/tests/raw_attributes.rs +++ b/tests/raw_attributes.rs @@ -32,7 +32,11 @@ struct Opt { )] x: i32, - #[clap(short = "l", long = "level", raw(aliases = r#"&["set-level", "lvl"]"#))] + #[clap( + short = "l", + long = "level", + raw(aliases = r#"&["set-level", "lvl"]"#) + )] level: String, #[clap(long = "values")] @@ -51,7 +55,7 @@ fn test_raw_slice() { files: Vec::new(), values: vec![], }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-l", "1"])) + Opt::parse_from(&["test", "-l", "1"]) ); assert_eq!( Opt { @@ -60,7 +64,7 @@ fn test_raw_slice() { files: Vec::new(), values: vec![], }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--level", "1"])) + Opt::parse_from(&["test", "--level", "1"]) ); assert_eq!( Opt { @@ -69,7 +73,7 @@ fn test_raw_slice() { files: Vec::new(), values: vec![], }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--set-level", "1"])) + Opt::parse_from(&["test", "--set-level", "1"]) ); assert_eq!( Opt { @@ -78,7 +82,7 @@ fn test_raw_slice() { files: Vec::new(), values: vec![], }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "--lvl", "1"])) + Opt::parse_from(&["test", "--lvl", "1"]) ); } @@ -91,7 +95,7 @@ fn test_raw_multi_args() { files: vec!["file".to_string()], values: vec![], }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-l", "1", "file"])) + Opt::parse_from(&["test", "-l", "1", "file"]) ); assert_eq!( Opt { @@ -100,15 +104,13 @@ fn test_raw_multi_args() { files: vec!["FILE".to_string()], values: vec![1], }, - Opt::from_argmatches( - &Opt::into_app().get_matches_from(&["test", "-l", "1", "--values", "1", "--", "FILE"]), - ) + Opt::parse_from(&["test", "-l", "1", "--values", "1", "--", "FILE"]) ); } #[test] fn test_raw_multi_args_fail() { - let result = Opt::into_app().get_matches_from_safe(&["test", "-l", "1", "--", "FILE"]); + let result = Opt::try_parse_from(&["test", "-l", "1", "--", "FILE"]); assert!(result.is_err()); } @@ -121,8 +123,8 @@ fn test_raw_bool() { files: vec![], values: vec![], }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "-l", "1", "--x=1"])) + Opt::parse_from(&["test", "-l", "1", "--x=1"]) ); - let result = Opt::into_app().get_matches_from_safe(&["test", "-l", "1", "--x", "1"]); + let result = Opt::try_parse_from(&["test", "-l", "1", "--x", "1"]); assert!(result.is_err()); } diff --git a/tests/subcommands.rs b/tests/subcommands.rs index defa43d..47a6156 100644 --- a/tests/subcommands.rs +++ b/tests/subcommands.rs @@ -46,9 +46,7 @@ fn test_fetch() { force: false, repo: "origin".to_string() }, - Opt::from_argmatches( - &Opt::into_app().get_matches_from(&["test", "fetch", "--all", "origin"]) - ) + Opt::parse_from(&["test", "fetch", "--all", "origin"]) ); assert_eq!( Opt::Fetch { @@ -56,7 +54,7 @@ fn test_fetch() { force: true, repo: "origin".to_string() }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "fetch", "-f", "origin"])) + Opt::parse_from(&["test", "fetch", "-f", "origin"]) ); } @@ -67,26 +65,26 @@ fn test_add() { interactive: false, verbose: false }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "add"])) + Opt::parse_from(&["test", "add"]) ); assert_eq!( Opt::Add { interactive: true, verbose: true }, - Opt::from_argmatches(&Opt::into_app().get_matches_from(&["test", "add", "-i", "-v"])) + Opt::parse_from(&["test", "add", "-i", "-v"]) ); } #[test] fn test_no_parse() { - let result = Opt::into_app().get_matches_from_safe(&["test", "badcmd", "-i", "-v"]); + let result = Opt::try_parse_from(&["test", "badcmd", "-i", "-v"]); assert!(result.is_err()); - let result = Opt::into_app().get_matches_from_safe(&["test", "add", "--badoption"]); + let result = Opt::try_parse_from(&["test", "add", "--badoption"]); assert!(result.is_err()); - let result = Opt::into_app().get_matches_from_safe(&["test"]); + let result = Opt::try_parse_from(&["test"]); assert!(result.is_err()); } @@ -104,11 +102,7 @@ fn test_hyphenated_subcommands() { Opt2::DoSomething { arg: "blah".to_string() }, - Opt2::from_argmatches(&Opt2::into_app().get_matches_from(&[ - "test", - "do-something", - "blah" - ])) + Opt2::parse_from(&["test", "do-something", "blah"]) ); } @@ -124,18 +118,9 @@ enum Opt3 { #[test] fn test_null_commands() { - assert_eq!( - Opt3::Add, - Opt3::from_argmatches(&Opt3::into_app().get_matches_from(&["test", "add"])) - ); - assert_eq!( - Opt3::Init, - Opt3::from_argmatches(&Opt3::into_app().get_matches_from(&["test", "init"])) - ); - assert_eq!( - Opt3::Fetch, - Opt3::from_argmatches(&Opt3::into_app().get_matches_from(&["test", "fetch"])) - ); + assert_eq!(Opt3::Add, Opt3::parse_from(&["test", "add"])); + assert_eq!(Opt3::Init, Opt3::parse_from(&["test", "init"])); + assert_eq!(Opt3::Fetch, Opt3::parse_from(&["test", "fetch"])); } #[derive(Clap, PartialEq, Debug)] @@ -162,21 +147,20 @@ enum Opt4 { #[test] fn test_tuple_commands() { + use clap::IntoApp; + assert_eq!( Opt4::Add(Add { file: "f".to_string() }), - Opt4::from_argmatches(&Opt4::into_app().get_matches_from(&["test", "add", "f"])) - ); - assert_eq!( - Opt4::Init, - Opt4::from_argmatches(&Opt4::into_app().get_matches_from(&["test", "init"])) + Opt4::parse_from(&["test", "add", "f"]) ); + assert_eq!(Opt4::Init, Opt4::parse_from(&["test", "init"])); assert_eq!( Opt4::Fetch(Fetch { remote: "origin".to_string() }), - Opt4::from_argmatches(&Opt4::into_app().get_matches_from(&["test", "fetch", "origin"])) + Opt4::parse_from(&["test", "fetch", "origin"]) ); let mut output = Vec::new(); @@ -204,10 +188,10 @@ fn enum_in_enum_subsubcommand() { Stop, } - let result = Opt::into_app().get_matches_from_safe(&["test"]); + let result = Opt::try_parse_from(&["test"]); assert!(result.is_err()); - let result = Opt::into_app().get_matches_from_safe(&["test", "daemon"]); + let result = Opt::try_parse_from(&["test", "daemon"]); assert!(result.is_err()); let result = Opt::parse_from(&["test", "daemon", "start"]); From 750d186c307a127cb807feee2d7175977cfdb1c2 Mon Sep 17 00:00:00 2001 From: Alan K Date: Fri, 20 Jul 2018 16:17:45 +0200 Subject: [PATCH 4/6] Updates to multiple occurences for v3-clap --- src/derives/clap.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/derives/clap.rs b/src/derives/clap.rs index a7c20da..45f4cde 100644 --- a/src/derives/clap.rs +++ b/src/derives/clap.rs @@ -103,7 +103,9 @@ fn gen_app_augmentation( Ty::Bool => quote!( .takes_value(false).multiple(false) ), Ty::Option => quote!( .takes_value(true).multiple(false) #validator ), Ty::Vec => quote!( .takes_value(true).multiple(true) #validator ), - Ty::Other if occurences => quote!( .takes_value(false).multiple(true) ), + Ty::Other if occurences => { + quote!( .takes_value(false).multiple_occurrences(true) ) + } Ty::Other => { let required = !attrs.has_method("default_value"); quote!( .takes_value(true).multiple(false).required(#required) #validator ) From d7f5484c28b497a815fd5e66e6ec7ec6a248ad7b Mon Sep 17 00:00:00 2001 From: Alan K Date: Fri, 20 Jul 2018 16:46:20 +0200 Subject: [PATCH 5/6] Removed the empty default value test --- tests/options.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/options.rs b/tests/options.rs index 3ffbed0..b29a692 100644 --- a/tests/options.rs +++ b/tests/options.rs @@ -83,13 +83,13 @@ fn options() { } #[test] -fn empy_default_value() { +fn default_value() { #[derive(Clap, PartialEq, Debug)] struct Opt { - #[clap(short = "a", default_value = "")] + #[clap(short = "a", default_value = "test")] arg: String, } - assert_eq!(Opt { arg: "".into() }, Opt::parse_from(&["test"])); + assert_eq!(Opt { arg: "test".into() }, Opt::parse_from(&["test"])); assert_eq!( Opt { arg: "foo".into() }, Opt::parse_from(&["test", "-afoo"]) From 48f83e0f53ca17fcdb6eb4609afedefc5f2342d1 Mon Sep 17 00:00:00 2001 From: Alan K Date: Fri, 20 Jul 2018 16:46:47 +0200 Subject: [PATCH 6/6] Removed some unnecessary builder methods --- src/derives/clap.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/derives/clap.rs b/src/derives/clap.rs index 45f4cde..09cda77 100644 --- a/src/derives/clap.rs +++ b/src/derives/clap.rs @@ -100,15 +100,13 @@ fn gen_app_augmentation( // @TODO remove unneccessary builders let modifier = match ty { - Ty::Bool => quote!( .takes_value(false).multiple(false) ), - Ty::Option => quote!( .takes_value(true).multiple(false) #validator ), + Ty::Bool => quote!(), + Ty::Option => quote!( .takes_value(true) #validator ), Ty::Vec => quote!( .takes_value(true).multiple(true) #validator ), - Ty::Other if occurences => { - quote!( .takes_value(false).multiple_occurrences(true) ) - } + Ty::Other if occurences => quote!( .multiple_occurrences(true) ), Ty::Other => { let required = !attrs.has_method("default_value"); - quote!( .takes_value(true).multiple(false).required(#required) #validator ) + quote!( .takes_value(true).required(#required) #validator ) } }; let methods = attrs.methods();