From 69a8bb6a3318b82e26e17e7c94c2d9b4b84530bd Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Tue, 7 Dec 2021 15:39:31 -0500 Subject: [PATCH] Fix redacting Vec options This changes argh_derive to handle redacting types like: ``` struct Cmd { #[argh(option)] /// fooey arg: Vec, } ``` Fixes #111 --- argh/Cargo.toml | 6 +++--- argh/tests/lib.rs | 18 ++++++++++++++++++ argh_derive/Cargo.toml | 4 ++-- argh_derive/src/lib.rs | 27 +++++++++++++++++++++++++-- argh_shared/Cargo.toml | 2 +- 5 files changed, 49 insertions(+), 8 deletions(-) diff --git a/argh/Cargo.toml b/argh/Cargo.toml index 80f71d3..ec92149 100644 --- a/argh/Cargo.toml +++ b/argh/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "argh" -version = "0.1.6" +version = "0.1.7" authors = ["Taylor Cramer ", "Benjamin Brittain ", "Erick Tryzelaar "] edition = "2018" keywords = ["args", "arguments", "derive", "cli"] @@ -10,5 +10,5 @@ repository = "https://github.com/google/argh" readme = "README.md" [dependencies] -argh_shared = { version = "0.1.6", path = "../argh_shared" } -argh_derive = { version = "0.1.6", path = "../argh_derive" } +argh_shared = { version = "0.1.7", path = "../argh_shared" } +argh_derive = { version = "0.1.7", path = "../argh_derive" } diff --git a/argh/tests/lib.rs b/argh/tests/lib.rs index fc4fc13..810e2bc 100644 --- a/argh/tests/lib.rs +++ b/argh/tests/lib.rs @@ -1004,6 +1004,24 @@ fn redact_arg_values_option_one_optional_args() { assert_eq!(actual, &["program-name", "--msg"]); } +#[test] +fn redact_arg_values_option_repeating() { + #[derive(FromArgs, Debug)] + /// Short description + struct Cmd { + #[argh(option)] + /// fooey + _msg: Vec, + } + + let actual = Cmd::redact_arg_values(&["program-name"], &[]).unwrap(); + assert_eq!(actual, &["program-name"]); + + let actual = + Cmd::redact_arg_values(&["program-name"], &["--msg", "abc", "--msg", "xyz"]).unwrap(); + assert_eq!(actual, &["program-name", "--msg", "--msg"]); +} + #[test] fn redact_arg_values_switch() { #[derive(FromArgs, Debug)] diff --git a/argh_derive/Cargo.toml b/argh_derive/Cargo.toml index 39712d5..b74329d 100644 --- a/argh_derive/Cargo.toml +++ b/argh_derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "argh_derive" -version = "0.1.6" +version = "0.1.7" authors = ["Taylor Cramer ", "Benjamin Brittain ", "Erick Tryzelaar "] edition = "2018" license = "BSD-3-Clause" @@ -16,4 +16,4 @@ heck = "0.3.1" proc-macro2 = "1.0" quote = "1.0" syn = "1.0" -argh_shared = { version = "0.1.5", path = "../argh_shared" } +argh_shared = { version = "0.1.7", path = "../argh_shared" } diff --git a/argh_derive/src/lib.rs b/argh_derive/src/lib.rs index 9a11077..a123d4e 100644 --- a/argh_derive/src/lib.rs +++ b/argh_derive/src/lib.rs @@ -626,8 +626,17 @@ fn declare_local_storage_for_redacted_fields<'a>( } } FieldKind::Option => { + let field_slot_type = match field.optionality { + Optionality::Repeating => { + quote! { std::vec::Vec } + } + Optionality::None | Optionality::Optional | Optionality::Defaulted(_) => { + quote! { std::option::Option } + } + }; + quote! { - let mut #field_name: argh::ParseValueSlotTy::, String> = + let mut #field_name: argh::ParseValueSlotTy::<#field_slot_type, String> = argh::ParseValueSlotTy { slot: std::default::Default::default(), parse_func: |arg, _| { Ok(arg.to_string()) }, @@ -668,13 +677,27 @@ fn unwrap_redacted_fields<'a>( let field_name = field.name; match field.kind { - FieldKind::Switch | FieldKind::Option => { + FieldKind::Switch => { quote! { if let Some(__field_name) = #field_name.slot { __redacted.push(__field_name); } } } + FieldKind::Option => match field.optionality { + Optionality::Repeating => { + quote! { + __redacted.extend(#field_name.slot.into_iter()); + } + } + Optionality::None | Optionality::Optional | Optionality::Defaulted(_) => { + quote! { + if let Some(__field_name) = #field_name.slot { + __redacted.push(__field_name); + } + } + } + }, FieldKind::Positional => { quote! { __redacted.extend(#field_name.slot.into_iter()); diff --git a/argh_shared/Cargo.toml b/argh_shared/Cargo.toml index 2c24892..1f7e694 100644 --- a/argh_shared/Cargo.toml +++ b/argh_shared/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "argh_shared" -version = "0.1.6" +version = "0.1.7" authors = ["Taylor Cramer ", "Benjamin Brittain ", "Erick Tryzelaar "] edition = "2018" license = "BSD-3-Clause"