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:
parent
ada5df3229
commit
709f20d713
3 changed files with 49 additions and 35 deletions
|
@ -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,
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue