Skip to content

Commit

Permalink
Fix redacting Vec<T> options
Browse files Browse the repository at this point in the history
This changes argh_derive to handle redacting types like:

```
struct Cmd {
    #[argh(option)]
    /// fooey
    arg: Vec<String>,
}
```

Fixes #111
  • Loading branch information
erickt committed Dec 7, 2021
1 parent f102494 commit 69a8bb6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
6 changes: 3 additions & 3 deletions argh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "argh"
version = "0.1.6"
version = "0.1.7"
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
edition = "2018"
keywords = ["args", "arguments", "derive", "cli"]
Expand All @@ -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" }
18 changes: 18 additions & 0 deletions argh/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

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)]
Expand Down
4 changes: 2 additions & 2 deletions argh_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "argh_derive"
version = "0.1.6"
version = "0.1.7"
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
edition = "2018"
license = "BSD-3-Clause"
Expand All @@ -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" }
27 changes: 25 additions & 2 deletions argh_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> }
}
Optionality::None | Optionality::Optional | Optionality::Defaulted(_) => {
quote! { std::option::Option<String> }
}
};

quote! {
let mut #field_name: argh::ParseValueSlotTy::<Option<String>, 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()) },
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion argh_shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "argh_shared"
version = "0.1.6"
version = "0.1.7"
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
edition = "2018"
license = "BSD-3-Clause"
Expand Down

0 comments on commit 69a8bb6

Please sign in to comment.