2
0
Fork 0
mirror of https://github.com/clap-rs/clap synced 2024-12-14 23:02:31 +00:00

Improve SubcommandPrecedenceOverArg description

Signed-off-by: David McNeil <mcneil.david2@gmail.com>
This commit is contained in:
David McNeil 2020-04-21 08:31:49 -05:00
parent ada5df3229
commit 709f20d713
3 changed files with 49 additions and 35 deletions

View file

@ -442,13 +442,57 @@ pub enum AppSettings {
/// Instructs the parser to stop when encountering a subcommand instead of greedily consuming /// Instructs the parser to stop when encountering a subcommand instead of greedily consuming
/// args. /// args.
/// ///
/// By default, if an option taking multiple values is followed by a subcommand, the
/// subcommand will be parsed as another value.
///
/// ```text
/// app --foo val1 val2 subcommand
/// --------- ----------
/// values another value
/// ```
///
/// This setting instructs the parser to stop when encountering a subcommand instead of
/// greedily consuming arguments.
///
/// ```text
/// app --foo val1 val2 subcommand
/// --------- ----------
/// values subcommand
/// ```
///
/// **Note:** Make sure you apply it as `global_setting` if you want it to be propagated to
/// sub-sub commands!
///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, AppSettings}; /// # use clap::{App, AppSettings, Arg};
/// App::new("myprog") /// let app = App::new("app").subcommand(App::new("sub")).arg(
/// .setting(AppSettings::SubcommandPrecedenceOverArg) /// Arg::with_name("arg")
/// # ; /// .long("arg")
/// .multiple(true)
/// .takes_value(true),
/// );
///
/// let matches = app
/// .clone()
/// .try_get_matches_from(&["app", "--arg", "1", "2", "3", "sub"])
/// .unwrap();
/// assert_eq!(
/// matches.values_of("arg").unwrap().collect::<Vec<_>>(),
/// &["1", "2", "3", "sub"]
/// );
/// assert!(matches.subcommand_matches("sub").is_none());
///
/// let app = app.setting(AppSettings::SubcommandPrecedenceOverArg);
/// let matches = app
/// .try_get_matches_from(&["app", "--arg", "1", "2", "3", "sub"])
/// .unwrap();
/// assert_eq!(
/// matches.values_of("arg").unwrap().collect::<Vec<_>>(),
/// &["1", "2", "3"]
/// );
/// assert!(matches.subcommand_matches("sub").is_some());
/// ``` /// ```
SubcommandPrecedenceOverArg, SubcommandPrecedenceOverArg,

View file

@ -124,36 +124,6 @@ fn arg_required_else_help_over_reqs() {
assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand); assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand);
} }
#[test]
fn subcommand_precedence_over_arg() {
let app = App::new("app").subcommand(App::new("sub")).arg(
Arg::with_name("arg")
.long("arg")
.multiple(true)
.takes_value(true),
);
let matches = app
.clone()
.try_get_matches_from(&["app", "--arg", "1", "2", "3", "sub"])
.unwrap();
assert_eq!(
matches.values_of("arg").unwrap().collect::<Vec<_>>(),
&["1", "2", "3", "sub"]
);
assert!(matches.subcommand_matches("sub").is_none());
let app = app.setting(AppSettings::SubcommandPrecedenceOverArg);
let matches = app
.try_get_matches_from(&["app", "--arg", "1", "2", "3", "sub"])
.unwrap();
assert_eq!(
matches.values_of("arg").unwrap().collect::<Vec<_>>(),
&["1", "2", "3"]
);
assert!(matches.subcommand_matches("sub").is_some());
}
#[cfg(not(feature = "suggestions"))] #[cfg(not(feature = "suggestions"))]
#[test] #[test]
fn infer_subcommands_fail_no_args() { fn infer_subcommands_fail_no_args() {

View file

@ -1,6 +1,6 @@
mod utils; mod utils;
use clap::{App, AppSettings, Arg, ErrorKind}; use clap::{App, Arg, ErrorKind};
static VISIBLE_ALIAS_HELP: &str = "clap-test 2.6 static VISIBLE_ALIAS_HELP: &str = "clap-test 2.6