Skip to content

Commit 5676ea1

Browse files
committed
Fix redacting Vec<T> options
This changes argh_derive to handle redacting types like: ``` struct Cmd { #[argh(option)] /// fooey arg: Vec<String>, } ``` Fixes #111
1 parent 3a363a6 commit 5676ea1

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

argh/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "argh"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
55
edition = "2018"
66
keywords = ["args", "arguments", "derive", "cli"]
@@ -10,5 +10,5 @@ repository = "https://github.com/google/argh"
1010
readme = "README.md"
1111

1212
[dependencies]
13-
argh_shared = { version = "0.1.6", path = "../argh_shared" }
14-
argh_derive = { version = "0.1.6", path = "../argh_derive" }
13+
argh_shared = { version = "0.1.7", path = "../argh_shared" }
14+
argh_derive = { version = "0.1.7", path = "../argh_derive" }

argh/tests/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,24 @@ fn redact_arg_values_option_one_optional_args() {
10041004
assert_eq!(actual, &["program-name", "--msg"]);
10051005
}
10061006

1007+
#[test]
1008+
fn redact_arg_values_option_repeating() {
1009+
#[derive(FromArgs, Debug)]
1010+
/// Short description
1011+
struct Cmd {
1012+
#[argh(option)]
1013+
/// fooey
1014+
_msg: Vec<String>,
1015+
}
1016+
1017+
let actual = Cmd::redact_arg_values(&["program-name"], &[]).unwrap();
1018+
assert_eq!(actual, &["program-name"]);
1019+
1020+
let actual =
1021+
Cmd::redact_arg_values(&["program-name"], &["--msg", "abc", "--msg", "xyz"]).unwrap();
1022+
assert_eq!(actual, &["program-name", "--msg", "--msg"]);
1023+
}
1024+
10071025
#[test]
10081026
fn redact_arg_values_switch() {
10091027
#[derive(FromArgs, Debug)]

argh_derive/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "argh_derive"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
55
edition = "2018"
66
license = "BSD-3-Clause"
@@ -16,4 +16,4 @@ heck = "0.3.1"
1616
proc-macro2 = "1.0"
1717
quote = "1.0"
1818
syn = "1.0"
19-
argh_shared = { version = "0.1.5", path = "../argh_shared" }
19+
argh_shared = { version = "0.1.7", path = "../argh_shared" }

argh_derive/src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,17 @@ fn declare_local_storage_for_redacted_fields<'a>(
626626
}
627627
}
628628
FieldKind::Option => {
629+
let field_slot_type = match field.optionality {
630+
Optionality::Repeating => {
631+
quote! { std::vec::Vec<String> }
632+
}
633+
Optionality::None | Optionality::Optional | Optionality::Defaulted(_) => {
634+
quote! { std::option::Option<String> }
635+
}
636+
};
637+
629638
quote! {
630-
let mut #field_name: argh::ParseValueSlotTy::<Option<String>, String> =
639+
let mut #field_name: argh::ParseValueSlotTy::<#field_slot_type, String> =
631640
argh::ParseValueSlotTy {
632641
slot: std::default::Default::default(),
633642
parse_func: |arg, _| { Ok(arg.to_string()) },
@@ -668,13 +677,27 @@ fn unwrap_redacted_fields<'a>(
668677
let field_name = field.name;
669678

670679
match field.kind {
671-
FieldKind::Switch | FieldKind::Option => {
680+
FieldKind::Switch => {
672681
quote! {
673682
if let Some(__field_name) = #field_name.slot {
674683
__redacted.push(__field_name);
675684
}
676685
}
677686
}
687+
FieldKind::Option => match field.optionality {
688+
Optionality::Repeating => {
689+
quote! {
690+
__redacted.extend(#field_name.slot.into_iter());
691+
}
692+
}
693+
Optionality::None | Optionality::Optional | Optionality::Defaulted(_) => {
694+
quote! {
695+
if let Some(__field_name) = #field_name.slot {
696+
__redacted.push(__field_name);
697+
}
698+
}
699+
}
700+
},
678701
FieldKind::Positional => {
679702
quote! {
680703
__redacted.extend(#field_name.slot.into_iter());

argh_shared/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "argh_shared"
3-
version = "0.1.6"
3+
version = "0.1.7"
44
authors = ["Taylor Cramer <[email protected]>", "Benjamin Brittain <[email protected]>", "Erick Tryzelaar <[email protected]>"]
55
edition = "2018"
66
license = "BSD-3-Clause"

0 commit comments

Comments
 (0)