Skip to content

Commit

Permalink
Merge clap-rs#2994
Browse files Browse the repository at this point in the history
2994: Revert:  Add new App::mut_args for mutating all arguments clap-rs#2966  r=pksunkara a=epage



Co-authored-by: Ed Page <eopage@gmail.com>
  • Loading branch information
bors[bot] and epage authored Nov 5, 2021
2 parents b9d007d + 839ad67 commit 6b0c49e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 95 deletions.
39 changes: 0 additions & 39 deletions src/build/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1808,45 +1808,6 @@ impl<'help> App<'help> {
self
}

/// Allows one to mutate all arguments after they've been added to an [`App`].
///
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg};
///
/// let mut app = App::new("foo")
/// .arg(Arg::new("foo")
/// .short('x'))
/// .arg(Arg::new("bar")
/// .short('y'))
/// .mut_args(|a| (a.get_name() == "foo").then(|| a.short('f')));
///
/// let res = app.try_get_matches_from_mut(vec!["foo", "-x"]);
///
/// // Since we changed `foo`'s short to "f" this should err as there
/// // is no `-x` anymore, only `-f`
///
/// assert!(res.is_err());
///
/// let res = app.try_get_matches_from_mut(vec!["foo", "-f"]);
/// assert!(res.is_ok());
/// ```
pub fn mut_args<F>(mut self, f: F) -> Self
where
F: Fn(Arg<'help>) -> Option<Arg<'help>>,
{
for a in self.args.args_mut() {
if let Some(arg) = f(a.clone()) {
*a = arg;
if a.provider == ArgProvider::Generated {
a.provider = ArgProvider::GeneratedMutated;
}
}
}
self
}

/// Custom error message for post-parsing validation
///
/// # Examples
Expand Down
56 changes: 0 additions & 56 deletions tests/app_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,6 @@ SUBCOMMANDS:
";

static PARTIAL_MUT_ARGS: &str = "myprog 3.0
USAGE:
myprog [OPTIONS]
OPTIONS:
--first first about
-h, --help Print help information
-s, --second
--third third arg
-V, --version Print version information
";

#[test]
fn setting() {
let m = App::new("setting").setting(AppSettings::AllArgsOverrideSelf);
Expand Down Expand Up @@ -1229,46 +1216,3 @@ fn no_auto_version_mut_arg() {
assert!(result.is_ok());
assert!(result.unwrap().is_present("version"));
}

#[test]
fn no_auto_version_mut_args() {
let app = App::new("myprog")
.version("3.0")
.mut_args(|v| Some(v.about("custom about")))
.setting(AppSettings::NoAutoVersion);

let result = app
.clone()
.try_get_matches_from("myprog --version".split(" "));

assert!(result.is_ok());
assert!(result.unwrap().is_present("version"));

let result = app.clone().try_get_matches_from("myprog -V".split(" "));

assert!(result.is_ok());
assert!(result.unwrap().is_present("version"));
}

#[test]
fn partial_mut_args() {
let app = App::new("myprog")
.version("3.0")
.arg(
Arg::new("first").long("first").about("first arg"), // this about will be replaced
)
.arg(Arg::new("second").long("second").short('r')) // this short will be replaced
.arg(Arg::new("third").long("third").about("third arg")) // this will not be mutated
.mut_args(|v| match v.get_name() {
"first" => Some(v.about("first about")),
"second" => Some(v.short('s')),
_ => None,
});

assert!(utils::compare_output(
app,
"myprog -h",
PARTIAL_MUT_ARGS,
false
));
}

0 comments on commit 6b0c49e

Please sign in to comment.