mirror of
https://github.com/topjohnwu/argh.git
synced 2024-11-23 11:49:45 +00:00
Rename FromArgs::redact to FromArgs::redact_arg_values
This commit is contained in:
parent
bcb66a6915
commit
4ddfde0f1b
@ -247,7 +247,7 @@ fn impl_from_args_struct(
|
||||
&fields,
|
||||
);
|
||||
|
||||
let redact_method = impl_from_args_struct_redact(
|
||||
let redact_arg_values_method = impl_from_args_struct_redact_arg_values(
|
||||
errors,
|
||||
type_attrs,
|
||||
&fields,
|
||||
@ -259,7 +259,7 @@ fn impl_from_args_struct(
|
||||
impl argh::FromArgs for #name {
|
||||
#from_args_method
|
||||
|
||||
#redact_method
|
||||
#redact_arg_values_method
|
||||
}
|
||||
|
||||
#top_or_sub_cmd_impl
|
||||
@ -373,13 +373,13 @@ fn impl_from_args_struct_from_args<'a>(
|
||||
method_impl.into()
|
||||
}
|
||||
|
||||
fn impl_from_args_struct_redact<'a>(
|
||||
fn impl_from_args_struct_redact_arg_values<'a>(
|
||||
errors: &Errors,
|
||||
type_attrs: &TypeAttrs,
|
||||
fields: &'a [StructField<'a>],
|
||||
) -> TokenStream {
|
||||
let init_fields = declare_local_storage_for_redact_fields(&fields);
|
||||
let unwrap_fields = unwrap_redact_fields(&fields);
|
||||
let init_fields = declare_local_storage_for_redact_arg_values_fields(&fields);
|
||||
let unwrap_fields = unwrap_redact_arg_values_fields(&fields);
|
||||
|
||||
let positional_fields: Vec<&StructField<'_>> =
|
||||
fields.iter().filter(|field| field.kind == FieldKind::Positional).collect();
|
||||
@ -423,7 +423,7 @@ fn impl_from_args_struct_redact<'a>(
|
||||
Some(argh::ParseStructSubCommand {
|
||||
subcommands: <#ty as argh::SubCommands>::COMMANDS,
|
||||
parse_func: &mut |__command, __remaining_args| {
|
||||
#name = Some(<#ty as argh::FromArgs>::redact(__command, __remaining_args)?);
|
||||
#name = Some(<#ty as argh::FromArgs>::redact_arg_values(__command, __remaining_args)?);
|
||||
Ok(())
|
||||
},
|
||||
})
|
||||
@ -443,7 +443,7 @@ fn impl_from_args_struct_redact<'a>(
|
||||
let help = help::help(errors, cmd_name_str_array_ident, type_attrs, &fields, subcommand);
|
||||
|
||||
let method_impl = quote_spanned! { impl_span =>
|
||||
fn redact(__cmd_name: &[&str], __args: &[&str]) -> Result<Vec<String>, argh::EarlyExit> {
|
||||
fn redact_arg_values(__cmd_name: &[&str], __args: &[&str]) -> Result<Vec<String>, argh::EarlyExit> {
|
||||
#( #init_fields )*
|
||||
|
||||
argh::parse_struct_args(
|
||||
@ -613,7 +613,7 @@ fn unwrap_from_args_fields<'a>(fields: &'a [StructField<'a>]) -> impl Iterator<I
|
||||
/// Most fields are stored in `Option<FieldType>` locals.
|
||||
/// `argh(option)` fields are stored in a `ParseValueSlotTy` along with a
|
||||
/// function that knows how to decode the appropriate value.
|
||||
fn declare_local_storage_for_redact_fields<'a>(
|
||||
fn declare_local_storage_for_redact_arg_values_fields<'a>(
|
||||
fields: &'a [StructField<'a>],
|
||||
) -> impl Iterator<Item = TokenStream> + 'a {
|
||||
fields.iter().map(|field| {
|
||||
@ -664,7 +664,7 @@ fn declare_local_storage_for_redact_fields<'a>(
|
||||
}
|
||||
|
||||
/// Unwrap non-optional fields and take options out of their tuple slots.
|
||||
fn unwrap_redact_fields<'a>(fields: &'a [StructField<'a>]) -> impl Iterator<Item = TokenStream> + 'a {
|
||||
fn unwrap_redact_arg_values_fields<'a>(fields: &'a [StructField<'a>]) -> impl Iterator<Item = TokenStream> + 'a {
|
||||
fields.iter().map(|field| {
|
||||
let field_name = field.name;
|
||||
|
||||
@ -850,11 +850,11 @@ fn impl_from_args_enum(
|
||||
unreachable!("no subcommand matched")
|
||||
}
|
||||
|
||||
fn redact(command_name: &[&str], args: &[&str]) -> std::result::Result<Vec<String>, argh::EarlyExit> {
|
||||
fn redact_arg_values(command_name: &[&str], args: &[&str]) -> std::result::Result<Vec<String>, argh::EarlyExit> {
|
||||
let subcommand_name = *command_name.last().expect("no subcommand name");
|
||||
#(
|
||||
if subcommand_name == <#variant_ty as argh::SubCommand>::COMMAND.name {
|
||||
return <#variant_ty as argh::FromArgs>::redact(command_name, args);
|
||||
return <#variant_ty as argh::FromArgs>::redact_arg_values(command_name, args);
|
||||
}
|
||||
)*
|
||||
unreachable!("no subcommand matched")
|
||||
|
12
src/lib.rs
12
src/lib.rs
@ -247,7 +247,7 @@ pub trait FromArgs: Sized {
|
||||
/// students: Vec<String>,
|
||||
/// }
|
||||
///
|
||||
/// let args = ClassroomCmd::redact(
|
||||
/// let args = ClassroomCmd::redact_arg_values(
|
||||
/// &["classroom"],
|
||||
/// &["list"],
|
||||
/// ).unwrap();
|
||||
@ -259,7 +259,7 @@ pub trait FromArgs: Sized {
|
||||
/// ],
|
||||
/// );
|
||||
///
|
||||
/// let args = ClassroomCmd::redact(
|
||||
/// let args = ClassroomCmd::redact_arg_values(
|
||||
/// &["classroom"],
|
||||
/// &["list", "--teacher-name", "Smith"],
|
||||
/// ).unwrap();
|
||||
@ -272,7 +272,7 @@ pub trait FromArgs: Sized {
|
||||
/// ],
|
||||
/// );
|
||||
///
|
||||
/// let args = ClassroomCmd::redact(
|
||||
/// let args = ClassroomCmd::redact_arg_values(
|
||||
/// &["classroom"],
|
||||
/// &["add", "--teacher-name", "Smith", "--started", "Math", "Abe", "Sung"],
|
||||
/// ).unwrap();
|
||||
@ -289,16 +289,16 @@ pub trait FromArgs: Sized {
|
||||
/// ],
|
||||
/// );
|
||||
///
|
||||
/// // `ClassroomCmd::redact` will error out if passed invalid arguments.
|
||||
/// // `ClassroomCmd::redact_arg_values` will error out if passed invalid arguments.
|
||||
/// assert_eq!(
|
||||
/// ClassroomCmd::redact(&["classroom"], &["add", "--teacher-name"]),
|
||||
/// ClassroomCmd::redact_arg_values(&["classroom"], &["add", "--teacher-name"]),
|
||||
/// Err(argh::EarlyExit {
|
||||
/// output: "No value provided for option '--teacher-name'.\n".into(),
|
||||
/// status: Err(()),
|
||||
/// }),
|
||||
/// );
|
||||
/// ```
|
||||
fn redact(_command_name: &[&str], _args: &[&str]) -> Result<Vec<String>, EarlyExit> {
|
||||
fn redact_arg_values(_command_name: &[&str], _args: &[&str]) -> Result<Vec<String>, EarlyExit> {
|
||||
Ok(vec!["<<REDACTED>>".into()])
|
||||
}
|
||||
}
|
||||
|
64
tests/lib.rs
64
tests/lib.rs
@ -878,7 +878,7 @@ Error codes:
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_no_args() {
|
||||
fn redact_arg_values_no_args() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -887,12 +887,12 @@ fn redact_no_args() {
|
||||
msg: Option<String>,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &[]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &[]).unwrap();
|
||||
assert_eq!(actual, &["program-name"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_optional_arg() {
|
||||
fn redact_arg_values_optional_arg() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -901,12 +901,12 @@ fn redact_optional_arg() {
|
||||
msg: Option<String>,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["--msg", "hello"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["--msg", "hello"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "--msg"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_two_option_args() {
|
||||
fn redact_arg_values_two_option_args() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -920,12 +920,12 @@ fn redact_two_option_args() {
|
||||
}
|
||||
|
||||
let actual =
|
||||
Cmd::redact(&["program-name"], &["--msg", "hello", "--delivery", "next day"]).unwrap();
|
||||
Cmd::redact_arg_values(&["program-name"], &["--msg", "hello", "--delivery", "next day"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "--msg", "--delivery"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_option_one_optional_args() {
|
||||
fn redact_arg_values_option_one_optional_args() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -939,15 +939,15 @@ fn redact_option_one_optional_args() {
|
||||
}
|
||||
|
||||
let actual =
|
||||
Cmd::redact(&["program-name"], &["--msg", "hello", "--delivery", "next day"]).unwrap();
|
||||
Cmd::redact_arg_values(&["program-name"], &["--msg", "hello", "--delivery", "next day"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "--msg", "--delivery"]);
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["--msg", "hello"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["--msg", "hello"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "--msg"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_switch() {
|
||||
fn redact_arg_values_switch() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -956,15 +956,15 @@ fn redact_switch() {
|
||||
faster: bool,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["--faster"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["--faster"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "--faster"]);
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["-f"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["-f"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "-f"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_positional() {
|
||||
fn redact_arg_values_positional() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -973,12 +973,12 @@ fn redact_positional() {
|
||||
speed: u8,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_positional_repeating() {
|
||||
fn redact_arg_values_positional_repeating() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -987,12 +987,12 @@ fn redact_positional_repeating() {
|
||||
speed: Vec<u8>,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5", "6"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5", "6"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed", "speed"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_positional_err() {
|
||||
fn redact_arg_values_positional_err() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -1001,7 +1001,7 @@ fn redact_positional_err() {
|
||||
speed: u8,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &[]).unwrap_err();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &[]).unwrap_err();
|
||||
assert_eq!(
|
||||
actual,
|
||||
argh::EarlyExit {
|
||||
@ -1012,7 +1012,7 @@ fn redact_positional_err() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_two_positional() {
|
||||
fn redact_arg_values_two_positional() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -1025,12 +1025,12 @@ fn redact_two_positional() {
|
||||
direction: String,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5", "north"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5", "north"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed", "direction"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_positional_option() {
|
||||
fn redact_arg_values_positional_option() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -1043,12 +1043,12 @@ fn redact_positional_option() {
|
||||
direction: String,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5", "--direction", "north"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5", "--direction", "north"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed", "--direction"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_positional_optional_option() {
|
||||
fn redact_arg_values_positional_optional_option() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -1061,12 +1061,12 @@ fn redact_positional_optional_option() {
|
||||
direction: Option<String>,
|
||||
}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_subcommand() {
|
||||
fn redact_arg_values_subcommand() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -1106,12 +1106,12 @@ fn redact_subcommand() {
|
||||
/// short description
|
||||
struct DrivingSubcommand {}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5", "walking", "--music", "Bach"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5", "walking", "--music", "Bach"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed", "walking", "--music"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_subcommand_with_space_in_name() {
|
||||
fn redact_arg_values_subcommand_with_space_in_name() {
|
||||
#[derive(FromArgs, Debug)]
|
||||
/// Short description
|
||||
struct Cmd {
|
||||
@ -1146,12 +1146,12 @@ fn redact_subcommand_with_space_in_name() {
|
||||
/// Short description
|
||||
struct BikingSubcommand {}
|
||||
|
||||
let actual = Cmd::redact(&["program-name"], &["5", "has space", "--music", "Bach"]).unwrap();
|
||||
let actual = Cmd::redact_arg_values(&["program-name"], &["5", "has space", "--music", "Bach"]).unwrap();
|
||||
assert_eq!(actual, &["program-name", "speed", "has space", "--music"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_produces_help() {
|
||||
fn redact_arg_values_produces_help() {
|
||||
#[derive(argh::FromArgs, Debug, PartialEq)]
|
||||
/// Woot
|
||||
struct Repeating {
|
||||
@ -1161,7 +1161,7 @@ fn redact_produces_help() {
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
Repeating::redact(&["program-name"], &["--help"]),
|
||||
Repeating::redact_arg_values(&["program-name"], &["--help"]),
|
||||
Err(argh::EarlyExit {
|
||||
output: r###"Usage: program-name [-n <n...>]
|
||||
|
||||
@ -1178,7 +1178,7 @@ Options:
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn redact_produces_errors_with_bad_arguments() {
|
||||
fn redact_arg_values_produces_errors_with_bad_arguments() {
|
||||
#[derive(argh::FromArgs, Debug, PartialEq)]
|
||||
/// Woot
|
||||
struct Cmd {
|
||||
@ -1188,7 +1188,7 @@ fn redact_produces_errors_with_bad_arguments() {
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
Cmd::redact(&["program-name"], &["--n"]),
|
||||
Cmd::redact_arg_values(&["program-name"], &["--n"]),
|
||||
Err(argh::EarlyExit {
|
||||
output: "No value provided for option '--n'.\n".to_string(),
|
||||
status: Err(()),
|
||||
|
Loading…
Reference in New Issue
Block a user