Merge pull request #1377 from clap-rs/v3-towards-finish

V3 towards finish
This commit is contained in:
Kevin K 2018-11-08 20:35:11 -05:00 committed by GitHub
commit 88c7677e11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
65 changed files with 904 additions and 3488 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "clap" name = "clap"
version = "3.0.0-alpha.1" version = "3.0.0-beta.1"
authors = ["Kevin K. <kbknapp@gmail.com>"] authors = ["Kevin K. <kbknapp@gmail.com>"]
exclude = [ exclude = [
".github/*", ".github/*",
@ -82,13 +82,13 @@ debug = [] # Enables debug messages
no_cargo = [] # Enable if you're not using Cargo, disables Cargo-env-var-dependent macros no_cargo = [] # Enable if you're not using Cargo, disables Cargo-env-var-dependent macros
doc = ["yaml"] # All the features which add to documentation doc = ["yaml"] # All the features which add to documentation
[profile.release] # [profile.release]
opt-level = 3 # opt-level = 3
debug = false # debug = false
rpath = false # rpath = false
lto = true # lto = true
debug-assertions = false # debug-assertions = false
codegen-units = 1 # codegen-units = 1
[profile.dev] [profile.dev]
opt-level = 0 opt-level = 0

View file

@ -289,7 +289,7 @@ fn main() {
.about("controls testing features") .about("controls testing features")
.version("1.3") .version("1.3")
.author("Someone E. <someone_else@other.com>") .author("Someone E. <someone_else@other.com>")
.arg_from_usage("-d, --debug 'Print debug information'")) .arg("-d, --debug 'Print debug information'"))
.get_matches(); .get_matches();
// Same as previous example... // Same as previous example...
@ -334,7 +334,7 @@ subcommands:
Since this feature requires additional dependencies that not everyone may want, it is *not* compiled in by default and we need to enable a feature flag in Cargo.toml: Since this feature requires additional dependencies that not everyone may want, it is *not* compiled in by default and we need to enable a feature flag in Cargo.toml:
Simply change your `clap = "3.0.0-alpha.1"` to `clap = {version = "3.0.0-alpha.1", features = ["yaml"]}`. Simply change your `clap = "3.0.0-beta.1"` to `clap = {version = "3.0.0-beta.1", features = ["yaml"]}`.
Finally we create our `main.rs` file just like we would have with the previous two examples: Finally we create our `main.rs` file just like we would have with the previous two examples:
@ -405,7 +405,7 @@ To test out `clap`'s default auto-generated help/version follow these steps:
```toml ```toml
[dependencies] [dependencies]
clap = "3.0.0-alpha.1" clap = "3.0.0-beta.1"
``` ```
* Add the following to your `src/main.rs` * Add the following to your `src/main.rs`
@ -434,7 +434,7 @@ For full usage, add `clap` as a dependency in your `Cargo.toml` () to use from c
```toml ```toml
[dependencies] [dependencies]
clap = "~3.0.0-alpha.1" clap = "~3.0.0-beta.1"
``` ```
(**note**: If you are concerned with supporting a minimum version of Rust that is *older* than the current stable Rust minus 2 stable releases, it's recommended to use the `~major.minor.patch` style versions in your `Cargo.toml` which will only update the patch version automatically. For more information see the [Compatibility Policy](#compatibility-policy)) (**note**: If you are concerned with supporting a minimum version of Rust that is *older* than the current stable Rust minus 2 stable releases, it's recommended to use the `~major.minor.patch` style versions in your `Cargo.toml` which will only update the patch version automatically. For more information see the [Compatibility Policy](#compatibility-policy))
@ -458,7 +458,7 @@ To disable these, add this to your `Cargo.toml`:
```toml ```toml
[dependencies.clap] [dependencies.clap]
version = "3.0.0-alpha.1" version = "3.0.0-beta.1"
default-features = false default-features = false
``` ```
@ -466,7 +466,7 @@ You can also selectively enable only the features you'd like to include, by addi
```toml ```toml
[dependencies.clap] [dependencies.clap]
version = "3.0.0-alpha.1" version = "3.0.0-beta.1"
default-features = false default-features = false
# Cherry-pick the features you'd like to use # Cherry-pick the features you'd like to use
@ -509,7 +509,7 @@ In order to keep from being surprised of breaking changes, it is **highly** reco
```toml ```toml
[dependencies] [dependencies]
clap = "~3.0.0-alpha.1" clap = "~3.0.0-beta.1"
``` ```
This will cause *only* the patch version to be updated upon a `cargo update` call, and therefore cannot break due to new features, or bumped minimum versions of Rust. This will cause *only* the patch version to be updated upon a `cargo update` call, and therefore cannot break due to new features, or bumped minimum versions of Rust.
@ -526,11 +526,11 @@ Right now Cargo's version resolution is pretty naive, it's just a brute-force se
# In one Cargo.toml # In one Cargo.toml
[dependencies] [dependencies]
clap = "~3.0.0-alpha.1" clap = "~3.0.0-beta.1"
# In another Cargo.toml # In another Cargo.toml
[dependencies] [dependencies]
clap = "3.0.0-alpha.1" clap = "3.0.0-beta.1"
``` ```
This is inherently an unresolvable crate graph in Cargo right now. Cargo requires there's only one major version of a crate, and being in the same workspace these two crates must share a version. This is impossible in this location, though, as these version constraints cannot be met. This is inherently an unresolvable crate graph in Cargo right now. Cargo requires there's only one major version of a crate, and being in the same workspace these two crates must share a version. This is impossible in this location, though, as these version constraints cannot be met.

View file

@ -4,7 +4,7 @@
extern crate clap; extern crate clap;
extern crate test; extern crate test;
use clap::{App, AppSettings, Arg, ArgSettings, SubCommand}; use clap::{App, AppSettings, Arg, ArgSettings, };
use test::Bencher; use test::Bencher;
@ -39,12 +39,12 @@ macro_rules! create_app {
Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3), Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3),
]) ])
.subcommand( .subcommand(
SubCommand::with_name("subcmd") App::new("subcmd")
.about("tests subcommands") .about("tests subcommands")
.version("0.1") .version("0.1")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.arg_from_usage("-o --option [scoption]... 'tests options'") .arg("-o --option [scoption]... 'tests options'")
.arg_from_usage("[scpositional] 'tests positionals'"), .arg("[scpositional] 'tests positionals'"),
) )
}}; }};
} }
@ -146,7 +146,7 @@ fn create_app_builder(b: &mut Bencher) {
.max_values(3), .max_values(3),
) )
.subcommand( .subcommand(
SubCommand::with_name("subcmd") App::new("subcmd")
.about("tests subcommands") .about("tests subcommands")
.version("0.1") .version("0.1")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")

View file

@ -8,7 +8,7 @@ use test::Bencher;
use std::io::Cursor; use std::io::Cursor;
use clap::App; use clap::App;
use clap::{Arg, ArgSettings, SubCommand}; use clap::{Arg, ArgSettings, };
fn build_help(app: &mut App) -> String { fn build_help(app: &mut App) -> String {
let mut buf = Cursor::new(Vec::with_capacity(50)); let mut buf = Cursor::new(Vec::with_capacity(50));
@ -28,9 +28,9 @@ fn app_example1<'b, 'c>() -> App<'b, 'c> {
-d... 'Turn debugging information on'", -d... 'Turn debugging information on'",
) )
.subcommand( .subcommand(
SubCommand::with_name("test") App::new("test")
.about("does testing things") .about("does testing things")
.arg_from_usage("-l, --list 'lists test values'"), .arg("-l, --list 'lists test values'"),
) )
} }
@ -59,7 +59,7 @@ fn app_example3<'b, 'c>() -> App<'b, 'c> {
.index(1) .index(1)
.setting(ArgSettings::Required), .setting(ArgSettings::Required),
]) ])
.arg_from_usage("--license 'display the license file'") .arg("--license 'display the license file'")
.args_from_usage( .args_from_usage(
"[output] 'Supply an output file to use' "[output] 'Supply an output file to use'
-i, --int=[IFACE] 'Set an interface to use'", -i, --int=[IFACE] 'Set an interface to use'",

View file

@ -7,7 +7,7 @@
extern crate clap; extern crate clap;
extern crate test; extern crate test;
use clap::{App, AppSettings, Arg, ArgGroup, ArgSettings, Shell, SubCommand}; use clap::{App, AppSettings, Arg, ArgGroup, ArgSettings, Shell, };
use test::Bencher; use test::Bencher;
@ -34,16 +34,16 @@ pub fn build_cli() -> App<'static, 'static> {
.help("Enable verbose output") .help("Enable verbose output")
.short('v') .short('v')
.long("verbose")) .long("verbose"))
.subcommand(SubCommand::with_name("show") .subcommand(App::new("show")
.about("Show the active and installed toolchains") .about("Show the active and installed toolchains")
.after_help(SHOW_HELP)) .after_help(SHOW_HELP))
.subcommand(SubCommand::with_name("install") .subcommand(App::new("install")
.about("Update Rust toolchains") .about("Update Rust toolchains")
.after_help(TOOLCHAIN_INSTALL_HELP) .after_help(TOOLCHAIN_INSTALL_HELP)
.setting(AppSettings::Hidden) // synonym for 'toolchain install' .setting(AppSettings::Hidden) // synonym for 'toolchain install'
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required))) .setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("update") .subcommand(App::new("update")
.about("Update Rust toolchains") .about("Update Rust toolchains")
.after_help(UPDATE_HELP) .after_help(UPDATE_HELP)
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required)) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required))
@ -51,85 +51,85 @@ pub fn build_cli() -> App<'static, 'static> {
.help("Don't perform self update when running the `rustup` command") .help("Don't perform self update when running the `rustup` command")
.long("no-self-update") .long("no-self-update")
.setting(ArgSettings::Hidden))) .setting(ArgSettings::Hidden)))
.subcommand(SubCommand::with_name("default") .subcommand(App::new("default")
.about("Set the default toolchain") .about("Set the default toolchain")
.after_help(DEFAULT_HELP) .after_help(DEFAULT_HELP)
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required))) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("toolchain") .subcommand(App::new("toolchain")
.about("Modify or query the installed toolchains") .about("Modify or query the installed toolchains")
.after_help(TOOLCHAIN_HELP) .after_help(TOOLCHAIN_HELP)
.setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp) // .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list").about("List installed toolchains")) .subcommand(App::new("list").about("List installed toolchains"))
.subcommand(SubCommand::with_name("install") .subcommand(App::new("install")
.about("Install or update a given toolchain") .about("Install or update a given toolchain")
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required))) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("uninstall") .subcommand(App::new("uninstall")
.about("Uninstall a toolchain") .about("Uninstall a toolchain")
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required))) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("link") .subcommand(App::new("link")
.about("Create a custom toolchain by symlinking to a directory") .about("Create a custom toolchain by symlinking to a directory")
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required)) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required))
.arg(Arg::with_name("path").setting(ArgSettings::Required))) .arg(Arg::with_name("path").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("update") .subcommand(App::new("update")
.setting(AppSettings::Hidden) // synonym for 'install' .setting(AppSettings::Hidden) // synonym for 'install'
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required))) .setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("add") .subcommand(App::new("add")
.setting(AppSettings::Hidden) // synonym for 'install' .setting(AppSettings::Hidden) // synonym for 'install'
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required))) .setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("remove") .subcommand(App::new("remove")
.setting(AppSettings::Hidden) // synonym for 'uninstall' .setting(AppSettings::Hidden) // synonym for 'uninstall'
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required)))) .setting(ArgSettings::Required))))
.subcommand(SubCommand::with_name("target") .subcommand(App::new("target")
.about("Modify a toolchain's supported targets") .about("Modify a toolchain's supported targets")
.setting(AppSettings::VersionlessSubcommands) .setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp) // .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list") .subcommand(App::new("list")
.about("List installed and available targets") .about("List installed and available targets")
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("add") .subcommand(App::new("add")
.about("Add a target to a Rust toolchain") .about("Add a target to a Rust toolchain")
.arg(Arg::with_name("target").setting(ArgSettings::Required)) .arg(Arg::with_name("target").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("remove") .subcommand(App::new("remove")
.about("Remove a target from a Rust toolchain") .about("Remove a target from a Rust toolchain")
.arg(Arg::with_name("target").setting(ArgSettings::Required)) .arg(Arg::with_name("target").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("install") .subcommand(App::new("install")
.setting(AppSettings::Hidden) // synonym for 'add' .setting(AppSettings::Hidden) // synonym for 'add'
.arg(Arg::with_name("target") .arg(Arg::with_name("target")
.setting(ArgSettings::Required)) .setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("uninstall") .subcommand(App::new("uninstall")
.setting(AppSettings::Hidden) // synonym for 'remove' .setting(AppSettings::Hidden) // synonym for 'remove'
.arg(Arg::with_name("target") .arg(Arg::with_name("target")
.setting(ArgSettings::Required)) .setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue)))) .setting(ArgSettings::TakesValue))))
.subcommand(SubCommand::with_name("component") .subcommand(App::new("component")
.about("Modify a toolchain's installed components") .about("Modify a toolchain's installed components")
.setting(AppSettings::VersionlessSubcommands) .setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp) // .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list") .subcommand(App::new("list")
.about("List installed and available components") .about("List installed and available components")
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("add") .subcommand(App::new("add")
.about("Add a component to a Rust toolchain") .about("Add a component to a Rust toolchain")
.arg(Arg::with_name("component").setting(ArgSettings::Required)) .arg(Arg::with_name("component").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
@ -138,7 +138,7 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("target") .arg(Arg::with_name("target")
.long("target") .long("target")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("remove") .subcommand(App::new("remove")
.about("Remove a component from a Rust toolchain") .about("Remove a component from a Rust toolchain")
.arg(Arg::with_name("component").setting(ArgSettings::Required)) .arg(Arg::with_name("component").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
@ -147,17 +147,17 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("target") .arg(Arg::with_name("target")
.long("target") .long("target")
.setting(ArgSettings::TakesValue)))) .setting(ArgSettings::TakesValue))))
.subcommand(SubCommand::with_name("override") .subcommand(App::new("override")
.about("Modify directory toolchain overrides") .about("Modify directory toolchain overrides")
.after_help(OVERRIDE_HELP) .after_help(OVERRIDE_HELP)
.setting(AppSettings::VersionlessSubcommands) .setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp) // .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list").about("List directory toolchain overrides")) .subcommand(App::new("list").about("List directory toolchain overrides"))
.subcommand(SubCommand::with_name("set") .subcommand(App::new("set")
.about("Set the override toolchain for a directory") .about("Set the override toolchain for a directory")
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required))) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("unset") .subcommand(App::new("unset")
.about("Remove the override toolchain for a directory") .about("Remove the override toolchain for a directory")
.after_help(OVERRIDE_UNSET_HELP) .after_help(OVERRIDE_UNSET_HELP)
.arg(Arg::with_name("path") .arg(Arg::with_name("path")
@ -167,11 +167,11 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("nonexistent") .arg(Arg::with_name("nonexistent")
.long("nonexistent") .long("nonexistent")
.help("Remove override toolchain for all nonexistent directories"))) .help("Remove override toolchain for all nonexistent directories")))
.subcommand(SubCommand::with_name("add") .subcommand(App::new("add")
.setting(AppSettings::Hidden) // synonym for 'set' .setting(AppSettings::Hidden) // synonym for 'set'
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required))) .setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("remove") .subcommand(App::new("remove")
.setting(AppSettings::Hidden) // synonym for 'unset' .setting(AppSettings::Hidden) // synonym for 'unset'
.about("Remove the override toolchain for a directory") .about("Remove the override toolchain for a directory")
.arg(Arg::with_name("path") .arg(Arg::with_name("path")
@ -180,17 +180,17 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("nonexistent") .arg(Arg::with_name("nonexistent")
.long("nonexistent") .long("nonexistent")
.help("Remove override toolchain for all nonexistent directories")))) .help("Remove override toolchain for all nonexistent directories"))))
.subcommand(SubCommand::with_name("run") .subcommand(App::new("run")
.about("Run a command with an environment configured for a given toolchain") .about("Run a command with an environment configured for a given toolchain")
.after_help(RUN_HELP) .after_help(RUN_HELP)
.setting(AppSettings::TrailingVarArg) .setting(AppSettings::TrailingVarArg)
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required)) .arg(Arg::with_name("toolchain").setting(ArgSettings::Required))
.arg(Arg::with_name("command") .arg(Arg::with_name("command")
.settings(&[ArgSettings::Required, ArgSettings::MultipleValues, ArgSettings::MultipleOccurrences]))) .settings(&[ArgSettings::Required, ArgSettings::MultipleValues, ArgSettings::MultipleOccurrences])))
.subcommand(SubCommand::with_name("which") .subcommand(App::new("which")
.about("Display which binary will be run for a given command") .about("Display which binary will be run for a given command")
.arg(Arg::with_name("command").setting(ArgSettings::Required))) .arg(Arg::with_name("command").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("doc") .subcommand(App::new("doc")
.about("Open the documentation for the current toolchain") .about("Open the documentation for the current toolchain")
.after_help(DOC_HELP) .after_help(DOC_HELP)
.arg(Arg::with_name("book") .arg(Arg::with_name("book")
@ -200,37 +200,37 @@ pub fn build_cli() -> App<'static, 'static> {
.long("std") .long("std")
.help("Standard library API documentation")) .help("Standard library API documentation"))
.group(ArgGroup::with_name("page").args(&["book", "std"]))) .group(ArgGroup::with_name("page").args(&["book", "std"])))
.subcommand(SubCommand::with_name("man") .subcommand(App::new("man")
.about("View the man page for a given command") .about("View the man page for a given command")
.arg(Arg::with_name("command").setting(ArgSettings::Required)) .arg(Arg::with_name("command").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain") .arg(Arg::with_name("toolchain")
.long("toolchain") .long("toolchain")
.setting(ArgSettings::TakesValue))) .setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("self") .subcommand(App::new("self")
.about("Modify the rustup installation") .about("Modify the rustup installation")
.setting(AppSettings::VersionlessSubcommands) .setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::DeriveDisplayOrder)
.subcommand(SubCommand::with_name("update") .subcommand(App::new("update")
.about("Download and install updates to rustup")) .about("Download and install updates to rustup"))
.subcommand(SubCommand::with_name("uninstall") .subcommand(App::new("uninstall")
.about("Uninstall rustup.") .about("Uninstall rustup.")
.arg(Arg::with_name("no-prompt").short('y'))) .arg(Arg::with_name("no-prompt").short('y')))
.subcommand(SubCommand::with_name("upgrade-data") .subcommand(App::new("upgrade-data")
.about("Upgrade the internal data format."))) .about("Upgrade the internal data format.")))
.subcommand(SubCommand::with_name("telemetry") .subcommand(App::new("telemetry")
.about("rustup telemetry commands") .about("rustup telemetry commands")
.setting(AppSettings::Hidden) .setting(AppSettings::Hidden)
.setting(AppSettings::VersionlessSubcommands) .setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder) .setting(AppSettings::DeriveDisplayOrder)
.subcommand(SubCommand::with_name("enable").about("Enable rustup telemetry")) .subcommand(App::new("enable").about("Enable rustup telemetry"))
.subcommand(SubCommand::with_name("disable").about("Disable rustup telemetry")) .subcommand(App::new("disable").about("Disable rustup telemetry"))
.subcommand(SubCommand::with_name("analyze").about("Analyze stored telemetry"))) .subcommand(App::new("analyze").about("Analyze stored telemetry")))
.subcommand(SubCommand::with_name("set") .subcommand(App::new("set")
.about("Alter rustup settings") .about("Alter rustup settings")
.subcommand(SubCommand::with_name("default-host") .subcommand(App::new("default-host")
.about("The triple used to identify toolchains when not specified") .about("The triple used to identify toolchains when not specified")
.arg(Arg::with_name("host_triple").setting(ArgSettings::Required)))) .arg(Arg::with_name("host_triple").setting(ArgSettings::Required))))
.subcommand(SubCommand::with_name("completions") .subcommand(App::new("completions")
.about("Generate completion scripts for your shell") .about("Generate completion scripts for your shell")
.after_help(COMPLETIONS_HELP) .after_help(COMPLETIONS_HELP)
.setting(AppSettings::ArgRequiredElseHelp) .setting(AppSettings::ArgRequiredElseHelp)

View file

@ -5,7 +5,7 @@ mod test {
use regex::Regex; use regex::Regex;
use clap::{App, Arg, SubCommand, ArgGroup}; use clap::{App, Arg, ArgGroup};
fn compare<S, S2>(l: S, r: S2) -> bool fn compare<S, S2>(l: S, r: S2) -> bool
where S: AsRef<str>, where S: AsRef<str>,
@ -31,7 +31,7 @@ mod test {
pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool { pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
let mut buf = Cursor::new(Vec::with_capacity(50)); let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>()); let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err(); let err = res.unwrap_err();
err.write_to(&mut buf).unwrap(); err.write_to(&mut buf).unwrap();
let content = buf.into_inner(); let content = buf.into_inner();
@ -43,7 +43,7 @@ mod test {
pub fn compare_output2(l: App, args: &str, right1: &str, right2: &str, stderr: bool) -> bool { pub fn compare_output2(l: App, args: &str, right1: &str, right2: &str, stderr: bool) -> bool {
let mut buf = Cursor::new(Vec::with_capacity(50)); let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>()); let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err(); let err = res.unwrap_err();
err.write_to(&mut buf).unwrap(); err.write_to(&mut buf).unwrap();
let content = buf.into_inner(); let content = buf.into_inner();
@ -55,15 +55,14 @@ mod test {
// Legacy tests from the pyhton script days // Legacy tests from the pyhton script days
pub fn complex_app() -> App<'static, 'static> { pub fn complex_app() -> App<'static, 'static> {
let args = "-o --option=[opt]... 'tests options'
[positional] 'tests positionals'";
let opt3_vals = ["fast", "slow"]; let opt3_vals = ["fast", "slow"];
let pos3_vals = ["vi", "emacs"]; let pos3_vals = ["vi", "emacs"];
App::new("clap-test") App::new("clap-test")
.version("v1.4.8") .version("v1.4.8")
.about("tests clap library") .about("tests clap library")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.args_from_usage(args) .arg("-o --option=[opt]... 'tests options'")
.arg("[positional] 'tests positionals'")
.arg(Arg::from("-f --flag... 'tests flags'") .arg(Arg::from("-f --flag... 'tests flags'")
.global(true)) .global(true))
.args(&[ .args(&[
@ -77,12 +76,12 @@ mod test {
Arg::from("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2), Arg::from("--minvals2 [minvals]... 'Tests 2 min vals'").min_values(2),
Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3) Arg::from("--maxvals3 [maxvals]... 'Tests 3 max vals'").max_values(3)
]) ])
.subcommand(SubCommand::with_name("subcmd") .subcommand(App::new("subcmd")
.about("tests subcommands") .about("tests subcommands")
.version("0.1") .version("0.1")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.arg_from_usage("-o --option [scoption]... 'tests options'") .arg("-o --option [scoption]... 'tests options'")
.arg_from_usage("-s --subcmdarg [subcmdarg] 'tests other args'") .arg("-s --subcmdarg [subcmdarg] 'tests other args'")
.arg_from_usage("[scpositional] 'tests positionals'")) .arg("[scpositional] 'tests positionals'"))
} }
} }

View file

@ -33,7 +33,7 @@ where
pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool { pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
let mut buf = Cursor::new(Vec::with_capacity(50)); let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>()); let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err(); let err = res.unwrap_err();
err.write_to(&mut buf).unwrap(); err.write_to(&mut buf).unwrap();
let content = buf.into_inner(); let content = buf.into_inner();
@ -50,7 +50,7 @@ pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
pub fn compare_output2(l: App, args: &str, right1: &str, right2: &str, stderr: bool) -> bool { pub fn compare_output2(l: App, args: &str, right1: &str, right2: &str, stderr: bool) -> bool {
let mut buf = Cursor::new(Vec::with_capacity(50)); let mut buf = Cursor::new(Vec::with_capacity(50));
let res = l.get_matches_from_safe(args.split(' ').collect::<Vec<_>>()); let res = l.try_get_matches_from(args.split(' ').collect::<Vec<_>>());
let err = res.unwrap_err(); let err = res.unwrap_err();
err.write_to(&mut buf).unwrap(); err.write_to(&mut buf).unwrap();
let content = buf.into_inner(); let content = buf.into_inner();

View file

@ -1,6 +1,6 @@
extern crate clap; extern crate clap;
use clap::{App, SubCommand}; use clap::{App, };
fn main() { fn main() {
// This example shows how to create an application with several arguments using usage strings, which can be // This example shows how to create an application with several arguments using usage strings, which can be
@ -35,15 +35,13 @@ fn main() {
.version("1.0") .version("1.0")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things") .about("Does awesome things")
.args_from_usage( .arg("-c, --config=[FILE] 'Sets a custom config file'")
"-c, --config=[FILE] 'Sets a custom config file' .arg("<output> 'Sets an optional output file'")
<output> 'Sets an optional output file' .arg("-d... 'Turn debugging information on'")
-d... 'Turn debugging information on'",
)
.subcommand( .subcommand(
SubCommand::with_name("test") App::new("test")
.about("does testing things") .about("does testing things")
.arg_from_usage("-l, --list 'lists test values'"), .arg("-l, --list 'lists test values'"),
) )
.get_matches(); .get_matches();

View file

@ -1,6 +1,6 @@
extern crate clap; extern crate clap;
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, };
fn main() { fn main() {
// This method shows the traditional, and slightly more configurable way to set up arguments. This method is // This method shows the traditional, and slightly more configurable way to set up arguments. This method is
@ -57,7 +57,7 @@ fn main() {
.help("Turn debugging information on"), .help("Turn debugging information on"),
) )
.subcommand( .subcommand(
SubCommand::with_name("test") App::new("test")
.about("does testing things") .about("does testing things")
.arg(Arg::with_name("list").short('l').help("lists test values")), .arg(Arg::with_name("list").short('l').help("lists test values")),
) )

View file

@ -11,7 +11,7 @@ fn main() {
// another option, usage(), which is an exception to the rule. This should only be used when // another option, usage(), which is an exception to the rule. This should only be used when
// the default usage string automatically generated by clap doesn't suffice. // the default usage string automatically generated by clap doesn't suffice.
// //
// You also set all the valid arguments your App should accept via the arg(), args(), arg_from_usage() // You also set all the valid arguments your App should accept via the arg(), args(), arg()
// and args_from_usage() (as well as subcommands via the subcommand() and subcommands() methods) which // and args_from_usage() (as well as subcommands via the subcommand() and subcommands() methods) which
// will be covered later. // will be covered later.
// //

View file

@ -5,14 +5,14 @@ use clap::{App, Arg};
fn main() { fn main() {
// Args describe a possible valid argument which may be supplied by the user at runtime. There // Args describe a possible valid argument which may be supplied by the user at runtime. There
// are three different types of arguments (flags, options, and positional) as well as a fourth // are three different types of arguments (flags, options, and positional) as well as a fourth
// special type of argument, called SubCommands (which will be discussed separately). // special type of argument, called s (which will be discussed separately).
// //
// Args are described in the same manner as Apps using the "builder pattern" with multiple // Args are described in the same manner as Apps using the "builder pattern" with multiple
// methods describing various settings for the individual arguments. Or by supplying a "usage" // methods describing various settings for the individual arguments. Or by supplying a "usage"
// string. Both methods have their pros and cons. // string. Both methods have their pros and cons.
// //
// Arguments can be added to applications in two manners, one at a time with the arg(), and // Arguments can be added to applications in two manners, one at a time with the arg(), and
// arg_from_usage() method, or multiple arguments at once via a Vec<Arg> inside the args() method, // arg() method, or multiple arguments at once via a Vec<Arg> inside the args() method,
// or a single &str describing multiple Args (one per line) supplied to args_from_usage(). // or a single &str describing multiple Args (one per line) supplied to args_from_usage().
// //
// There are various options which can be set for a given argument, some apply to any of the // There are various options which can be set for a given argument, some apply to any of the
@ -49,16 +49,16 @@ fn main() {
// *Note* the following two examples are convenience methods, if you wish // *Note* the following two examples are convenience methods, if you wish
// to still get the full configurability of Arg::with_name() and the readability // to still get the full configurability of Arg::with_name() and the readability
// of arg_from_usage(), you can instantiate a new Arg with Arg::from() and // of arg(), you can instantiate a new Arg with Arg::from() and
// still be able to set all the additional properties, just like Arg::with_name() // still be able to set all the additional properties, just like Arg::with_name()
// //
// //
// One "Flag" using a usage string // One "Flag" using a usage string
.arg_from_usage("--license 'display the license file'") .arg("--license 'display the license file'")
// Two args, one "Positional", and one "Option" using a usage string // Two args, one "Positional", and one "Option" using a usage string
.args_from_usage("[output] 'Supply an output file to use' .arg("[output] 'Supply an output file to use'")
-i, --int=[IFACE] 'Set an interface to use'") .arg("-i, --int=[IFACE] 'Set an interface to use'")
.get_matches(); .get_matches();
// Here are some examples of using the arguments defined above. Keep in mind that this is only // Here are some examples of using the arguments defined above. Keep in mind that this is only

View file

@ -1,10 +1,10 @@
extern crate clap; extern crate clap;
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, };
fn main() { fn main() {
// SubCommands function exactly like sub-Apps, because that's exactly what they are. Each // s function exactly like sub-Apps, because that's exactly what they are. Each
// instance of a SubCommand can have it's own version, author(s), Args, and even it's own // instance of a can have it's own version, author(s), Args, and even it's own
// subcommands. // subcommands.
// //
// # Help and Version // # Help and Version
@ -16,14 +16,14 @@ fn main() {
// subcommand along with "-h" and "--help" (applies to sub-subcommands as well). // subcommand along with "-h" and "--help" (applies to sub-subcommands as well).
// //
// Just like arg() and args(), subcommands can be specified one at a time via subcommand() or // Just like arg() and args(), subcommands can be specified one at a time via subcommand() or
// multiple ones at once with a Vec<SubCommand> provided to subcommands(). // multiple ones at once with a Vec<> provided to subcommands().
let matches = App::new("MyApp") let matches = App::new("MyApp")
// Normal App and Arg configuration goes here... // Normal App and Arg configuration goes here...
// In the following example assume we wanted an application which // In the following example assume we wanted an application which
// supported an "add" subcommand, this "add" subcommand also took // supported an "add" subcommand, this "add" subcommand also took
// one positional argument of a file to add: // one positional argument of a file to add:
.subcommand(SubCommand::with_name("add") // The name we call argument with .subcommand(App::new("add") // The name we call argument with
.about("Adds files to myapp") // The message displayed in "myapp -h" .about("Adds files to myapp") // The message displayed in "myapp -h"
// or "myapp help" // or "myapp help"
.version("0.1") // Subcommands can have independent version .version("0.1") // Subcommands can have independent version

View file

@ -28,9 +28,8 @@ fn main() {
let matches = App::new("myapp") let matches = App::new("myapp")
// Create two arguments, a required positional which accepts multiple values // Create two arguments, a required positional which accepts multiple values
// and an optional '-l value' // and an optional '-l value'
.args_from_usage( .arg("<seq>... 'A sequence of whole positive numbers, i.e. 20 25 30'")
"<seq>... 'A sequence of whole positive numbers, i.e. 20 25 30' .arg("-l [len] 'A length to use, defaults to 10 when omitted'")
-l [len] 'A length to use, defaults to 10 when omitted'")
.get_matches(); .get_matches();
// Here we get a value of type u32 from our optional -l argument. // Here we get a value of type u32 from our optional -l argument.

View file

@ -49,7 +49,7 @@ fn main() {
// not, the valid values will ALWAYS be displayed on a failed parse. // not, the valid values will ALWAYS be displayed on a failed parse.
.possible_values(&enum_vals)) .possible_values(&enum_vals))
// For the second positional, lets not use possible_values() just to show the difference // For the second positional, lets not use possible_values() just to show the difference
.arg_from_usage("<oof> 'The Oof to use'") .arg("<oof> 'The Oof to use'")
.get_matches(); .get_matches();
let t = value_t!(m.value_of("foo"), Foo).unwrap_or_else(|e| e.exit()); let t = value_t!(m.value_of("foo"), Foo).unwrap_or_else(|e| e.exit());

View file

@ -28,10 +28,10 @@ fn main() {
// Create application like normal // Create application like normal
let matches = App::new("myapp") let matches = App::new("myapp")
// Add the version arguments // Add the version arguments
.args_from_usage("--set-ver [ver] 'set version manually' .arg("--set-ver [ver] 'set version manually'")
--major 'auto inc major' .arg("--major 'auto inc major'")
--minor 'auto inc minor' .arg("--minor 'auto inc minor'")
--patch 'auto inc patch'") .arg("--patch 'auto inc patch'")
// Create a group, make it required, and add the above arguments // Create a group, make it required, and add the above arguments
.group(ArgGroup::with_name("vers") .group(ArgGroup::with_name("vers")
.required(true) .required(true)

View file

@ -1,6 +1,6 @@
extern crate clap; extern crate clap;
use clap::{App, AppSettings, SubCommand}; use clap::{App, AppSettings, };
fn main() { fn main() {
// You can use AppSettings to change the application level behavior of clap. .setting() function // You can use AppSettings to change the application level behavior of clap. .setting() function
@ -15,11 +15,11 @@ fn main() {
.setting(AppSettings::SubcommandsNegateReqs) .setting(AppSettings::SubcommandsNegateReqs)
// Negates requirement of parent command. // Negates requirement of parent command.
.arg_from_usage("<input> 'input file to use'") .arg("<input> 'input file to use'")
// Required positional argument called input. This // Required positional argument called input. This
// will be only required if subcommand is not present. // will be only required if subcommand is not present.
.subcommand(SubCommand::with_name("test") .subcommand(App::new("test")
.about("does some testing")) .about("does some testing"))
// if program is invoked with subcommand, you do not // if program is invoked with subcommand, you do not
// need to specify the <input> argument anymore due to // need to specify the <input> argument anymore due to

View file

@ -15,7 +15,7 @@ fn main() {
}; };
// External module may contain this subcommand. If this exists in another module, a function is // External module may contain this subcommand. If this exists in another module, a function is
// required to access it. Recommend `fn clap() -> Clap::SubCommand`. // required to access it. Recommend `fn clap() -> Clap::`.
let external_sub_command = clap_app!( @subcommand foo => let external_sub_command = clap_app!( @subcommand foo =>
(@arg bar: -b "Bar") (@arg bar: -b "Bar")
); );

View file

@ -1,5 +1,5 @@
// Working with subcommands is simple. There are a few key points to remember when working with // Working with subcommands is simple. There are a few key points to remember when working with
// subcommands in clap. First, SubCommands are really just Apps. This means they can have their own // subcommands in clap. First, s are really just Apps. This means they can have their own
// settings, version, authors, args, and even their own subcommands. The next thing to remember is // settings, version, authors, args, and even their own subcommands. The next thing to remember is
// that subcommands are set up in a tree like heirachy. // that subcommands are set up in a tree like heirachy.
// //
@ -41,7 +41,7 @@
extern crate clap; extern crate clap;
use clap::{App, AppSettings, Arg, SubCommand}; use clap::{App, AppSettings, Arg, };
fn main() { fn main() {
let matches = App::new("git") let matches = App::new("git")
@ -49,28 +49,28 @@ fn main() {
.version("1.0") .version("1.0")
.author("Me") .author("Me")
.subcommand( .subcommand(
SubCommand::with_name("clone").about("clones repos").arg( App::new("clone").about("clones repos").arg(
Arg::with_name("repo") Arg::with_name("repo")
.help("The repo to clone") .help("The repo to clone")
.required(true), .required(true),
), ),
) )
.subcommand( .subcommand(
SubCommand::with_name("push") App::new("push")
.about("pushes things") .about("pushes things")
.setting(AppSettings::SubcommandRequiredElseHelp) .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand( .subcommand(
SubCommand::with_name("remote") // Subcommands can have thier own subcommands, App::new("remote") // Subcommands can have thier own subcommands,
// which in turn have their own subcommands // which in turn have their own subcommands
.about("pushes remote things") .about("pushes remote things")
.arg(Arg::with_name("repo") .arg(Arg::with_name("repo")
.required(true) .required(true)
.help("The remote repo to push things to")), .help("The remote repo to push things to")),
) )
.subcommand(SubCommand::with_name("local").about("pushes local things")), .subcommand(App::new("local").about("pushes local things")),
) )
.subcommand( .subcommand(
SubCommand::with_name("add") App::new("add")
.about("adds things") .about("adds things")
.author("Someone Else") // Subcommands can list different authors .author("Someone Else") // Subcommands can list different authors
.version("v2.0 (I'm versioned differently") // or different version from their parents .version("v2.0 (I'm versioned differently") // or different version from their parents

View file

@ -1,11 +1,11 @@
extern crate clap; extern crate clap;
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, };
fn main() { fn main() {
let matches = App::new("MyApp") let matches = App::new("MyApp")
.subcommand( .subcommand(
SubCommand::with_name("ls") App::new("ls")
.aliases(&["list", "dir"]) .aliases(&["list", "dir"])
.about("Adds files to myapp") .about("Adds files to myapp")
.version("0.1") .version("0.1")

View file

@ -17,7 +17,6 @@ use yaml_rust::Yaml;
// Internal // Internal
use build::{Arg, ArgGroup, ArgSettings}; use build::{Arg, ArgGroup, ArgSettings};
use completions::{ComplGen, Shell};
use output::fmt::ColorWhen; use output::fmt::ColorWhen;
use output::{Help, Usage}; use output::{Help, Usage};
use parse::errors::Result as ClapResult; use parse::errors::Result as ClapResult;
@ -112,14 +111,6 @@ where
#[doc(hidden)] #[doc(hidden)]
pub groups: Vec<ArgGroup<'a>>, pub groups: Vec<ArgGroup<'a>>,
#[doc(hidden)] #[doc(hidden)]
pub help_short: Option<char>,
#[doc(hidden)]
pub version_short: Option<char>,
#[doc(hidden)]
pub help_message: Option<&'a str>,
#[doc(hidden)]
pub version_message: Option<&'a str>,
#[doc(hidden)]
pub help_headings: Vec<Option<&'a str>>, pub help_headings: Vec<Option<&'a str>>,
} }
@ -180,7 +171,7 @@ impl<'a, 'b> App<'a, 'b> {
/// **Pro-tip:** When building things such as third party `cargo` subcommands, this setting /// **Pro-tip:** When building things such as third party `cargo` subcommands, this setting
/// **should** be used! /// **should** be used!
/// ///
/// **NOTE:** This command **should not** be used for [`SubCommand`]s. /// **NOTE:** This command **should not** be used for [``]s.
/// ///
/// # Examples /// # Examples
/// ///
@ -190,7 +181,7 @@ impl<'a, 'b> App<'a, 'b> {
/// .bin_name("my_binary") /// .bin_name("my_binary")
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
pub fn bin_name<S: Into<String>>(mut self, name: S) -> Self { pub fn bin_name<S: Into<String>>(mut self, name: S) -> Self {
self.bin_name = Some(name.into()); self.bin_name = Some(name.into());
self self
@ -469,7 +460,7 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Enables a single command, or [`SubCommand`], level settings. /// Enables a single command, or [``], level settings.
/// ///
/// See [`AppSettings`] for a full list of possibilities and examples. /// See [`AppSettings`] for a full list of possibilities and examples.
/// ///
@ -482,14 +473,14 @@ impl<'a, 'b> App<'a, 'b> {
/// .setting(AppSettings::WaitOnError) /// .setting(AppSettings::WaitOnError)
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`AppSettings`]: ./enum.AppSettings.html /// [`AppSettings`]: ./enum.AppSettings.html
pub fn setting(mut self, setting: AppSettings) -> Self { pub fn setting(mut self, setting: AppSettings) -> Self {
self.settings.set(setting); self.settings.set(setting);
self self
} }
/// Disables a single command, or [`SubCommand`], level setting. /// Disables a single command, or [``], level setting.
/// ///
/// See [`AppSettings`] for a full list of possibilities and examples. /// See [`AppSettings`] for a full list of possibilities and examples.
/// ///
@ -501,11 +492,12 @@ impl<'a, 'b> App<'a, 'b> {
/// .unset_setting(AppSettings::ColorAuto) /// .unset_setting(AppSettings::ColorAuto)
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`AppSettings`]: ./enum.AppSettings.html /// [`AppSettings`]: ./enum.AppSettings.html
/// [global]: ./struct.App.html#method.global_setting /// [global]: ./struct.App.html#method.global_setting
pub fn unset_setting(mut self, setting: AppSettings) -> Self { pub fn unset_setting(mut self, setting: AppSettings) -> Self {
self.settings.unset(setting); self.settings.unset(setting);
self.g_settings.unset(setting);
self self
} }
@ -683,7 +675,7 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Allows adding a [`SubCommand`] alias, which function as "hidden" subcommands that /// Allows adding a [``] alias, which function as "hidden" subcommands that
/// automatically dispatch as if this subcommand was used. This is more efficient, and easier /// automatically dispatch as if this subcommand was used. This is more efficient, and easier
/// than creating multiple hidden subcommands as one only needs to check for the existence of /// than creating multiple hidden subcommands as one only needs to check for the existence of
/// this command, and not all variants. /// this command, and not all variants.
@ -691,14 +683,14 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test") /// .subcommand(App::new("test")
/// .alias("do-stuff")) /// .alias("do-stuff"))
/// .get_matches_from(vec!["myprog", "do-stuff"]); /// .get_matches_from(vec!["myprog", "do-stuff"]);
/// assert_eq!(m.subcommand_name(), Some("test")); /// assert_eq!(m.subcommand_name(), Some("test"));
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
pub fn alias<S: Into<&'b str>>(mut self, name: S) -> Self { pub fn alias<S: Into<&'b str>>(mut self, name: S) -> Self {
if let Some(ref mut als) = self.aliases { if let Some(ref mut als) = self.aliases {
als.push((name.into(), false)); als.push((name.into(), false));
@ -708,7 +700,7 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Allows adding [`SubCommand`] aliases, which function as "hidden" subcommands that /// Allows adding [``] aliases, which function as "hidden" subcommands that
/// automatically dispatch as if this subcommand was used. This is more efficient, and easier /// automatically dispatch as if this subcommand was used. This is more efficient, and easier
/// than creating multiple hidden subcommands as one only needs to check for the existence of /// than creating multiple hidden subcommands as one only needs to check for the existence of
/// this command, and not all variants. /// this command, and not all variants.
@ -716,9 +708,9 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test") /// .subcommand(App::new("test")
/// .aliases(&["do-stuff", "do-tests", "tests"])) /// .aliases(&["do-stuff", "do-tests", "tests"]))
/// .arg(Arg::with_name("input") /// .arg(Arg::with_name("input")
/// .help("the file to add") /// .help("the file to add")
@ -727,7 +719,7 @@ impl<'a, 'b> App<'a, 'b> {
/// .get_matches_from(vec!["myprog", "do-tests"]); /// .get_matches_from(vec!["myprog", "do-tests"]);
/// assert_eq!(m.subcommand_name(), Some("test")); /// assert_eq!(m.subcommand_name(), Some("test"));
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
pub fn aliases(mut self, names: &[&'b str]) -> Self { pub fn aliases(mut self, names: &[&'b str]) -> Self {
if let Some(ref mut als) = self.aliases { if let Some(ref mut als) = self.aliases {
for n in names { for n in names {
@ -739,20 +731,20 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Allows adding a [`SubCommand`] alias that functions exactly like those defined with /// Allows adding a [``] alias that functions exactly like those defined with
/// [`App::alias`], except that they are visible inside the help message. /// [`App::alias`], except that they are visible inside the help message.
/// ///
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test") /// .subcommand(App::new("test")
/// .visible_alias("do-stuff")) /// .visible_alias("do-stuff"))
/// .get_matches_from(vec!["myprog", "do-stuff"]); /// .get_matches_from(vec!["myprog", "do-stuff"]);
/// assert_eq!(m.subcommand_name(), Some("test")); /// assert_eq!(m.subcommand_name(), Some("test"));
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`App::alias`]: ./struct.App.html#method.alias /// [`App::alias`]: ./struct.App.html#method.alias
pub fn visible_alias<S: Into<&'b str>>(mut self, name: S) -> Self { pub fn visible_alias<S: Into<&'b str>>(mut self, name: S) -> Self {
if let Some(ref mut als) = self.aliases { if let Some(ref mut als) = self.aliases {
@ -763,20 +755,20 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Allows adding multiple [`SubCommand`] aliases that functions exactly like those defined /// Allows adding multiple [``] aliases that functions exactly like those defined
/// with [`App::aliases`], except that they are visible inside the help message. /// with [`App::aliases`], except that they are visible inside the help message.
/// ///
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test") /// .subcommand(App::new("test")
/// .visible_aliases(&["do-stuff", "tests"])) /// .visible_aliases(&["do-stuff", "tests"]))
/// .get_matches_from(vec!["myprog", "do-stuff"]); /// .get_matches_from(vec!["myprog", "do-stuff"]);
/// assert_eq!(m.subcommand_name(), Some("test")); /// assert_eq!(m.subcommand_name(), Some("test"));
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`App::aliases`]: ./struct.App.html#method.aliases /// [`App::aliases`]: ./struct.App.html#method.aliases
pub fn visible_aliases(mut self, names: &[&'b str]) -> Self { pub fn visible_aliases(mut self, names: &[&'b str]) -> Self {
if let Some(ref mut als) = self.aliases { if let Some(ref mut als) = self.aliases {
@ -812,11 +804,10 @@ impl<'a, 'b> App<'a, 'b> {
/// ```no_run /// ```no_run
/// # use clap::{App, ArgGroup}; /// # use clap::{App, ArgGroup};
/// App::new("app") /// App::new("app")
/// .args_from_usage( /// .arg("--set-ver [ver] 'set the version manually'")
/// "--set-ver [ver] 'set the version manually' /// .arg("--major 'auto increase major'")
/// --major 'auto increase major' /// .arg("--minor 'auto increase minor'")
/// --minor 'auto increase minor' /// .arg("--patch 'auto increase patch'")
/// --patch 'auto increase patch'")
/// .group(ArgGroup::with_name("vers") /// .group(ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"]) /// .args(&["set-ver", "major", "minor","patch"])
/// .required(true)) /// .required(true))
@ -835,13 +826,12 @@ impl<'a, 'b> App<'a, 'b> {
/// ```no_run /// ```no_run
/// # use clap::{App, ArgGroup}; /// # use clap::{App, ArgGroup};
/// App::new("app") /// App::new("app")
/// .args_from_usage( /// .arg("--set-ver [ver] 'set the version manually'")
/// "--set-ver [ver] 'set the version manually' /// .arg("--major 'auto increase major'")
/// --major 'auto increase major' /// .arg("--minor 'auto increase minor'")
/// --minor 'auto increase minor' /// .arg("--patch 'auto increase patch'")
/// --patch 'auto increase patch' /// .arg("-c [FILE] 'a config file'")
/// -c [FILE] 'a config file' /// .arg("-i [IFACE] 'an interface'")
/// -i [IFACE] 'an interface'")
/// .groups(&[ /// .groups(&[
/// ArgGroup::with_name("vers") /// ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"]) /// .args(&["set-ver", "major", "minor","patch"])
@ -860,7 +850,7 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Adds a [`SubCommand`] to the list of valid possibilities. Subcommands are effectively /// Adds a [``] to the list of valid possibilities. Subcommands are effectively
/// sub-[`App`]s, because they can contain their own arguments, subcommands, version, usage, /// sub-[`App`]s, because they can contain their own arguments, subcommands, version, usage,
/// etc. They also function just like [`App`]s, in that they get their own auto generated help, /// etc. They also function just like [`App`]s, in that they get their own auto generated help,
/// version, and usage. /// version, and usage.
@ -868,14 +858,14 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// App::new("myprog") /// App::new("myprog")
/// .subcommand(SubCommand::with_name("config") /// .subcommand(App::new("config")
/// .about("Controls configuration features") /// .about("Controls configuration features")
/// .arg_from_usage("<config> 'Required configuration file to use'")) /// .arg("<config> 'Required configuration file to use'"))
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`App`]: ./struct.App.html /// [`App`]: ./struct.App.html
pub fn subcommand(mut self, subcmd: App<'a, 'b>) -> Self { pub fn subcommand(mut self, subcmd: App<'a, 'b>) -> Self {
self.subcommands.push(subcmd); self.subcommands.push(subcmd);
@ -883,20 +873,20 @@ impl<'a, 'b> App<'a, 'b> {
} }
/// Adds multiple subcommands to the list of valid possibilities by iterating over an /// Adds multiple subcommands to the list of valid possibilities by iterating over an
/// [`IntoIterator`] of [`SubCommand`]s /// [`IntoIterator`] of [``]s
/// ///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// # App::new("myprog") /// # App::new("myprog")
/// .subcommands( vec![ /// .subcommands( vec![
/// SubCommand::with_name("config").about("Controls configuration functionality") /// App::new("config").about("Controls configuration functionality")
/// .arg(Arg::with_name("config_file").index(1)), /// .arg(Arg::with_name("config_file").index(1)),
/// SubCommand::with_name("debug").about("Controls debug functionality")]) /// App::new("debug").about("Controls debug functionality")])
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html /// [`IntoIterator`]: https://doc.rust-lang.org/std/iter/trait.IntoIterator.html
pub fn subcommands<I>(mut self, subcmds: I) -> Self pub fn subcommands<I>(mut self, subcmds: I) -> Self
where where
@ -908,7 +898,7 @@ impl<'a, 'b> App<'a, 'b> {
self self
} }
/// Allows custom ordering of [`SubCommand`]s within the help message. Subcommands with a lower /// Allows custom ordering of [``]s within the help message. Subcommands with a lower
/// value will be displayed first in the help message. This is helpful when one would like to /// value will be displayed first in the help message. This is helpful when one would like to
/// emphasise frequently used subcommands, or prioritize those towards the top of the list. /// emphasise frequently used subcommands, or prioritize those towards the top of the list.
/// Duplicate values **are** allowed. Subcommands with duplicate display orders will be /// Duplicate values **are** allowed. Subcommands with duplicate display orders will be
@ -919,15 +909,15 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, SubCommand}; /// # use clap::{App, };
/// let m = App::new("cust-ord") /// let m = App::new("cust-ord")
/// .subcommand(SubCommand::with_name("alpha") // typically subcommands are grouped /// .subcommand(App::new("alpha") // typically subcommands are grouped
/// // alphabetically by name. Subcommands /// // alphabetically by name. Subcommands
/// // without a display_order have a value of /// // without a display_order have a value of
/// // 999 and are displayed alphabetically with /// // 999 and are displayed alphabetically with
/// // all other 999 subcommands /// // all other 999 subcommands
/// .about("Some help and text")) /// .about("Some help and text"))
/// .subcommand(SubCommand::with_name("beta") /// .subcommand(App::new("beta")
/// .display_order(1) // In order to force this subcommand to appear *first* /// .display_order(1) // In order to force this subcommand to appear *first*
/// // all we have to do is give it a value lower than 999. /// // all we have to do is give it a value lower than 999.
/// // Any other subcommands with a value of 1 will be displayed /// // Any other subcommands with a value of 1 will be displayed
@ -954,7 +944,7 @@ impl<'a, 'b> App<'a, 'b> {
/// beta I should be first! /// beta I should be first!
/// alpha Some help and text /// alpha Some help and text
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
pub fn display_order(mut self, ord: usize) -> Self { pub fn display_order(mut self, ord: usize) -> Self {
self.disp_ord = ord; self.disp_ord = ord;
self self
@ -1313,7 +1303,7 @@ impl<'a, 'b> App<'a, 'b> {
/// .unwrap_or_else( |e| { panic!("An error occurs: {}", e) }); /// .unwrap_or_else( |e| { panic!("An error occurs: {}", e) });
/// ``` /// ```
/// [`App::get_matches_from`]: ./struct.App.html#method.get_matches_from /// [`App::get_matches_from`]: ./struct.App.html#method.get_matches_from
/// [`App::try_get_matches`]: ./struct.App.html#method.get_matches_safe /// [`App::try_get_matches`]: ./struct.App.html#method.try_get_matches
/// [`ErrorKind::HelpDisplayed`]: ./enum.ErrorKind.html#variant.HelpDisplayed /// [`ErrorKind::HelpDisplayed`]: ./enum.ErrorKind.html#variant.HelpDisplayed
/// [`ErrorKind::VersionDisplayed`]: ./enum.ErrorKind.html#variant.VersionDisplayed /// [`ErrorKind::VersionDisplayed`]: ./enum.ErrorKind.html#variant.VersionDisplayed
/// [`Error::exit`]: ./struct.Error.html#method.exit /// [`Error::exit`]: ./struct.Error.html#method.exit
@ -1528,36 +1518,27 @@ impl<'a, 'b> App<'a, 'b> {
pub(crate) fn _create_help_and_version(&mut self) { pub(crate) fn _create_help_and_version(&mut self) {
debugln!("App::_create_help_and_version;"); debugln!("App::_create_help_and_version;");
// name is "hclap_help" because flags are sorted by name if !(self.args.iter().any(|x| x.long == Some("help")) || self.args.iter().any(|x| x.name == "help")){
if !self.contains_long("help") {
debugln!("App::_create_help_and_version: Building --help"); debugln!("App::_create_help_and_version: Building --help");
if self.help_short.is_none() && !self.contains_short('h') { let mut help = Arg::with_name("help")
self.help_short = Some('h');
}
let mut arg = Arg::with_name("hclap_help")
.long("help") .long("help")
.help(self.help_message.unwrap_or("Prints help information")); .help("Prints help information");
if !self.args.iter().any(|x| x.short == Some('h')) {
// we have to set short manually because we're dealing with char's help = help.short('h');
arg.short = self.help_short;
self.args.push(arg);
} else {
self.settings.unset(AppSettings::NeedsLongHelp);
}
if !self.is_set(AppSettings::DisableVersion) && !self.contains_long("version") {
debugln!("App::_create_help_and_version: Building --version");
if self.version_short.is_none() && !self.contains_short('V') {
self.version_short = Some('V');
} }
// name is "vclap_version" because flags are sorted by name
let mut arg = Arg::with_name("vclap_version") self.args.push(help);
}
if !(self.args.iter().any(|x| x.long == Some("version")) || self.args.iter().any(|x| x.name == "version") || self.is_set(AppSettings::DisableVersion)){
debugln!("App::_create_help_and_version: Building --version");
let mut version = Arg::with_name("version")
.long("version") .long("version")
.help(self.version_message.unwrap_or("Prints version information")); .help("Prints version information");
// we have to set short manually because we're dealing with char's if !self.args.iter().any(|x| x.short == Some('V')) {
arg.short = self.version_short; version = version.short('V');
self.args.push(arg); }
} else {
self.settings.unset(AppSettings::NeedsLongVersion); self.args.push(version);
} }
if self.has_subcommands() if self.has_subcommands()
&& !self.is_set(AppSettings::DisableHelpSubcommand) && !self.is_set(AppSettings::DisableHelpSubcommand)
@ -1568,8 +1549,6 @@ impl<'a, 'b> App<'a, 'b> {
App::new("help") App::new("help")
.about("Prints this message or the help of the given subcommand(s)"), .about("Prints this message or the help of the given subcommand(s)"),
); );
} else {
self.settings.unset(AppSettings::NeedsSubcommandHelp);
} }
} }
@ -1870,271 +1849,9 @@ impl<'a, 'b> App<'a, 'b> {
} }
} }
// @TODO @v3-beta: remove
// Deprecations
impl<'a, 'b> App<'a, 'b> {
/// **Deprecated:** Use `App::global_setting( SettingOne | SettingTwo )` instead
#[deprecated(
since = "2.33.0",
note = "Use `App::global_setting( SettingOne | SettingTwo )` instead"
)]
pub fn global_settings(mut self, settings: &[AppSettings]) -> Self {
for s in settings {
self.settings.set(*s);
self.g_settings.set(*s)
}
self
}
/// **Deprecated:** Use `App::setting( SettingOne | SettingTwo )` instead
#[deprecated(
since = "2.33.0",
note = "Use `App::setting( SettingOne | SettingTwo )` instead"
)]
pub fn settings(mut self, settings: &[AppSettings]) -> Self {
for s in settings {
self.settings.set(*s);
}
self
}
/// **Deprecated:** Use `App::unset_setting( SettingOne | SettingTwo )` instead
#[deprecated(
since = "2.33.0",
note = "Use `App::unset_setting( SettingOne | SettingTwo )` instead"
)]
pub fn unset_settings(mut self, settings: &[AppSettings]) -> Self {
for s in settings {
self.settings.unset(*s);
self.g_settings.unset(*s);
}
self
}
/// **Deprecated:** Use explicit `App::author()` and `App::version()` calls instead.
#[deprecated(
since = "2.14.1",
note = "Can never work; use explicit App::author() and \
App::version() calls instead. Will be removed in v3.0-beta"
)]
pub fn with_defaults<S: Into<String>>(n: S) -> Self {
App {
name: n.into(),
author: Some("Kevin K. <kbknapp@gmail.com>"),
version: Some("2.19.2"),
..Default::default()
}
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use App::from instead. Will be removed in v3.0-beta"
)]
#[cfg(feature = "yaml")]
pub fn from_yaml(yaml: &'a Yaml) -> App<'a, 'a> { App::from(yaml) }
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `App::mut_arg(\"help\", |a| a.short(\"H\"))` instead. Will be removed in v3.0-beta"
)]
pub fn help_short<S: AsRef<str> + 'b>(mut self, s: S) -> Self {
let c = s
.as_ref()
.trim_left_matches(|c| c == '-')
.chars()
.nth(0)
.unwrap_or('h');
self.help_short = Some(c);
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `App::mut_arg(\"version\", |a| a.short(\"v\"))` instead. Will be removed in v3.0-beta"
)]
pub fn version_short<S: AsRef<str>>(mut self, s: S) -> Self {
let c = s
.as_ref()
.trim_left_matches(|c| c == '-')
.chars()
.nth(0)
.unwrap_or('V');
self.version_short = Some(c);
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `App::mut_arg(\"help\", |a| a.help(\"Some message\"))` instead. Will be removed in v3.0-beta"
)]
pub fn help_message<S: Into<&'a str>>(mut self, s: S) -> Self {
self.help_message = Some(s.into());
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `App::mut_arg(\"version\", |a| a.short(\"Some message\"))` instead. Will be removed in v3.0-beta"
)]
pub fn version_message<S: Into<&'a str>>(mut self, s: S) -> Self {
self.version_message = Some(s.into());
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Renamed to `App::override_usage`. Will be removed in v3.0-beta"
)]
pub fn usage<S: Into<&'b str>>(mut self, usage: S) -> Self {
self.usage_str = Some(usage.into());
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Renamed to `App::override_help`. Will be removed in v3.0-beta"
)]
pub fn help<S: Into<&'b str>>(mut self, help: S) -> Self {
self.help_str = Some(help.into());
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Renamed to `App::help_template`. Will be removed in v3.0-beta"
)]
pub fn template<S: Into<&'b str>>(mut self, s: S) -> Self {
self.template = Some(s.into());
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `App::arg(Arg::from(&str)` instead. Will be removed in v3.0-beta"
)]
pub fn arg_from_usage(mut self, usage: &'a str) -> Self {
self.args.push(Arg::from(usage));
self
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `App::args(&str)` instead. Will be removed in v3.0-beta"
)]
pub fn args_from_usage(mut self, usage: &'a str) -> Self {
for line in usage.lines() {
let l = line.trim();
if l.is_empty() {
continue;
}
self.args.push(Arg::from(l));
}
self
}
/// **Deprecated:** Use
#[allow(deprecated)]
#[deprecated(
since = "2.30.0",
note = "Use `clap_generate crate and clap_generate::generate_completions` instead. Will be removed in v3.0-beta"
)]
pub fn gen_completions<T: Into<OsString>, S: Into<String>>(
&mut self,
bin_name: S,
for_shell: Shell,
out_dir: T,
) {
use std::error::Error;
let out_dir = PathBuf::from(out_dir.into());
let name = &*self.bin_name.as_ref().unwrap().clone();
let file_name = match for_shell {
Shell::Bash => format!("{}.bash", name),
Shell::Fish => format!("{}.fish", name),
Shell::Zsh => format!("_{}", name),
Shell::PowerShell => format!("_{}.ps1", name),
Shell::Elvish => format!("{}.elv", name),
_ => panic!("Unsupported shell type for completion generation"),
};
let mut file = match File::create(out_dir.join(file_name)) {
Err(why) => panic!("couldn't create completion file: {}", why.description()),
Ok(file) => file,
};
self.gen_completions_to(bin_name.into(), for_shell, &mut file)
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Use `clap_generate crate and clap_generate::generate_completions_to` instead. Will be removed in v3.0-beta"
)]
pub fn gen_completions_to<W: Write, S: Into<String>>(
&mut self,
bin_name: S,
for_shell: Shell,
buf: &mut W,
) {
self.bin_name = Some(bin_name.into());
if !self.is_set(AppSettings::Propagated) {
self._build(Propagation::Full);
self._build_bin_names();
}
ComplGen::new(self).generate(for_shell, buf)
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Renamed `App::try_get_matches` to be consistent with Rust naming conventions. Will be removed in v3.0-beta"
)]
pub fn get_matches_safe(self) -> ClapResult<ArgMatches<'a>> {
// Start the parsing
self.try_get_matches_from(&mut env::args_os())
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Renamed `App::try_get_matches_from` to be consistent with Rust naming conventions. Will be removed in v3.0-beta"
)]
pub fn get_matches_from_safe<I, T>(mut self, itr: I) -> ClapResult<ArgMatches<'a>>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
self.try_get_matches_from_mut(itr)
}
/// **Deprecated:** Use
#[deprecated(
since = "2.30.0",
note = "Renamed `App::try_get_matches_from_mut` to be consistent with Rust naming conventions. Will be removed in v3.0-beta"
)]
pub fn get_matches_from_safe_borrow<I, T>(&mut self, itr: I) -> ClapResult<ArgMatches<'a>>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
self.try_get_matches_from_mut(itr)
}
}
#[cfg(feature = "yaml")] #[cfg(feature = "yaml")]
impl<'a> From<&'a Yaml> for App<'a, 'a> { impl<'a> From<&'a Yaml> for App<'a, 'a> {
fn from(mut yaml: &'a Yaml) -> Self { fn from(mut yaml: &'a Yaml) -> Self {
use parse::SubCommand;
// We WANT this to panic on error...so expect() is good. // We WANT this to panic on error...so expect() is good.
let mut is_sc = None; let mut is_sc = None;
let mut a = if let Some(name) = yaml["name"].as_str() { let mut a = if let Some(name) = yaml["name"].as_str() {
@ -2169,8 +1886,6 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
yaml_str!(a, yaml, template); yaml_str!(a, yaml, template);
yaml_str!(a, yaml, usage); yaml_str!(a, yaml, usage);
yaml_str!(a, yaml, help); yaml_str!(a, yaml, help);
yaml_str!(a, yaml, help_short);
yaml_str!(a, yaml, version_short);
yaml_str!(a, yaml, help_message); yaml_str!(a, yaml, help_message);
yaml_str!(a, yaml, version_message); yaml_str!(a, yaml, version_message);
yaml_str!(a, yaml, alias); yaml_str!(a, yaml, alias);
@ -2264,7 +1979,7 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
} }
if let Some(v) = yaml["subcommands"].as_vec() { if let Some(v) = yaml["subcommands"].as_vec() {
for sc_yaml in v { for sc_yaml in v {
a = a.subcommand(SubCommand::from_yaml(sc_yaml)); a = a.subcommand(::from_yaml(sc_yaml));
} }
} }
if let Some(v) = yaml["groups"].as_vec() { if let Some(v) = yaml["groups"].as_vec() {

View file

@ -14,39 +14,38 @@ bitflags! {
const UNIFIED_HELP = 1 << 5; const UNIFIED_HELP = 1 << 5;
const WAIT_ON_ERROR = 1 << 6; const WAIT_ON_ERROR = 1 << 6;
const SC_REQUIRED_ELSE_HELP= 1 << 7; const SC_REQUIRED_ELSE_HELP= 1 << 7;
const NEEDS_LONG_HELP = 1 << 8; const NO_AUTO_HELP = 1 << 8;
const NEEDS_LONG_VERSION = 1 << 9; const NO_AUTO_VERSION = 1 << 9;
const NEEDS_SC_HELP = 1 << 10; const DISABLE_VERSION = 1 << 10;
const DISABLE_VERSION = 1 << 11; const HIDDEN = 1 << 11;
const HIDDEN = 1 << 12; const TRAILING_VARARG = 1 << 12;
const TRAILING_VARARG = 1 << 13; const NO_BIN_NAME = 1 << 13;
const NO_BIN_NAME = 1 << 14; const ALLOW_UNK_SC = 1 << 14;
const ALLOW_UNK_SC = 1 << 15; const UTF8_STRICT = 1 << 15;
const UTF8_STRICT = 1 << 16; const UTF8_NONE = 1 << 16;
const UTF8_NONE = 1 << 17; const LEADING_HYPHEN = 1 << 17;
const LEADING_HYPHEN = 1 << 18; const NO_POS_VALUES = 1 << 18;
const NO_POS_VALUES = 1 << 19; const NEXT_LINE_HELP = 1 << 19;
const NEXT_LINE_HELP = 1 << 20; const DERIVE_DISP_ORDER = 1 << 20;
const DERIVE_DISP_ORDER = 1 << 21; const COLORED_HELP = 1 << 21;
const COLORED_HELP = 1 << 22; const COLOR_ALWAYS = 1 << 22;
const COLOR_ALWAYS = 1 << 23; const COLOR_AUTO = 1 << 23;
const COLOR_AUTO = 1 << 24; const COLOR_NEVER = 1 << 24;
const COLOR_NEVER = 1 << 25; const DONT_DELIM_TRAIL = 1 << 25;
const DONT_DELIM_TRAIL = 1 << 26; const ALLOW_NEG_NUMS = 1 << 26;
const ALLOW_NEG_NUMS = 1 << 27; const LOW_INDEX_MUL_POS = 1 << 27;
const LOW_INDEX_MUL_POS = 1 << 28; const DISABLE_HELP_SC = 1 << 28;
const DISABLE_HELP_SC = 1 << 29; const DONT_COLLAPSE_ARGS = 1 << 29;
const DONT_COLLAPSE_ARGS = 1 << 30; const ARGS_NEGATE_SCS = 1 << 30;
const ARGS_NEGATE_SCS = 1 << 31; const PROPAGATE_VALS_DOWN = 1 << 31;
const PROPAGATE_VALS_DOWN = 1 << 32; const ALLOW_MISSING_POS = 1 << 32;
const ALLOW_MISSING_POS = 1 << 33; const TRAILING_VALUES = 1 << 33;
const TRAILING_VALUES = 1 << 34; const VALID_NEG_NUM_FOUND = 1 << 34;
const VALID_NEG_NUM_FOUND = 1 << 35; const PROPAGATED = 1 << 35;
const PROPAGATED = 1 << 36; const VALID_ARG_FOUND = 1 << 36;
const VALID_ARG_FOUND = 1 << 37; const INFER_SUBCOMMANDS = 1 << 37;
const INFER_SUBCOMMANDS = 1 << 38; const CONTAINS_LAST = 1 << 38;
const CONTAINS_LAST = 1 << 39; const ARGS_OVERRIDE_SELF = 1 << 39;
const ARGS_OVERRIDE_SELF = 1 << 40;
} }
} }
@ -61,13 +60,7 @@ impl BitOr for AppFlags {
impl Default for AppFlags { impl Default for AppFlags {
fn default() -> Self { fn default() -> Self {
AppFlags( AppFlags(Flags::UTF8_NONE | Flags::COLOR_AUTO)
Flags::NEEDS_LONG_VERSION
| Flags::NEEDS_LONG_HELP
| Flags::NEEDS_SC_HELP
| Flags::UTF8_NONE
| Flags::COLOR_AUTO,
)
} }
} }
@ -97,9 +90,8 @@ impl AppFlags {
HidePossibleValuesInHelp => Flags::NO_POS_VALUES, HidePossibleValuesInHelp => Flags::NO_POS_VALUES,
Hidden => Flags::HIDDEN, Hidden => Flags::HIDDEN,
LowIndexMultiplePositional => Flags::LOW_INDEX_MUL_POS, LowIndexMultiplePositional => Flags::LOW_INDEX_MUL_POS,
NeedsLongHelp => Flags::NEEDS_LONG_HELP, NoAutoHelp => Flags::NO_AUTO_HELP,
NeedsLongVersion => Flags::NEEDS_LONG_VERSION, NoAutoVersion => Flags::NO_AUTO_VERSION,
NeedsSubcommandHelp => Flags::NEEDS_SC_HELP,
NoBinaryName => Flags::NO_BIN_NAME, NoBinaryName => Flags::NO_BIN_NAME,
PropagateGlobalValuesDown=> Flags::PROPAGATE_VALS_DOWN, PropagateGlobalValuesDown=> Flags::PROPAGATE_VALS_DOWN,
StrictUtf8 => Flags::UTF8_STRICT, StrictUtf8 => Flags::UTF8_STRICT,
@ -138,7 +130,7 @@ pub enum AppSettings {
/// UTF-8 values /// UTF-8 values
/// ///
/// **NOTE:** This rule only applies to argument values, as flags, options, and /// **NOTE:** This rule only applies to argument values, as flags, options, and
/// [`SubCommand`]s themselves only allow valid UTF-8 code points. /// [``]s themselves only allow valid UTF-8 code points.
/// ///
/// # Platform Specific /// # Platform Specific
/// ///
@ -154,8 +146,8 @@ pub enum AppSettings {
/// ///
/// let r = App::new("myprog") /// let r = App::new("myprog")
/// //.setting(AppSettings::AllowInvalidUtf8) /// //.setting(AppSettings::AllowInvalidUtf8)
/// .arg_from_usage("<arg> 'some positional arg'") /// .arg("<arg> 'some positional arg'")
/// .get_matches_from_safe( /// .try_get_matches_from(
/// vec![ /// vec![
/// OsString::from("myprog"), /// OsString::from("myprog"),
/// OsString::from_vec(vec![0xe9])]); /// OsString::from_vec(vec![0xe9])]);
@ -168,7 +160,7 @@ pub enum AppSettings {
/// [`ArgMatches::os_values_of`]: ./struct.ArgMatches.html#method.os_values_of /// [`ArgMatches::os_values_of`]: ./struct.ArgMatches.html#method.os_values_of
/// [`ArgMatches::lossy_value_of`]: ./struct.ArgMatches.html#method.lossy_value_of /// [`ArgMatches::lossy_value_of`]: ./struct.ArgMatches.html#method.lossy_value_of
/// [`ArgMatches::lossy_values_of`]: ./struct.ArgMatches.html#method.lossy_values_of /// [`ArgMatches::lossy_values_of`]: ./struct.ArgMatches.html#method.lossy_values_of
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
AllowInvalidUtf8, AllowInvalidUtf8,
/// Specifies that leading hyphens are allowed in argument *values*, such as negative numbers /// Specifies that leading hyphens are allowed in argument *values*, such as negative numbers
@ -213,7 +205,7 @@ pub enum AppSettings {
/// .version("v1.1") /// .version("v1.1")
/// .setting(AppSettings::AllowNegativeNumbers) /// .setting(AppSettings::AllowNegativeNumbers)
/// .arg(Arg::with_name("num")) /// .arg(Arg::with_name("num"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", "-20" /// "myprog", "-20"
/// ]); /// ]);
/// assert!(res.is_ok()); /// assert!(res.is_ok());
@ -332,7 +324,7 @@ pub enum AppSettings {
/// Specifies that an unexpected positional argument, /// Specifies that an unexpected positional argument,
/// which would otherwise cause a [`ErrorKind::UnknownArgument`] error, /// which would otherwise cause a [`ErrorKind::UnknownArgument`] error,
/// should instead be treated as a [`SubCommand`] within the [`ArgMatches`] struct. /// should instead be treated as a [``] within the [`ArgMatches`] struct.
/// ///
/// **NOTE:** Use this setting with caution, /// **NOTE:** Use this setting with caution,
/// as a truly unexpected argument (i.e. one that is *NOT* an external subcommand) /// as a truly unexpected argument (i.e. one that is *NOT* an external subcommand)
@ -362,7 +354,7 @@ pub enum AppSettings {
/// } /// }
/// ``` /// ```
/// [`ErrorKind::UnknownArgument`]: ./enum.ErrorKind.html#variant.UnknownArgument /// [`ErrorKind::UnknownArgument`]: ./enum.ErrorKind.html#variant.UnknownArgument
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`ArgMatches`]: ./struct.ArgMatches.html /// [`ArgMatches`]: ./struct.ArgMatches.html
AllowExternalSubcommands, AllowExternalSubcommands,
@ -384,14 +376,14 @@ pub enum AppSettings {
/// .setting(AppSettings::ArgsNegateSubcommands) /// .setting(AppSettings::ArgsNegateSubcommands)
/// # ; /// # ;
/// ``` /// ```
/// [subcommands]: ./struct.SubCommand.html /// [subcommands]: ./struct..html
/// [argument]: ./struct.Arg.html /// [argument]: ./struct.Arg.html
ArgsNegateSubcommands, ArgsNegateSubcommands,
/// Specifies that the help text should be displayed (and then exit gracefully), /// Specifies that the help text should be displayed (and then exit gracefully),
/// if no arguments are present at runtime (i.e. an empty run such as, `$ myprog`. /// if no arguments are present at runtime (i.e. an empty run such as, `$ myprog`.
/// ///
/// **NOTE:** [`SubCommand`]s count as arguments /// **NOTE:** [``]s count as arguments
/// ///
/// **NOTE:** Setting [`Arg::default_value`] effectively disables this option as it will /// **NOTE:** Setting [`Arg::default_value`] effectively disables this option as it will
/// ensure that some argument is always present. /// ensure that some argument is always present.
@ -404,7 +396,7 @@ pub enum AppSettings {
/// .setting(AppSettings::ArgRequiredElseHelp) /// .setting(AppSettings::ArgRequiredElseHelp)
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`Arg::default_value`]: ./struct.Arg.html#method.default_value /// [`Arg::default_value`]: ./struct.Arg.html#method.default_value
ArgRequiredElseHelp, ArgRequiredElseHelp,
@ -419,7 +411,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::ColoredHelp) /// .setting(AppSettings::ColoredHelp)
/// .get_matches(); /// .get_matches();
@ -439,7 +431,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::ColorAuto) /// .setting(AppSettings::ColorAuto)
/// .get_matches(); /// .get_matches();
@ -457,7 +449,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::ColorAlways) /// .setting(AppSettings::ColorAlways)
/// .get_matches(); /// .get_matches();
@ -475,7 +467,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::ColorNever) /// .setting(AppSettings::ColorNever)
/// .get_matches(); /// .get_matches();
@ -487,7 +479,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::DontCollapseArgsInUsage) /// .setting(AppSettings::DontCollapseArgsInUsage)
/// .get_matches(); /// .get_matches();
@ -504,7 +496,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::DontDelimitTrailingValues) /// .setting(AppSettings::DontDelimitTrailingValues)
/// .get_matches(); /// .get_matches();
@ -518,23 +510,23 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, AppSettings, ErrorKind, SubCommand}; /// # use clap::{App, AppSettings, ErrorKind, };
/// let res = App::new("myprog") /// let res = App::new("myprog")
/// .version("v1.1") /// .version("v1.1")
/// .setting(AppSettings::DisableHelpSubcommand) /// .setting(AppSettings::DisableHelpSubcommand)
/// // Normally, creating a subcommand causes a `help` subcommand to automaticaly /// // Normally, creating a subcommand causes a `help` subcommand to automaticaly
/// // be generated as well /// // be generated as well
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", "help" /// "myprog", "help"
/// ]); /// ]);
/// assert!(res.is_err()); /// assert!(res.is_err());
/// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); /// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
DisableHelpSubcommand, DisableHelpSubcommand,
/// Disables `-V` and `--version` [`App`] without affecting any of the [`SubCommand`]s /// Disables `-V` and `--version` [`App`] without affecting any of the [``]s
/// (Defaults to `false`; application *does* have a version flag) /// (Defaults to `false`; application *does* have a version flag)
/// ///
/// # Examples /// # Examples
@ -544,7 +536,7 @@ pub enum AppSettings {
/// let res = App::new("myprog") /// let res = App::new("myprog")
/// .version("v1.1") /// .version("v1.1")
/// .setting(AppSettings::DisableVersion) /// .setting(AppSettings::DisableVersion)
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", "-V" /// "myprog", "-V"
/// ]); /// ]);
/// assert!(res.is_err()); /// assert!(res.is_err());
@ -552,36 +544,36 @@ pub enum AppSettings {
/// ``` /// ```
/// ///
/// ```rust /// ```rust
/// # use clap::{App, SubCommand, AppSettings, ErrorKind}; /// # use clap::{App, AppSettings, ErrorKind};
/// let res = App::new("myprog") /// let res = App::new("myprog")
/// .version("v1.1") /// .version("v1.1")
/// .setting(AppSettings::DisableVersion) /// .setting(AppSettings::DisableVersion)
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", "test", "-V" /// "myprog", "test", "-V"
/// ]); /// ]);
/// assert!(res.is_err()); /// assert!(res.is_err());
/// assert_eq!(res.unwrap_err().kind, ErrorKind::VersionDisplayed); /// assert_eq!(res.unwrap_err().kind, ErrorKind::VersionDisplayed);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`App`]: ./struct.App.html /// [`App`]: ./struct.App.html
DisableVersion, DisableVersion,
/// Displays the arguments and [`SubCommand`]s in the help message in the order that they were /// Displays the arguments and [``]s in the help message in the order that they were
/// declared in, and not alphabetically which is the default. /// declared in, and not alphabetically which is the default.
/// ///
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::DeriveDisplayOrder) /// .setting(AppSettings::DeriveDisplayOrder)
/// .get_matches(); /// .get_matches();
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
DeriveDisplayOrder, DeriveDisplayOrder,
/// Specifies to use the version of the current command for all child [`SubCommand`]s. /// Specifies to use the version of the current command for all child [``]s.
/// (Defaults to `false`; subcommands have independent version strings from their parents.) /// (Defaults to `false`; subcommands have independent version strings from their parents.)
/// ///
/// **NOTE:** The version for the current command **and** this setting must be set **prior** to /// **NOTE:** The version for the current command **and** this setting must be set **prior** to
@ -590,30 +582,30 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .version("v1.1") /// .version("v1.1")
/// .setting(AppSettings::GlobalVersion) /// .setting(AppSettings::GlobalVersion)
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches(); /// .get_matches();
/// // running `$ myprog test --version` will display /// // running `$ myprog test --version` will display
/// // "myprog-test v1.1" /// // "myprog-test v1.1"
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
GlobalVersion, GlobalVersion,
/// Specifies that this [`SubCommand`] should be hidden from help messages /// Specifies that this [``] should be hidden from help messages
/// ///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, AppSettings, SubCommand}; /// # use clap::{App, Arg, AppSettings, };
/// App::new("myprog") /// App::new("myprog")
/// .subcommand(SubCommand::with_name("test") /// .subcommand(App::new("test")
/// .setting(AppSettings::Hidden)) /// .setting(AppSettings::Hidden))
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
Hidden, Hidden,
/// Tells `clap` *not* to print possible values when displaying help information. /// Tells `clap` *not* to print possible values when displaying help information.
@ -635,16 +627,16 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// let m = App::new("prog") /// let m = App::new("prog")
/// .setting(AppSettings::InferSubcommands) /// .setting(AppSettings::InferSubcommands)
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from(vec![ /// .get_matches_from(vec![
/// "prog", "te" /// "prog", "te"
/// ]); /// ]);
/// assert_eq!(m.subcommand_name(), Some("test")); /// assert_eq!(m.subcommand_name(), Some("test"));
/// ``` /// ```
/// [`subcommands`]: ./struct.SubCommand.html /// [`subcommands`]: ./struct..html
/// [positional/free arguments]: ./struct.Arg.html#method.index /// [positional/free arguments]: ./struct.Arg.html#method.index
/// [aliases]: ./struct.App.html#method.alias /// [aliases]: ./struct.App.html#method.alias
/// [`AppSeettings::ArgsNegateSubcommands`]: ./enum.AppSettings.html#variant.ArgsNegateSubcommands /// [`AppSeettings::ArgsNegateSubcommands`]: ./enum.AppSettings.html#variant.ArgsNegateSubcommands
@ -673,7 +665,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::NextLineHelp) /// .setting(AppSettings::NextLineHelp)
/// .get_matches(); /// .get_matches();
@ -689,11 +681,11 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, AppSettings, SubCommand}; /// # use clap::{App, Arg, AppSettings, };
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .arg(Arg::from("[cmd] 'command to run'") /// .arg(Arg::from("[cmd] 'command to run'")
/// .global(true)) /// .global(true))
/// .subcommand(SubCommand::with_name("foo")) /// .subcommand(App::new("foo"))
/// .get_matches_from(vec!["myprog", "set", "foo"]); /// .get_matches_from(vec!["myprog", "set", "foo"]);
/// ///
/// assert_eq!(m.value_of("cmd"), Some("set")); /// assert_eq!(m.value_of("cmd"), Some("set"));
@ -705,11 +697,11 @@ pub enum AppSettings {
/// propagated down. /// propagated down.
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, AppSettings, SubCommand}; /// # use clap::{App, Arg, AppSettings, };
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .arg(Arg::from("[cmd] 'command to run'") /// .arg(Arg::from("[cmd] 'command to run'")
/// .global(true)) /// .global(true))
/// .subcommand(SubCommand::with_name("foo")) /// .subcommand(App::new("foo"))
/// .get_matches_from(vec!["myprog", "set"]); /// .get_matches_from(vec!["myprog", "set"]);
/// ///
/// assert_eq!(m.value_of("cmd"), Some("set")); /// assert_eq!(m.value_of("cmd"), Some("set"));
@ -722,7 +714,7 @@ pub enum AppSettings {
)] )]
PropagateGlobalValuesDown, PropagateGlobalValuesDown,
/// Allows [`SubCommand`]s to override all requirements of the parent command. /// Allows [``]s to override all requirements of the parent command.
/// For example if you had a subcommand or top level application with a required argument /// For example if you had a subcommand or top level application with a required argument
/// that is only required as long as there is no subcommand present, /// that is only required as long as there is no subcommand present,
/// using this setting would allow you to set those arguments to [`Arg::required(true)`] /// using this setting would allow you to set those arguments to [`Arg::required(true)`]
@ -735,12 +727,12 @@ pub enum AppSettings {
/// This first example shows that it is an error to not use a required argument /// This first example shows that it is an error to not use a required argument
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, AppSettings, SubCommand, ErrorKind}; /// # use clap::{App, Arg, AppSettings, ErrorKind};
/// let err = App::new("myprog") /// let err = App::new("myprog")
/// .setting(AppSettings::SubcommandsNegateReqs) /// .setting(AppSettings::SubcommandsNegateReqs)
/// .arg(Arg::with_name("opt").required(true)) /// .arg(Arg::with_name("opt").required(true))
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog" /// "myprog"
/// ]); /// ]);
/// assert!(err.is_err()); /// assert!(err.is_err());
@ -752,23 +744,23 @@ pub enum AppSettings {
/// valid subcommand is used. /// valid subcommand is used.
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, AppSettings, SubCommand, ErrorKind}; /// # use clap::{App, Arg, AppSettings, ErrorKind};
/// let noerr = App::new("myprog") /// let noerr = App::new("myprog")
/// .setting(AppSettings::SubcommandsNegateReqs) /// .setting(AppSettings::SubcommandsNegateReqs)
/// .arg(Arg::with_name("opt").required(true)) /// .arg(Arg::with_name("opt").required(true))
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", "test" /// "myprog", "test"
/// ]); /// ]);
/// assert!(noerr.is_ok()); /// assert!(noerr.is_ok());
/// # ; /// # ;
/// ``` /// ```
/// [`Arg::required(true)`]: ./struct.Arg.html#method.required /// [`Arg::required(true)`]: ./struct.Arg.html#method.required
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
SubcommandsNegateReqs, SubcommandsNegateReqs,
/// Specifies that the help text should be displayed (before exiting gracefully) if no /// Specifies that the help text should be displayed (before exiting gracefully) if no
/// [`SubCommand`]s are present at runtime (i.e. an empty run such as `$ myprog`). /// [``]s are present at runtime (i.e. an empty run such as `$ myprog`).
/// ///
/// **NOTE:** This should *not* be used with [`AppSettings::SubcommandRequired`] as they do /// **NOTE:** This should *not* be used with [`AppSettings::SubcommandRequired`] as they do
/// nearly same thing; this prints the help text, and the other prints an error. /// nearly same thing; this prints the help text, and the other prints an error.
@ -785,7 +777,7 @@ pub enum AppSettings {
/// .setting(AppSettings::SubcommandRequiredElseHelp) /// .setting(AppSettings::SubcommandRequiredElseHelp)
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`AppSettings::SubcommandRequired`]: ./enum.AppSettings.html#variant.SubcommandRequired /// [`AppSettings::SubcommandRequired`]: ./enum.AppSettings.html#variant.SubcommandRequired
/// [`AppSettings::ArgRequiredElseHelp`]: ./enum.AppSettings.html#variant.ArgRequiredElseHelp /// [`AppSettings::ArgRequiredElseHelp`]: ./enum.AppSettings.html#variant.ArgRequiredElseHelp
SubcommandRequiredElseHelp, SubcommandRequiredElseHelp,
@ -794,7 +786,7 @@ pub enum AppSettings {
/// with a [`ErrorKind::InvalidUtf8`] error. /// with a [`ErrorKind::InvalidUtf8`] error.
/// ///
/// **NOTE:** This rule only applies to argument values; Things such as flags, options, and /// **NOTE:** This rule only applies to argument values; Things such as flags, options, and
/// [`SubCommand`]s themselves only allow valid UTF-8 code points. /// [``]s themselves only allow valid UTF-8 code points.
/// ///
/// # Platform Specific /// # Platform Specific
/// ///
@ -810,8 +802,8 @@ pub enum AppSettings {
/// ///
/// let m = App::new("myprog") /// let m = App::new("myprog")
/// .setting(AppSettings::StrictUtf8) /// .setting(AppSettings::StrictUtf8)
/// .arg_from_usage("<arg> 'some positional arg'") /// .arg("<arg> 'some positional arg'")
/// .get_matches_from_safe( /// .try_get_matches_from(
/// vec![ /// vec![
/// OsString::from("myprog"), /// OsString::from("myprog"),
/// OsString::from_vec(vec![0xe9])]); /// OsString::from_vec(vec![0xe9])]);
@ -819,11 +811,11 @@ pub enum AppSettings {
/// assert!(m.is_err()); /// assert!(m.is_err());
/// assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); /// assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`ErrorKind::InvalidUtf8`]: ./enum.ErrorKind.html#variant.InvalidUtf8 /// [`ErrorKind::InvalidUtf8`]: ./enum.ErrorKind.html#variant.InvalidUtf8
StrictUtf8, StrictUtf8,
/// Allows specifying that if no [`SubCommand`] is present at runtime, /// Allows specifying that if no [``] is present at runtime,
/// error and exit gracefully. /// error and exit gracefully.
/// ///
/// **NOTE:** This defaults to `false` (subcommands do *not* need to be present) /// **NOTE:** This defaults to `false` (subcommands do *not* need to be present)
@ -831,18 +823,18 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, AppSettings, SubCommand, ErrorKind}; /// # use clap::{App, AppSettings, ErrorKind};
/// let err = App::new("myprog") /// let err = App::new("myprog")
/// .setting(AppSettings::SubcommandRequired) /// .setting(AppSettings::SubcommandRequired)
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", /// "myprog",
/// ]); /// ]);
/// assert!(err.is_err()); /// assert!(err.is_err());
/// assert_eq!(err.unwrap_err().kind, ErrorKind::MissingSubcommand); /// assert_eq!(err.unwrap_err().kind, ErrorKind::MissingSubcommand);
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
SubcommandRequired, SubcommandRequired,
/// Specifies that the final positional argument is a "VarArg" and that `clap` should not /// Specifies that the final positional argument is a "VarArg" and that `clap` should not
@ -879,7 +871,7 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand, AppSettings}; /// # use clap::{App, Arg, AppSettings};
/// App::new("myprog") /// App::new("myprog")
/// .setting(AppSettings::UnifiedHelpMessage) /// .setting(AppSettings::UnifiedHelpMessage)
/// .get_matches(); /// .get_matches();
@ -887,7 +879,7 @@ pub enum AppSettings {
/// ``` /// ```
UnifiedHelpMessage, UnifiedHelpMessage,
/// Disables `-V` and `--version` for all [`SubCommand`]s /// Disables `-V` and `--version` for all [``]s
/// (Defaults to `false`; subcommands *do* have version flags.) /// (Defaults to `false`; subcommands *do* have version flags.)
/// ///
/// **NOTE:** This setting must be set **prior** adding any subcommands /// **NOTE:** This setting must be set **prior** adding any subcommands
@ -895,18 +887,18 @@ pub enum AppSettings {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, SubCommand, AppSettings, ErrorKind}; /// # use clap::{App, AppSettings, ErrorKind};
/// let res = App::new("myprog") /// let res = App::new("myprog")
/// .version("v1.1") /// .version("v1.1")
/// .setting(AppSettings::VersionlessSubcommands) /// .setting(AppSettings::VersionlessSubcommands)
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", "test", "-V" /// "myprog", "test", "-V"
/// ]); /// ]);
/// assert!(res.is_err()); /// assert!(res.is_err());
/// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); /// assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
VersionlessSubcommands, VersionlessSubcommands,
/// Will display a message "Press \[ENTER\]/\[RETURN\] to continue..." and wait for user before /// Will display a message "Press \[ENTER\]/\[RETURN\] to continue..." and wait for user before
@ -916,7 +908,7 @@ pub enum AppSettings {
/// Windows where a user tries to open the binary by double-clicking instead of using the /// Windows where a user tries to open the binary by double-clicking instead of using the
/// command line. /// command line.
/// ///
/// **NOTE:** This setting is **not** recursive with [`SubCommand`]s, meaning if you wish this /// **NOTE:** This setting is **not** recursive with [``]s, meaning if you wish this
/// behavior for all subcommands, you must set this on each command (needing this is extremely /// behavior for all subcommands, you must set this on each command (needing this is extremely
/// rare) /// rare)
/// ///
@ -928,17 +920,14 @@ pub enum AppSettings {
/// .setting(AppSettings::WaitOnError) /// .setting(AppSettings::WaitOnError)
/// # ; /// # ;
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
WaitOnError, WaitOnError,
#[doc(hidden)] /// @TODO-v3: @docs write them...maybe rename
NeedsLongVersion, NoAutoHelp,
#[doc(hidden)] /// @TODO-v3: @docs write them...maybe rename
NeedsLongHelp, NoAutoVersion,
#[doc(hidden)]
NeedsSubcommandHelp,
#[doc(hidden)] #[doc(hidden)]
LowIndexMultiplePositional, LowIndexMultiplePositional,

View file

@ -569,7 +569,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("dbg") /// .arg(Arg::with_name("dbg")
/// .long("debug")) /// .long("debug"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--debug" /// "prog", "--debug"
/// ]); /// ]);
/// ///
@ -587,7 +587,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("dbg") /// .arg(Arg::with_name("dbg")
/// .long("debug")) /// .long("debug"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog" /// "prog"
/// ]); /// ]);
/// ///
@ -638,7 +638,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile") /// .arg(Arg::with_name("infile")
/// .short('i') /// .short('i')
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--debug", "-i", "file" /// "prog", "--debug", "-i", "file"
/// ]); /// ]);
/// ///
@ -660,7 +660,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile") /// .arg(Arg::with_name("infile")
/// .short('i') /// .short('i')
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog" /// "prog"
/// ]); /// ]);
/// ///
@ -712,7 +712,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile") /// .arg(Arg::with_name("infile")
/// .short('i') /// .short('i')
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--debug" /// "prog", "--debug"
/// ]); /// ]);
/// ///
@ -734,7 +734,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile") /// .arg(Arg::with_name("infile")
/// .short('i') /// .short('i')
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog" /// "prog"
/// ]); /// ]);
/// ///
@ -785,7 +785,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("debug") /// .arg(Arg::with_name("debug")
/// .long("debug")) /// .long("debug"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--debug", "--config", "file.conf" /// "prog", "--debug", "--config", "file.conf"
/// ]); /// ]);
/// ///
@ -834,7 +834,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("debug")) /// .long("debug"))
/// .arg(Arg::with_name("input") /// .arg(Arg::with_name("input")
/// .index(1)) /// .index(1))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--config", "file.conf", "file.txt" /// "prog", "--config", "file.conf", "file.txt"
/// ]); /// ]);
/// ///
@ -1028,7 +1028,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("input") /// .arg(Arg::with_name("input")
/// .index(1)) /// .index(1))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog" /// "prog"
/// ]); /// ]);
/// ///
@ -1046,7 +1046,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("input") /// .arg(Arg::with_name("input")
/// .index(1)) /// .index(1))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--config", "file.conf" /// "prog", "--config", "file.conf"
/// ]); /// ]);
/// ///
@ -1098,7 +1098,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .requires_if("my.cfg", "other") /// .requires_if("my.cfg", "other")
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("other")) /// .arg(Arg::with_name("other"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--config", "some.cfg" /// "prog", "--config", "some.cfg"
/// ]); /// ]);
/// ///
@ -1116,7 +1116,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .requires_if("my.cfg", "input") /// .requires_if("my.cfg", "input")
/// .long("config")) /// .long("config"))
/// .arg(Arg::with_name("input")) /// .arg(Arg::with_name("input"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--config", "my.cfg" /// "prog", "--config", "my.cfg"
/// ]); /// ]);
/// ///
@ -1176,7 +1176,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("option") /// .long("option")
/// .takes_value(true)) /// .takes_value(true))
/// .arg(Arg::with_name("other")) /// .arg(Arg::with_name("other"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--config", "special.conf" /// "prog", "--config", "special.conf"
/// ]); /// ]);
/// ///
@ -1234,7 +1234,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("other") /// .arg(Arg::with_name("other")
/// .long("other") /// .long("other")
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--other", "not-special" /// "prog", "--other", "not-special"
/// ]); /// ]);
/// ///
@ -1254,7 +1254,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("other") /// .arg(Arg::with_name("other")
/// .long("other") /// .long("other")
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--other", "special" /// "prog", "--other", "special"
/// ]); /// ]);
/// ///
@ -1317,7 +1317,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("option") /// .arg(Arg::with_name("option")
/// .takes_value(true) /// .takes_value(true)
/// .long("option")) /// .long("option"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--option", "other" /// "prog", "--option", "other"
/// ]); /// ]);
/// ///
@ -1343,7 +1343,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("option") /// .arg(Arg::with_name("option")
/// .takes_value(true) /// .takes_value(true)
/// .long("option")) /// .long("option"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--option", "spec" /// "prog", "--option", "spec"
/// ]); /// ]);
/// ///
@ -1398,7 +1398,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .index(1)) /// .index(1))
/// .arg(Arg::with_name("output") /// .arg(Arg::with_name("output")
/// .index(2)) /// .index(2))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog" /// "prog"
/// ]); /// ]);
/// ///
@ -1419,7 +1419,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .index(1)) /// .index(1))
/// .arg(Arg::with_name("output") /// .arg(Arg::with_name("output")
/// .index(2)) /// .index(2))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--config", "file.conf", "in.txt" /// "prog", "--config", "file.conf", "in.txt"
/// ]); /// ]);
/// ///
@ -1587,7 +1587,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("mode") /// .long("mode")
/// .takes_value(true) /// .takes_value(true)
/// .possible_values(&["fast", "slow", "medium"])) /// .possible_values(&["fast", "slow", "medium"]))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--mode", "wrong" /// "prog", "--mode", "wrong"
/// ]); /// ]);
/// assert!(res.is_err()); /// assert!(res.is_err());
@ -1652,7 +1652,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .possible_value("fast") /// .possible_value("fast")
/// .possible_value("slow") /// .possible_value("slow")
/// .possible_value("medium")) /// .possible_value("medium"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "--mode", "wrong" /// "prog", "--mode", "wrong"
/// ]); /// ]);
/// assert!(res.is_err()); /// assert!(res.is_err());
@ -1779,7 +1779,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true) /// .takes_value(true)
/// .number_of_values(2) /// .number_of_values(2)
/// .short('F')) /// .short('F'))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "-F", "file1" /// "prog", "-F", "file1"
/// ]); /// ]);
/// ///
@ -1819,7 +1819,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("file") /// .arg(Arg::with_name("file")
/// .index(1) /// .index(1)
/// .validator(has_at)) /// .validator(has_at))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "some@file" /// "prog", "some@file"
/// ]); /// ]);
/// assert!(res.is_ok()); /// assert!(res.is_ok());
@ -1858,7 +1858,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("file") /// .arg(Arg::with_name("file")
/// .index(1) /// .index(1)
/// .validator_os(has_ampersand)) /// .validator_os(has_ampersand))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "Fish & chips" /// "prog", "Fish & chips"
/// ]); /// ]);
/// assert!(res.is_ok()); /// assert!(res.is_ok());
@ -1907,7 +1907,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true) /// .takes_value(true)
/// .max_values(3) /// .max_values(3)
/// .short('F')) /// .short('F'))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "-F", "file1", "file2" /// "prog", "-F", "file1", "file2"
/// ]); /// ]);
/// ///
@ -1926,7 +1926,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true) /// .takes_value(true)
/// .max_values(2) /// .max_values(2)
/// .short('F')) /// .short('F'))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "-F", "file1", "file2", "file3" /// "prog", "-F", "file1", "file2", "file3"
/// ]); /// ]);
/// ///
@ -1971,7 +1971,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true) /// .takes_value(true)
/// .min_values(2) /// .min_values(2)
/// .short('F')) /// .short('F'))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "-F", "file1", "file2", "file3" /// "prog", "-F", "file1", "file2", "file3"
/// ]); /// ]);
/// ///
@ -1990,7 +1990,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true) /// .takes_value(true)
/// .min_values(2) /// .min_values(2)
/// .short('F')) /// .short('F'))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "prog", "-F", "file1" /// "prog", "-F", "file1"
/// ]); /// ]);
/// ///
@ -2974,7 +2974,7 @@ impl<'a, 'b> Arg<'a, 'b> {
} }
} }
/// Specifies that an argument can be matched to all child [`SubCommand`]s. /// Specifies that an argument can be matched to all child [``]s.
/// ///
/// **NOTE:** Global arguments *only* propagate down, **not** up (to parent commands), however /// **NOTE:** Global arguments *only* propagate down, **not** up (to parent commands), however
/// their values once a user uses them will be propagated back up to parents. In effect, this /// their values once a user uses them will be propagated back up to parents. In effect, this
@ -2996,14 +2996,14 @@ impl<'a, 'b> Arg<'a, 'b> {
/// want to clutter the source with three duplicate [`Arg`] definitions. /// want to clutter the source with three duplicate [`Arg`] definitions.
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, SubCommand, ArgSettings}; /// # use clap::{App, Arg, ArgSettings};
/// let m = App::new("prog") /// let m = App::new("prog")
/// .arg(Arg::with_name("verb") /// .arg(Arg::with_name("verb")
/// .long("verbose") /// .long("verbose")
/// .short('v') /// .short('v')
/// .setting(ArgSettings::Global)) /// .setting(ArgSettings::Global))
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .subcommand(SubCommand::with_name("do-stuff")) /// .subcommand(App::new("do-stuff"))
/// .get_matches_from(vec![ /// .get_matches_from(vec![
/// "prog", "do-stuff", "--verbose" /// "prog", "do-stuff", "--verbose"
/// ]); /// ]);
@ -3012,7 +3012,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// let sub_m = m.subcommand_matches("do-stuff").unwrap(); /// let sub_m = m.subcommand_matches("do-stuff").unwrap();
/// assert!(sub_m.is_present("verb")); /// assert!(sub_m.is_present("verb"));
/// ``` /// ```
/// [`SubCommand`]: ./struct.App.html#method.subcommand /// [``]: ./struct.App.html#method.subcommand
/// [required]: ./enum.ArgSettings.html#variant.Required /// [required]: ./enum.ArgSettings.html#variant.Required
/// [`ArgMatches`]: ./struct.ArgMatches.html /// [`ArgMatches`]: ./struct.ArgMatches.html
/// [`ArgMatches::is_present("flag")`]: ./struct.ArgMatches.html#method.is_present /// [`ArgMatches::is_present("flag")`]: ./struct.ArgMatches.html#method.is_present
@ -3846,9 +3846,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// ``` /// ```
pub fn hidden_short_help(self, hide: bool) -> Self { pub fn hidden_short_help(self, hide: bool) -> Self {
if hide { if hide {
self.set(ArgSettings::HiddenShortHelp) self.setting(ArgSettings::HiddenShortHelp)
} else { } else {
self.unset(ArgSettings::HiddenShortHelp) self.unset_setting(ArgSettings::HiddenShortHelp)
} }
} }
@ -3923,9 +3923,9 @@ impl<'a, 'b> Arg<'a, 'b> {
/// ``` /// ```
pub fn hidden_long_help(self, hide: bool) -> Self { pub fn hidden_long_help(self, hide: bool) -> Self {
if hide { if hide {
self.set(ArgSettings::HiddenLongHelp) self.setting(ArgSettings::HiddenLongHelp)
} else { } else {
self.unset(ArgSettings::HiddenLongHelp) self.unset_setting(ArgSettings::HiddenLongHelp)
} }
} }
@ -4050,46 +4050,12 @@ impl<'a, 'b> Arg<'a, 'b> {
} }
} }
// Deprecations
// @TODO @v3-beta: remove
impl<'a, 'b> Arg<'a, 'b> {
/// **Deprecated**
#[deprecated(
since = "2.30.0",
note = "Renamed to `Arg::setting`. Will be removed in v3.0-beta"
)]
pub fn set(mut self, s: ArgSettings) -> Self {
self.setb(s);
self
}
/// **Deprecated**
#[deprecated(
since = "2.30.0",
note = "Renamed to `Arg::unset_setting`. Will be removed in v3.0-beta"
)]
pub fn unset(mut self, s: ArgSettings) -> Self {
self.unsetb(s);
self
}
/// **Deprecated**
#[deprecated(
since = "2.30.0",
note = "Use `Arg::from` instead. Will be removed in v3.0-beta"
)]
pub fn from_usage(u: &'a str) -> Self {
let parser = UsageParser::from_usage(u);
parser.parse()
}
}
impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for Arg<'a, 'b> { impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for Arg<'a, 'b> {
fn from(a: &'z Arg<'a, 'b>) -> Self { a.clone() } fn from(a: &'z Arg<'a, 'b>) -> Self { a.clone() }
} }
impl<'a, 'b> From<&'a str> for Arg<'a, 'b> { impl<'a, 'b> From<&'a str> for Arg<'a, 'b> {
fn from(s: &'a str) -> Self { Self::from_usage(s) } fn from(s: &'a str) -> Self { UsageParser::from_usage(s).parse() }
} }
impl<'n, 'e> PartialEq for Arg<'n, 'e> { impl<'n, 'e> PartialEq for Arg<'n, 'e> {

View file

@ -39,15 +39,14 @@ use yaml_rust;
/// ```rust /// ```rust
/// # use clap::{App, ArgGroup, ErrorKind}; /// # use clap::{App, ArgGroup, ErrorKind};
/// let result = App::new("app") /// let result = App::new("app")
/// .args_from_usage( /// .arg("--set-ver [ver] 'set the version manually'")
/// "--set-ver [ver] 'set the version manually' /// .arg("--major 'auto increase major'")
/// --major 'auto increase major' /// .arg("--minor 'auto increase minor'")
/// --minor 'auto increase minor' /// .arg("--patch 'auto increase patch'")
/// --patch 'auto increase patch'")
/// .group(ArgGroup::with_name("vers") /// .group(ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"]) /// .args(&["set-ver", "major", "minor","patch"])
/// .required(true)) /// .required(true))
/// .get_matches_from_safe(vec!["app", "--major", "--patch"]); /// .try_get_matches_from(vec!["app", "--major", "--patch"]);
/// // Because we used two args in the group it's an error /// // Because we used two args in the group it's an error
/// assert!(result.is_err()); /// assert!(result.is_err());
/// let err = result.unwrap_err(); /// let err = result.unwrap_err();
@ -58,15 +57,14 @@ use yaml_rust;
/// ```rust /// ```rust
/// # use clap::{App, ArgGroup}; /// # use clap::{App, ArgGroup};
/// let result = App::new("app") /// let result = App::new("app")
/// .args_from_usage( /// .arg("--set-ver [ver] 'set the version manually'")
/// "--set-ver [ver] 'set the version manually' /// .arg("--major 'auto increase major'")
/// --major 'auto increase major' /// .arg("--minor 'auto increase minor'")
/// --minor 'auto increase minor' /// .arg("--patch 'auto increase patch'")
/// --patch 'auto increase patch'")
/// .group(ArgGroup::with_name("vers") /// .group(ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"]) /// .args(&["set-ver", "major", "minor","patch"])
/// .required(true)) /// .required(true))
/// .get_matches_from_safe(vec!["app", "--major"]); /// .try_get_matches_from(vec!["app", "--major"]);
/// assert!(result.is_ok()); /// assert!(result.is_ok());
/// let matches = result.unwrap(); /// let matches = result.unwrap();
/// // We may not know which of the args was used, so we can test for the group... /// // We may not know which of the args was used, so we can test for the group...
@ -225,7 +223,7 @@ impl<'a> ArgGroup<'a> {
/// .short('c')) /// .short('c'))
/// .group(ArgGroup::with_name("req_flags") /// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"])) /// .args(&["flag", "color"]))
/// .get_matches_from_safe(vec!["myprog", "-f", "-c"]); /// .try_get_matches_from(vec!["myprog", "-f", "-c"]);
/// // Because we used both args in the group it's an error /// // Because we used both args in the group it's an error
/// assert!(result.is_err()); /// assert!(result.is_err());
/// let err = result.unwrap_err(); /// let err = result.unwrap_err();
@ -242,7 +240,7 @@ impl<'a> ArgGroup<'a> {
/// that one argument from this group *must* be present at runtime (unless /// that one argument from this group *must* be present at runtime (unless
/// conflicting with another argument). /// conflicting with another argument).
/// ///
/// **NOTE:** This setting only applies to the current [`App`] / [`SubCommand`], and not /// **NOTE:** This setting only applies to the current [`App`] / [``], and not
/// globally. /// globally.
/// ///
/// **NOTE:** By default, [`ArgGroup::multiple`] is set to `false` which when combined with /// **NOTE:** By default, [`ArgGroup::multiple`] is set to `false` which when combined with
@ -262,14 +260,14 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags") /// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"]) /// .args(&["flag", "color"])
/// .required(true)) /// .required(true))
/// .get_matches_from_safe(vec!["myprog"]); /// .try_get_matches_from(vec!["myprog"]);
/// // Because we didn't use any of the args in the group, it's an error /// // Because we didn't use any of the args in the group, it's an error
/// assert!(result.is_err()); /// assert!(result.is_err());
/// let err = result.unwrap_err(); /// let err = result.unwrap_err();
/// assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); /// assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
/// ``` /// ```
/// [`App`]: ./struct.App.html /// [`App`]: ./struct.App.html
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`ArgGroup::multiple`]: ./struct.ArgGroup.html#method.multiple /// [`ArgGroup::multiple`]: ./struct.ArgGroup.html#method.multiple
pub fn required(mut self, r: bool) -> Self { pub fn required(mut self, r: bool) -> Self {
self.required = r; self.required = r;
@ -297,7 +295,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags") /// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"]) /// .args(&["flag", "color"])
/// .requires("debug")) /// .requires("debug"))
/// .get_matches_from_safe(vec!["myprog", "-c"]); /// .try_get_matches_from(vec!["myprog", "-c"]);
/// // because we used an arg from the group, and the group requires "-d" to be used, it's an /// // because we used an arg from the group, and the group requires "-d" to be used, it's an
/// // error /// // error
/// assert!(result.is_err()); /// assert!(result.is_err());
@ -338,7 +336,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags") /// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"]) /// .args(&["flag", "color"])
/// .requires_all(&["debug", "verb"])) /// .requires_all(&["debug", "verb"]))
/// .get_matches_from_safe(vec!["myprog", "-c", "-d"]); /// .try_get_matches_from(vec!["myprog", "-c", "-d"]);
/// // because we used an arg from the group, and the group requires "-d" and "-v" to be used, /// // because we used an arg from the group, and the group requires "-d" and "-v" to be used,
/// // yet we only used "-d" it's an error /// // yet we only used "-d" it's an error
/// assert!(result.is_err()); /// assert!(result.is_err());
@ -374,7 +372,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags") /// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"]) /// .args(&["flag", "color"])
/// .conflicts_with("debug")) /// .conflicts_with("debug"))
/// .get_matches_from_safe(vec!["myprog", "-c", "-d"]); /// .try_get_matches_from(vec!["myprog", "-c", "-d"]);
/// // because we used an arg from the group, and the group conflicts with "-d", it's an error /// // because we used an arg from the group, and the group conflicts with "-d", it's an error
/// assert!(result.is_err()); /// assert!(result.is_err());
/// let err = result.unwrap_err(); /// let err = result.unwrap_err();
@ -412,7 +410,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags") /// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"]) /// .args(&["flag", "color"])
/// .conflicts_with_all(&["debug", "verb"])) /// .conflicts_with_all(&["debug", "verb"]))
/// .get_matches_from_safe(vec!["myprog", "-c", "-v"]); /// .try_get_matches_from(vec!["myprog", "-c", "-v"]);
/// // because we used an arg from the group, and the group conflicts with either "-v" or "-d" /// // because we used an arg from the group, and the group conflicts with either "-v" or "-d"
/// // it's an error /// // it's an error
/// assert!(result.is_err()); /// assert!(result.is_err());

View file

@ -1,190 +0,0 @@
// Std
use std::io::Write;
// Internal
use build::{App, Arg};
use completions;
pub struct BashGen<'a, 'b>(&'b App<'a, 'b>)
where
'a: 'b;
impl<'a, 'b> BashGen<'a, 'b> {
pub fn new(app: &'b App<'a, 'b>) -> Self { BashGen(app) }
pub fn generate_to<W: Write>(&self, buf: &mut W) {
w!(
buf,
format!(
"_{name}() {{
local i cur prev opts cmds
COMPREPLY=()
cur=\"${{COMP_WORDS[COMP_CWORD]}}\"
prev=\"${{COMP_WORDS[COMP_CWORD-1]}}\"
cmd=\"\"
opts=\"\"
for i in ${{COMP_WORDS[@]}}
do
case \"${{i}}\" in
{name})
cmd=\"{name}\"
;;
{subcmds}
*)
;;
esac
done
case \"${{cmd}}\" in
{name})
opts=\"{name_opts}\"
if [[ ${{cur}} == -* || ${{COMP_CWORD}} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W \"${{opts}}\" -- ${{cur}}) )
return 0
fi
case \"${{prev}}\" in
{name_opts_details}
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W \"${{opts}}\" -- ${{cur}}) )
return 0
;;
{subcmd_details}
esac
}}
complete -F _{name} -o bashdefault -o default {name}
",
name = self.0.bin_name.as_ref().unwrap(),
name_opts = self.all_options_for_path(self.0.bin_name.as_ref().unwrap()),
name_opts_details = self.option_details_for_path(self.0.bin_name.as_ref().unwrap()),
subcmds = self.all_subcommands(),
subcmd_details = self.subcommand_details()
).as_bytes()
);
}
fn all_subcommands(&self) -> String {
debugln!("BashGen::all_subcommands;");
let mut subcmds = String::new();
let scs = completions::all_subcommand_names(self.0);
for sc in &scs {
subcmds = format!(
"{}
{name})
cmd+=\"__{fn_name}\"
;;",
subcmds,
name = sc,
fn_name = sc.replace("-", "__")
);
}
subcmds
}
fn subcommand_details(&self) -> String {
debugln!("BashGen::subcommand_details;");
let mut subcmd_dets = String::new();
let mut scs = completions::get_all_subcommand_paths(self.0, true);
scs.sort();
scs.dedup();
for sc in &scs {
subcmd_dets = format!(
"{}
{subcmd})
opts=\"{sc_opts}\"
if [[ ${{cur}} == -* || ${{COMP_CWORD}} -eq {level} ]] ; then
COMPREPLY=( $(compgen -W \"${{opts}}\" -- ${{cur}}) )
return 0
fi
case \"${{prev}}\" in
{opts_details}
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W \"${{opts}}\" -- ${{cur}}) )
return 0
;;",
subcmd_dets,
subcmd = sc.replace("-", "__"),
sc_opts = self.all_options_for_path(&*sc),
level = sc.split("__").map(|_| 1).fold(0, |acc, n| acc + n),
opts_details = self.option_details_for_path(&*sc)
);
}
subcmd_dets
}
fn option_details_for_path(&self, path: &str) -> String {
debugln!("BashGen::option_details_for_path: path={}", path);
let mut p = self.0;
for sc in path.split("__").skip(1) {
debugln!("BashGen::option_details_for_path:iter: sc={}", sc);
p = &find_subcmd!(p, sc).unwrap();
}
let mut opts = String::new();
for o in opts!(p) {
if let Some(l) = o.long {
opts = format!(
"{}
--{})
COMPREPLY=({})
return 0
;;",
opts,
l,
self.vals_for(o)
);
}
if let Some(s) = o.short {
opts = format!(
"{}
-{})
COMPREPLY=({})
return 0
;;",
opts,
s,
self.vals_for(o)
);
}
}
opts
}
fn vals_for(&self, o: &Arg) -> String {
debugln!("BashGen::vals_for: o={}", o.name);
if let Some(ref vals) = o.possible_vals {
format!("$(compgen -W \"{}\" -- ${{cur}})", vals.join(" "))
} else {
String::from("$(compgen -f ${cur})")
}
}
fn all_options_for_path(&self, path: &str) -> String {
debugln!("BashGen::all_options_for_path: path={}", path);
let mut p = self.0;
for sc in path.split("__").skip(1) {
debugln!("BashGen::all_options_for_path:iter: sc={}", sc);
p = &find_subcmd!(p, sc).unwrap();
}
let opts = format!(
"{shorts} {longs} {pos} {subcmds}",
shorts = shorts!(p).fold(String::new(), |acc, s| format!("{} -{}", acc, s)),
// Handles aliases too
longs = longs!(p).fold(String::new(), |acc, l| format!("{} --{}", acc, l)),
pos = positionals!(p).fold(String::new(), |acc, p| format!("{} {}", acc, p)),
// Handles aliases too
subcmds = sc_names!(p).fold(String::new(), |acc, s| format!("{} {}", acc, s))
);
opts
}
}

View file

@ -1,129 +0,0 @@
// Std
use std::io::Write;
// Internal
use build::App;
use INTERNAL_ERROR_MSG;
pub struct ElvishGen<'a, 'b, 'c>
where
'a: 'b,
'b: 'c,
{
app: &'c App<'a, 'b>,
}
impl<'a, 'b, 'c> ElvishGen<'a, 'b, 'c> {
pub fn new(p: &'c App<'a, 'b>) -> Self { ElvishGen { app: p } }
pub fn generate_to<W: Write>(&self, buf: &mut W) {
let bin_name = self.app.bin_name.as_ref().unwrap();
let mut names = vec![];
let subcommands_cases = generate_inner(self.app, "", &mut names);
let result = format!(
r#"
edit:completion:arg-completer[{bin_name}] = [@words]{{
fn spaces [n]{{
repeat $n ' ' | joins ''
}}
fn cand [text desc]{{
edit:complex-candidate $text &display-suffix=' '(spaces (- 14 (wcswidth $text)))$desc
}}
command = '{bin_name}'
for word $words[1:-1] {{
if (has-prefix $word '-') {{
break
}}
command = $command';'$word
}}
completions = [{subcommands_cases}
]
$completions[$command]
}}
"#,
bin_name = bin_name,
subcommands_cases = subcommands_cases
);
w!(buf, result.as_bytes());
}
}
// Escape string inside single quotes
fn escape_string(string: &str) -> String { string.replace("'", "''") }
fn get_tooltip<T: ToString>(help: Option<&str>, data: T) -> String {
match help {
Some(help) => escape_string(help),
_ => data.to_string(),
}
}
fn generate_inner<'a, 'b, 'c>(
p: &'c App<'a, 'b>,
previous_command_name: &str,
names: &mut Vec<&'a str>,
) -> String
where
'a: 'b,
'b: 'c,
{
debugln!("ElvishGen::generate_inner;");
let command_name = if previous_command_name.is_empty() {
p.bin_name.as_ref().expect(INTERNAL_ERROR_MSG).clone()
} else {
format!("{};{}", previous_command_name, &p.name)
};
let mut completions = String::new();
let preamble = String::from("\n cand ");
for option in opts!(p) {
if let Some(data) = option.short {
let tooltip = get_tooltip(option.help, data);
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", data, tooltip).as_str());
}
if let Some(data) = option.long {
let tooltip = get_tooltip(option.help, data);
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", data, tooltip).as_str());
}
}
for flag in flags!(p) {
if let Some(data) = flag.short {
let tooltip = get_tooltip(flag.help, data);
completions.push_str(&preamble);
completions.push_str(format!("-{} '{}'", data, tooltip).as_str());
}
if let Some(data) = flag.long {
let tooltip = get_tooltip(flag.help, data);
completions.push_str(&preamble);
completions.push_str(format!("--{} '{}'", data, tooltip).as_str());
}
}
for subcommand in &p.subcommands {
let data = &subcommand.name;
let tooltip = get_tooltip(subcommand.about, data);
completions.push_str(&preamble);
completions.push_str(format!("{} '{}'", data, tooltip).as_str());
}
let mut subcommands_cases = format!(
r"
&'{}'= {{{}
}}",
&command_name, completions
);
for subcommand in &p.subcommands {
let subcommand_subcommands_cases = generate_inner(&subcommand, &command_name, names);
subcommands_cases.push_str(&subcommand_subcommands_cases);
}
subcommands_cases
}

View file

@ -1,96 +0,0 @@
// Std
use std::io::Write;
// Internal
use build::App;
pub struct FishGen<'a, 'b>(&'b App<'a, 'b>)
where
'a: 'b;
impl<'a, 'b> FishGen<'a, 'b> {
pub fn new(app: &'b App<'a, 'b>) -> Self { FishGen(app) }
pub fn generate_to<W: Write>(&self, buf: &mut W) {
let command = self.0.bin_name.as_ref().unwrap();
let mut buffer = String::new();
gen_fish_inner(command, self, command, &mut buffer);
w!(buf, buffer.as_bytes());
}
}
// Escape string inside single quotes
fn escape_string(string: &str) -> String { string.replace("\\", "\\\\").replace("'", "\\'") }
fn gen_fish_inner(root_command: &str, comp_gen: &FishGen, subcommand: &str, buffer: &mut String) {
debugln!("FishGen::gen_fish_inner;");
// example :
//
// complete
// -c {command}
// -d "{description}"
// -s {short}
// -l {long}
// -a "{possible_arguments}"
// -r # if require parameter
// -f # don't use file completion
// -n "__fish_use_subcommand" # complete for command "myprog"
// -n "__fish_seen_subcommand_from subcmd1" # complete for command "myprog subcmd1"
let mut basic_template = format!("complete -c {} -n ", root_command);
if root_command == subcommand {
basic_template.push_str("\"__fish_use_subcommand\"");
} else {
basic_template.push_str(format!("\"__fish_seen_subcommand_from {}\"", subcommand).as_str());
}
for option in opts!(comp_gen.0) {
let mut template = basic_template.clone();
if let Some(data) = option.short {
template.push_str(format!(" -s {}", data).as_str());
}
if let Some(data) = option.long {
template.push_str(format!(" -l {}", data).as_str());
}
if let Some(data) = option.help {
template.push_str(format!(" -d '{}'", escape_string(data)).as_str());
}
if let Some(ref data) = option.possible_vals {
template.push_str(format!(" -r -f -a \"{}\"", data.join(" ")).as_str());
}
buffer.push_str(template.as_str());
buffer.push_str("\n");
}
for flag in flags!(comp_gen.0) {
let mut template = basic_template.clone();
if let Some(data) = flag.short {
template.push_str(format!(" -s {}", data).as_str());
}
if let Some(data) = flag.long {
template.push_str(format!(" -l {}", data).as_str());
}
if let Some(data) = flag.help {
template.push_str(format!(" -d '{}'", escape_string(data)).as_str());
}
buffer.push_str(template.as_str());
buffer.push_str("\n");
}
for subcommand in subcommands!(comp_gen.0) {
let mut template = basic_template.clone();
template.push_str(" -f");
template.push_str(format!(" -a \"{}\"", &subcommand.name).as_str());
if let Some(data) = subcommand.about {
template.push_str(format!(" -d '{}'", escape_string(data)).as_str())
}
buffer.push_str(template.as_str());
buffer.push_str("\n");
}
// generate options of subcommands
for subcommand in &comp_gen.0.subcommands {
let sub_comp_gen = FishGen::new(&subcommand);
gen_fish_inner(root_command, &sub_comp_gen, &subcommand.to_string(), buffer);
}
}

View file

@ -1,28 +0,0 @@
macro_rules! w {
($buf:expr, $to_w:expr) => {
match $buf.write_all($to_w) {
Ok(..) => (),
Err(..) => panic!("Failed to write to completions file"),
}
};
}
macro_rules! get_zsh_arg_conflicts {
($app:expr, $arg:ident, $msg:ident) => {
if let Some(ref conf_vec) = $arg.blacklist {
let mut v = vec![];
for arg_name in conf_vec {
let arg = find!($app, arg_name).expect($msg);
if let Some(s) = arg.short {
v.push(format!("-{}", s));
}
if let Some(l) = arg.long {
v.push(format!("--{}", l));
}
}
v.join(" ")
} else {
String::new()
}
};
}

View file

@ -1,163 +0,0 @@
#[macro_use]
mod macros;
mod bash;
mod elvish;
mod fish;
mod powershell;
mod shell;
mod zsh;
// Std
use std::io::Write;
// Internal
use self::bash::BashGen;
use self::elvish::ElvishGen;
use self::fish::FishGen;
use self::powershell::PowerShellGen;
pub use self::shell::Shell;
use self::zsh::ZshGen;
use build::App;
pub struct ComplGen<'a, 'b>(&'b App<'a, 'b>)
where
'a: 'b;
impl<'a, 'b> ComplGen<'a, 'b> {
pub fn new(app: &'b App<'a, 'b>) -> Self { ComplGen(app) }
pub fn generate<W: Write>(&self, for_shell: Shell, buf: &mut W) {
match for_shell {
Shell::Bash => BashGen::new(self.0).generate_to(buf),
Shell::Fish => FishGen::new(self.0).generate_to(buf),
Shell::Zsh => ZshGen::new(self.0).generate_to(buf),
Shell::PowerShell => PowerShellGen::new(self.0).generate_to(buf),
Shell::Elvish => ElvishGen::new(self.0).generate_to(buf),
_ => panic!("Unsupported shell type for generating completions"),
}
}
}
// Gets all subcommands including child subcommands in the form of 'name' where the name
// is a single word (i.e. "install") of the path to said subcommand (i.e.
// "rustup toolchain install")
//
// Also note, aliases are treated as their own subcommands but duplicates of whatever they're
// aliasing.
pub fn all_subcommand_names(p: &App) -> Vec<String> {
debugln!("all_subcommand_names;");
let mut subcmds: Vec<_> = subcommands_of(p)
.iter()
.map(|&(ref n, _)| n.clone())
.collect();
for sc_v in subcommands!(p).map(|s| all_subcommand_names(&s)) {
subcmds.extend(sc_v);
}
subcmds.sort();
subcmds.dedup();
subcmds
}
// Gets all subcommands including child subcommands in the form of ('name', 'bin_name') where the name
// is a single word (i.e. "install") of the path and full bin_name of said subcommand (i.e.
// "rustup toolchain install")
//
// Also note, aliases are treated as their own subcommands but duplicates of whatever they're
// aliasing.
pub fn all_subcommands(p: &App) -> Vec<(String, String)> {
debugln!("all_subcommands;");
let mut subcmds: Vec<_> = subcommands_of(p);
for sc_v in subcommands!(p).map(|s| all_subcommands(&s)) {
subcmds.extend(sc_v);
}
subcmds
}
// Gets all subcommands exlcuding child subcommands in the form of (name, bin_name) where the name
// is a single word (i.e. "install") and the bin_name is a space deliniated list of the path to said
// subcommand (i.e. "rustup toolchain install")
//
// Also note, aliases are treated as their own subcommands but duplicates of whatever they're
// aliasing.
pub fn subcommands_of(p: &App) -> Vec<(String, String)> {
debugln!(
"subcommands_of: name={}, bin_name={}",
p.name,
p.bin_name.as_ref().unwrap()
);
let mut subcmds = vec![];
debugln!(
"subcommands_of: Has subcommands...{:?}",
p.has_subcommands()
);
if !p.has_subcommands() {
let mut ret = vec![];
debugln!("subcommands_of: Looking for aliases...");
if let Some(ref aliases) = p.aliases {
for &(n, _) in aliases {
debugln!("subcommands_of:iter:iter: Found alias...{}", n);
let mut als_bin_name: Vec<_> = p.bin_name.as_ref().unwrap().split(' ').collect();
als_bin_name.push(n);
let old = als_bin_name.len() - 2;
als_bin_name.swap_remove(old);
ret.push((n.to_owned(), als_bin_name.join(" ")));
}
}
return ret;
}
for sc in subcommands!(p) {
debugln!(
"subcommands_of:iter: name={}, bin_name={}",
sc.name,
sc.bin_name.as_ref().unwrap()
);
debugln!("subcommands_of:iter: Looking for aliases...");
if let Some(ref aliases) = sc.aliases {
for &(n, _) in aliases {
debugln!("subcommands_of:iter:iter: Found alias...{}", n);
let mut als_bin_name: Vec<_> = p.bin_name.as_ref().unwrap().split(' ').collect();
als_bin_name.push(n);
let old = als_bin_name.len() - 2;
als_bin_name.swap_remove(old);
subcmds.push((n.to_owned(), als_bin_name.join(" ")));
}
}
subcmds.push((sc.name.clone(), sc.bin_name.as_ref().unwrap().clone()));
}
subcmds
}
pub fn get_all_subcommand_paths(p: &App, first: bool) -> Vec<String> {
debugln!("get_all_subcommand_paths;");
let mut subcmds = vec![];
if !p.has_subcommands() {
if !first {
let name = &*p.name;
let path = p.bin_name.as_ref().unwrap().clone().replace(" ", "__");
let mut ret = vec![path.clone()];
if let Some(ref aliases) = p.aliases {
for &(n, _) in aliases {
ret.push(path.replace(name, n));
}
}
return ret;
}
return vec![];
}
for sc in subcommands!(p) {
let name = &*sc.name;
let path = sc.bin_name.as_ref().unwrap().clone().replace(" ", "__");
subcmds.push(path.clone());
if let Some(ref aliases) = sc.aliases {
for &(n, _) in aliases {
subcmds.push(path.replace(name, n));
}
}
}
for sc_v in subcommands!(p).map(|s| get_all_subcommand_paths(&s, false)) {
subcmds.extend(sc_v);
}
subcmds
}

View file

@ -1,154 +0,0 @@
// Std
use std::io::Write;
// Internal
use build::App;
use INTERNAL_ERROR_MSG;
pub struct PowerShellGen<'a, 'b>(&'b App<'a, 'b>)
where
'a: 'b;
impl<'a, 'b> PowerShellGen<'a, 'b> {
pub fn new(app: &'b App<'a, 'b>) -> Self { PowerShellGen(app) }
pub fn generate_to<W: Write>(&self, buf: &mut W) {
let bin_name = self.0.bin_name.as_ref().unwrap();
let mut names = vec![];
let subcommands_cases = generate_inner(self.0, "", &mut names);
let result = format!(
r#"
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Register-ArgumentCompleter -Native -CommandName '{bin_name}' -ScriptBlock {{
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$command = @(
'{bin_name}'
for ($i = 1; $i -lt $commandElements.Count; $i++) {{
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-')) {{
break
}}
$element.Value
}}) -join ';'
$completions = @(switch ($command) {{{subcommands_cases}
}})
$completions.Where{{ $_.CompletionText -like "$wordToComplete*" }} |
Sort-Object -Property ListItemText
}}
"#,
bin_name = bin_name,
subcommands_cases = subcommands_cases
);
w!(buf, result.as_bytes());
}
}
// Escape string inside single quotes
fn escape_string(string: &str) -> String { string.replace("'", "''") }
fn get_tooltip<T: ToString>(help: Option<&str>, data: T) -> String {
match help {
Some(help) => escape_string(&help),
_ => data.to_string(),
}
}
fn generate_inner<'a, 'b, 'p>(
p: &'p App<'a, 'b>,
previous_command_name: &str,
names: &mut Vec<&'p str>,
) -> String {
debugln!("PowerShellGen::generate_inner;");
let command_name = if previous_command_name.is_empty() {
p.bin_name.as_ref().expect(INTERNAL_ERROR_MSG).clone()
} else {
format!("{};{}", previous_command_name, &p.name)
};
let mut completions = String::new();
let preamble = String::from("\n [CompletionResult]::new(");
for option in opts!(p) {
if let Some(data) = option.short {
let tooltip = get_tooltip(option.help, data);
completions.push_str(&preamble);
completions.push_str(
format!(
"'-{}', '{}', {}, '{}')",
data, data, "[CompletionResultType]::ParameterName", tooltip
).as_str(),
);
}
if let Some(data) = option.long {
let tooltip = get_tooltip(option.help, data);
completions.push_str(&preamble);
completions.push_str(
format!(
"'--{}', '{}', {}, '{}')",
data, data, "[CompletionResultType]::ParameterName", tooltip
).as_str(),
);
}
}
for flag in flags!(p) {
if let Some(data) = flag.short {
let tooltip = get_tooltip(flag.help, data);
completions.push_str(&preamble);
completions.push_str(
format!(
"'-{}', '{}', {}, '{}')",
data, data, "[CompletionResultType]::ParameterName", tooltip
).as_str(),
);
}
if let Some(data) = flag.long {
let tooltip = get_tooltip(flag.help, data);
completions.push_str(&preamble);
completions.push_str(
format!(
"'--{}', '{}', {}, '{}')",
data, data, "[CompletionResultType]::ParameterName", tooltip
).as_str(),
);
}
}
for subcommand in subcommands!(p) {
let data = &subcommand.name;
let tooltip = get_tooltip(subcommand.about, data);
completions.push_str(&preamble);
completions.push_str(
format!(
"'{}', '{}', {}, '{}')",
data, data, "[CompletionResultType]::ParameterValue", tooltip
).as_str(),
);
}
let mut subcommands_cases = format!(
r"
'{}' {{{}
break
}}",
&command_name, completions
);
for subcommand in &p.subcommands {
let subcommand_subcommands_cases = generate_inner(&subcommand, &command_name, names);
subcommands_cases.push_str(&subcommand_subcommands_cases);
}
subcommands_cases
}

View file

@ -1,57 +0,0 @@
#[allow(unused_imports)]
use std::ascii::AsciiExt;
use std::fmt;
use std::str::FromStr;
/// Describes which shell to produce a completions file for
#[cfg_attr(feature = "lints", allow(enum_variant_names))]
#[derive(Debug, Copy, Clone)]
pub enum Shell {
/// Generates a .bash completion file for the Bourne Again SHell (BASH)
Bash,
/// Generates a .fish completion file for the Friendly Interactive SHell (fish)
Fish,
/// Generates a completion file for the Z SHell (ZSH)
Zsh,
/// Generates a completion file for PowerShell
PowerShell,
/// Generates a completion file for Elvish
Elvish,
#[doc(hidden)]
__Nonexhaustive,
}
impl Shell {
/// A list of possible variants in `&'static str` form
pub fn variants() -> [&'static str; 5] { ["zsh", "bash", "fish", "powershell", "elvish"] }
}
impl FromStr for Shell {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"ZSH" | _ if s.eq_ignore_ascii_case("zsh") => Ok(Shell::Zsh),
"FISH" | _ if s.eq_ignore_ascii_case("fish") => Ok(Shell::Fish),
"BASH" | _ if s.eq_ignore_ascii_case("bash") => Ok(Shell::Bash),
"POWERSHELL" | _ if s.eq_ignore_ascii_case("powershell") => Ok(Shell::PowerShell),
"ELVISH" | _ if s.eq_ignore_ascii_case("elvish") => Ok(Shell::Elvish),
_ => Err(String::from(
"[valid values: bash, fish, zsh, powershell, elvish]",
)),
}
}
}
impl fmt::Display for Shell {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Shell::Bash => write!(f, "BASH"),
Shell::Fish => write!(f, "FISH"),
Shell::Zsh => write!(f, "ZSH"),
Shell::PowerShell => write!(f, "POWERSHELL"),
Shell::Elvish => write!(f, "ELVISH"),
_ => panic!("Unsupported shell type for completion generation"),
}
}
}

View file

@ -1,485 +0,0 @@
// Std
#[allow(unused_imports)]
use std::ascii::AsciiExt;
use std::io::Write;
// Internal
use build::{App, ArgSettings};
use completions;
use INTERNAL_ERROR_MSG;
pub struct ZshGen<'a, 'b>(&'b App<'a, 'b>)
where
'a: 'b;
impl<'a, 'b> ZshGen<'a, 'b> {
pub fn new(app: &'b App<'a, 'b>) -> Self {
debugln!("ZshGen::new;");
ZshGen(app)
}
pub fn generate_to<W: Write>(&self, buf: &mut W) {
debugln!("ZshGen::generate_to;");
w!(
buf,
format!(
"\
#compdef {name}
autoload -U is-at-least
_{name}() {{
typeset -A opt_args
typeset -a _arguments_options
local ret=1
if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi
local context curcontext=\"$curcontext\" state line
{initial_args}
{subcommands}
}}
{subcommand_details}
_{name} \"$@\"",
name = self.0.bin_name.as_ref().unwrap(),
initial_args = get_args_of(self.0),
subcommands = get_subcommands_of(self.0),
subcommand_details = subcommand_details(self.0)
).as_bytes()
);
}
}
// Displays the commands of a subcommand
// (( $+functions[_[bin_name_underscore]_commands] )) ||
// _[bin_name_underscore]_commands() {
// local commands; commands=(
// '[arg_name]:[arg_help]'
// )
// _describe -t commands '[bin_name] commands' commands "$@"
//
// Where the following variables are present:
// [bin_name_underscore]: The full space deliniated bin_name, where spaces have been replaced by
// underscore characters
// [arg_name]: The name of the subcommand
// [arg_help]: The help message of the subcommand
// [bin_name]: The full space deliniated bin_name
//
// Here's a snippet from rustup:
//
// (( $+functions[_rustup_commands] )) ||
// _rustup_commands() {
// local commands; commands=(
// 'show:Show the active and installed toolchains'
// 'update:Update Rust toolchains'
// # ... snip for brevity
// 'help:Prints this message or the help of the given subcommand(s)'
// )
// _describe -t commands 'rustup commands' commands "$@"
//
fn subcommand_details(p: &App) -> String {
debugln!("ZshGen::subcommand_details;");
// First we do ourself
let mut ret = vec![format!(
"\
(( $+functions[_{bin_name_underscore}_commands] )) ||
_{bin_name_underscore}_commands() {{
local commands; commands=(
{subcommands_and_args}
)
_describe -t commands '{bin_name} commands' commands \"$@\"
}}",
bin_name_underscore = p.bin_name.as_ref().unwrap().replace(" ", "__"),
bin_name = p.bin_name.as_ref().unwrap(),
subcommands_and_args = subcommands_of(p)
)];
// Next we start looping through all the children, grandchildren, etc.
let mut all_subcommands = completions::all_subcommands(p);
all_subcommands.sort();
all_subcommands.dedup();
for &(_, ref bin_name) in &all_subcommands {
debugln!("ZshGen::subcommand_details:iter: bin_name={}", bin_name);
ret.push(format!(
"\
(( $+functions[_{bin_name_underscore}_commands] )) ||
_{bin_name_underscore}_commands() {{
local commands; commands=(
{subcommands_and_args}
)
_describe -t commands '{bin_name} commands' commands \"$@\"
}}",
bin_name_underscore = bin_name.replace(" ", "__"),
bin_name = bin_name,
subcommands_and_args = subcommands_of(parser_of(p, bin_name))
));
}
ret.join("\n")
}
// Generates subcommand completions in form of
//
// '[arg_name]:[arg_help]'
//
// Where:
// [arg_name]: the subcommand's name
// [arg_help]: the help message of the subcommand
//
// A snippet from rustup:
// 'show:Show the active and installed toolchains'
// 'update:Update Rust toolchains'
fn subcommands_of(p: &App) -> String {
debugln!("ZshGen::subcommands_of;");
let mut ret = vec![];
fn add_sc(sc: &App, n: &str, ret: &mut Vec<String>) {
debugln!("ZshGen::add_sc;");
let s = format!(
"\"{name}:{help}\" \\",
name = n,
help = sc
.about
.unwrap_or("")
.replace("[", "\\[")
.replace("]", "\\]")
);
if !s.is_empty() {
ret.push(s);
}
}
// The subcommands
for sc in subcommands!(p) {
debugln!("ZshGen::subcommands_of:iter: subcommand={}", sc.name);
add_sc(sc, &sc.name, &mut ret);
if let Some(ref v) = sc.aliases {
for alias in v.iter().filter(|&&(_, vis)| vis).map(|&(n, _)| n) {
add_sc(sc, alias, &mut ret);
}
}
}
ret.join("\n")
}
// Get's the subcommand section of a completion file
// This looks roughly like:
//
// case $state in
// ([bin_name]_args)
// curcontext=\"${curcontext%:*:*}:[name_hyphen]-command-$words[1]:\"
// case $line[1] in
//
// ([name])
// _arguments -C -s -S \
// [subcommand_args]
// && ret=0
//
// [RECURSIVE_CALLS]
//
// ;;",
//
// [repeat]
//
// esac
// ;;
// esac",
//
// Where the following variables are present:
// [name] = The subcommand name in the form of "install" for "rustup toolchain install"
// [bin_name] = The full space deliniated bin_name such as "rustup toolchain install"
// [name_hyphen] = The full space deliniated bin_name, but replace spaces with hyphens
// [repeat] = From the same recursive calls, but for all subcommands
// [subcommand_args] = The same as zsh::get_args_of
fn get_subcommands_of(p: &App) -> String {
debugln!("get_subcommands_of;");
debugln!(
"get_subcommands_of: Has subcommands...{:?}",
p.has_subcommands()
);
if !p.has_subcommands() {
return String::new();
}
let sc_names = completions::subcommands_of(p);
let mut subcmds = vec![];
for &(ref name, ref bin_name) in &sc_names {
let mut v = vec![format!("({})", name)];
let subcommand_args = get_args_of(parser_of(p, &*bin_name));
if !subcommand_args.is_empty() {
v.push(subcommand_args);
}
let subcommands = get_subcommands_of(parser_of(p, &*bin_name));
if !subcommands.is_empty() {
v.push(subcommands);
}
v.push(String::from(";;"));
subcmds.push(v.join("\n"));
}
format!(
"case $state in
({name})
words=($line[{pos}] \"${{words[@]}}\")
(( CURRENT += 1 ))
curcontext=\"${{curcontext%:*:*}}:{name_hyphen}-command-$line[{pos}]:\"
case $line[{pos}] in
{subcommands}
esac
;;
esac",
name = p.name,
name_hyphen = p.bin_name.as_ref().unwrap().replace(" ", "-"),
subcommands = subcmds.join("\n"),
pos = positionals!(p).count() + 1
)
}
fn parser_of<'a, 'b>(p: &'b App<'a, 'b>, mut sc: &str) -> &'b App<'a, 'b> {
debugln!("parser_of: sc={}", sc);
if sc == p.bin_name.as_ref().unwrap_or(&String::new()) {
return p;
}
sc = sc.split(" ").last().unwrap();
find_subcmd!(p, sc).expect(INTERNAL_ERROR_MSG)
}
// Writes out the args section, which ends up being the flags, opts and postionals, and a jump to
// another ZSH function if there are subcommands.
// The structer works like this:
// ([conflicting_args]) [multiple] arg [takes_value] [[help]] [: :(possible_values)]
// ^-- list '-v -h' ^--'*' ^--'+' ^-- list 'one two three'
//
// An example from the rustup command:
//
// _arguments -C -s -S \
// '(-h --help --verbose)-v[Enable verbose output]' \
// '(-V -v --version --verbose --help)-h[Prints help information]' \
// # ... snip for brevity
// ':: :_rustup_commands' \ # <-- displays subcommands
// '*::: :->rustup' \ # <-- displays subcommand args and child subcommands
// && ret=0
//
// The args used for _arguments are as follows:
// -C: modify the $context internal variable
// -s: Allow stacking of short args (i.e. -a -b -c => -abc)
// -S: Do not complete anything after '--' and treat those as argument values
fn get_args_of(p: &App) -> String {
debugln!("get_args_of;");
let mut ret = vec![String::from("_arguments \"${_arguments_options[@]}\" \\")];
let opts = write_opts_of(p);
let flags = write_flags_of(p);
let positionals = write_positionals_of(p);
let sc_or_a = if p.has_subcommands() {
format!(
"\":: :_{name}_commands\" \\",
name = p.bin_name.as_ref().unwrap().replace(" ", "__")
)
} else {
String::new()
};
let sc = if p.has_subcommands() {
format!("\"*::: :->{name}\" \\", name = p.name)
} else {
String::new()
};
if !opts.is_empty() {
ret.push(opts);
}
if !flags.is_empty() {
ret.push(flags);
}
if !positionals.is_empty() {
ret.push(positionals);
}
if !sc_or_a.is_empty() {
ret.push(sc_or_a);
}
if !sc.is_empty() {
ret.push(sc);
}
ret.push(String::from("&& ret=0"));
ret.join("\n")
}
// Escape help string inside single quotes and brackets
fn escape_help(string: &str) -> String {
string
.replace("\\", "\\\\")
.replace("'", "'\\''")
.replace("[", "\\[")
.replace("]", "\\]")
}
// Escape value string inside single quotes and parentheses
fn escape_value(string: &str) -> String {
string
.replace("\\", "\\\\")
.replace("'", "'\\''")
.replace("(", "\\(")
.replace(")", "\\)")
.replace(" ", "\\ ")
}
fn write_opts_of(p: &App) -> String {
debugln!("write_opts_of;");
let mut ret = vec![];
for o in opts!(p) {
debugln!("write_opts_of:iter: o={}", o.name);
let help = o.help.map_or(String::new(), escape_help);
let mut conflicts = get_zsh_arg_conflicts!(p, o, INTERNAL_ERROR_MSG);
conflicts = if conflicts.is_empty() {
String::new()
} else {
format!("({})", conflicts)
};
// @TODO @soundness should probably be either multiple occurrences or multiple values and
// not both
let multiple = if o.is_set(ArgSettings::MultipleOccurrences)
|| o.is_set(ArgSettings::MultipleValues)
{
"*"
} else {
""
};
let pv = if let Some(ref pv_vec) = o.possible_vals {
format!(
": :({})",
pv_vec
.iter()
.map(|v| escape_value(*v))
.collect::<Vec<String>>()
.join(" ")
)
} else {
String::new()
};
if let Some(short) = o.short {
let s = format!(
"'{conflicts}{multiple}-{arg}+[{help}]{possible_values}' \\",
conflicts = conflicts,
multiple = multiple,
arg = short,
possible_values = pv,
help = help
);
debugln!("write_opts_of:iter: Wrote...{}", &*s);
ret.push(s);
}
if let Some(long) = o.long {
let l = format!(
"'{conflicts}{multiple}--{arg}=[{help}]{possible_values}' \\",
conflicts = conflicts,
multiple = multiple,
arg = long,
possible_values = pv,
help = help
);
debugln!("write_opts_of:iter: Wrote...{}", &*l);
ret.push(l);
}
}
ret.join("\n")
}
fn write_flags_of(p: &App) -> String {
debugln!("write_flags_of;");
let mut ret = vec![];
for f in flags!(p) {
debugln!("write_flags_of:iter: f={}", f.name);
let help = f.help.map_or(String::new(), escape_help);
let mut conflicts = get_zsh_arg_conflicts!(p, f, INTERNAL_ERROR_MSG);
conflicts = if conflicts.is_empty() {
String::new()
} else {
format!("({})", conflicts)
};
let multiple = if f.is_set(ArgSettings::MultipleOccurrences) {
"*"
} else {
""
};
if let Some(short) = f.short {
let s = format!(
"'{conflicts}{multiple}-{arg}[{help}]' \\",
multiple = multiple,
conflicts = conflicts,
arg = short,
help = help
);
debugln!("write_flags_of:iter: Wrote...{}", &*s);
ret.push(s);
}
if let Some(long) = f.long {
let l = format!(
"'{conflicts}{multiple}--{arg}[{help}]' \\",
conflicts = conflicts,
multiple = multiple,
arg = long,
help = help
);
debugln!("write_flags_of:iter: Wrote...{}", &*l);
ret.push(l);
}
}
ret.join("\n")
}
fn write_positionals_of(p: &App) -> String {
debugln!("write_positionals_of;");
let mut ret = vec![];
for arg in positionals!(p) {
debugln!("write_positionals_of:iter: arg={}", arg.name);
let a = format!(
"'{optional}:{name}{help}:{action}' \\",
optional = if !arg.is_set(ArgSettings::Required) {
":"
} else {
""
},
name = arg.name,
help = arg
.help
.map_or("".to_owned(), |v| " -- ".to_owned() + v)
.replace("[", "\\[")
.replace("]", "\\]"),
action = arg
.possible_vals
.as_ref()
.map_or("_files".to_owned(), |values| {
format!(
"({})",
values
.iter()
.map(|v| escape_value(*v))
.collect::<Vec<String>>()
.join(" ")
)
})
);
debugln!("write_positionals_of:iter: Wrote...{}", a);
ret.push(a);
}
ret.join("\n")
}

View file

@ -46,7 +46,7 @@
//! // more verbose, but allows easier editing, and at times more advanced options, or the possibility //! // more verbose, but allows easier editing, and at times more advanced options, or the possibility
//! // to generate arguments dynamically. //! // to generate arguments dynamically.
//! extern crate clap; //! extern crate clap;
//! use clap::{Arg, App, SubCommand}; //! use clap::{Arg, App, };
//! //!
//! fn main() { //! fn main() {
//! let matches = App::new("My Super Program") //! let matches = App::new("My Super Program")
@ -67,7 +67,7 @@
//! .short('v') //! .short('v')
//! .multiple(true) //! .multiple(true)
//! .help("Sets the level of verbosity")) //! .help("Sets the level of verbosity"))
//! .subcommand(SubCommand::with_name("test") //! .subcommand(App::new("test")
//! .about("controls testing features") //! .about("controls testing features")
//! .version("1.3") //! .version("1.3")
//! .author("Someone E. <someone_else@other.com>") //! .author("Someone E. <someone_else@other.com>")
@ -117,22 +117,21 @@
//! // This example demonstrates clap's "usage strings" method of creating arguments //! // This example demonstrates clap's "usage strings" method of creating arguments
//! // which is less verbose //! // which is less verbose
//! extern crate clap; //! extern crate clap;
//! use clap::{Arg, App, SubCommand}; //! use clap::{Arg, App, };
//! //!
//! fn main() { //! fn main() {
//! let matches = App::new("myapp") //! let matches = App::new("myapp")
//! .version("1.0") //! .version("1.0")
//! .author("Kevin K. <kbknapp@gmail.com>") //! .author("Kevin K. <kbknapp@gmail.com>")
//! .about("Does awesome things") //! .about("Does awesome things")
//! .args_from_usage( //! .arg("-c, --config=[FILE] 'Sets a custom config file'")
//! "-c, --config=[FILE] 'Sets a custom config file' //! .arg("<INPUT> 'Sets the input file to use'")
//! <INPUT> 'Sets the input file to use' //! .arg("-v... 'Sets the level of verbosity'")
//! -v... 'Sets the level of verbosity'") //! .subcommand(App::new("test")
//! .subcommand(SubCommand::with_name("test")
//! .about("controls testing features") //! .about("controls testing features")
//! .version("1.3") //! .version("1.3")
//! .author("Someone E. <someone_else@other.com>") //! .author("Someone E. <someone_else@other.com>")
//! .arg_from_usage("-d, --debug 'Print debug information'")) //! .arg("-d, --debug 'Print debug information'"))
//! .get_matches(); //! .get_matches();
//! //!
//! // Same as previous example... //! // Same as previous example...
@ -517,7 +516,7 @@
//! [license]: https://raw.githubusercontent.com/kbknapp/clap-rs/master/LICENSE-MIT //! [license]: https://raw.githubusercontent.com/kbknapp/clap-rs/master/LICENSE-MIT
#![crate_type = "lib"] #![crate_type = "lib"]
#![doc(html_root_url = "https://docs.rs/clap/3.0.0-alpha.1")] #![doc(html_root_url = "https://docs.rs/clap/3.0.0-beta.1")]
#![deny( #![deny(
missing_docs, missing_docs,
missing_debug_implementations, missing_debug_implementations,
@ -566,10 +565,9 @@ extern crate vec_map;
extern crate yaml_rust; extern crate yaml_rust;
pub use build::{App, AppSettings, Arg, ArgGroup, ArgSettings, Propagation}; pub use build::{App, AppSettings, Arg, ArgGroup, ArgSettings, Propagation};
pub use completions::Shell;
pub use output::fmt::Format; pub use output::fmt::Format;
pub use parse::errors::{Error, ErrorKind, Result}; pub use parse::errors::{Error, ErrorKind, Result};
pub use parse::{ArgMatches, OsValues, SubCommand, Values}; pub use parse::{ArgMatches, OsValues, Values};
#[cfg(feature = "yaml")] #[cfg(feature = "yaml")]
pub use yaml_rust::YamlLoader; pub use yaml_rust::YamlLoader;
@ -582,7 +580,6 @@ use std::result::Result as StdResult;
#[macro_use] #[macro_use]
mod macros; mod macros;
mod build; mod build;
mod completions;
mod output; mod output;
mod parse; mod parse;
mod util; mod util;

View file

@ -46,7 +46,7 @@ macro_rules! load_yaml {
/// # use clap::App; /// # use clap::App;
/// # fn main() { /// # fn main() {
/// let matches = App::new("myapp") /// let matches = App::new("myapp")
/// .arg_from_usage("[length] 'Set the length to use as a pos whole num, i.e. 20'") /// .arg("[length] 'Set the length to use as a pos whole num, i.e. 20'")
/// .get_matches(); /// .get_matches();
/// ///
/// let len = value_t!(matches.value_of("length"), u32).unwrap_or_else(|e| e.exit()); /// let len = value_t!(matches.value_of("length"), u32).unwrap_or_else(|e| e.exit());
@ -92,7 +92,7 @@ macro_rules! value_t {
/// # use clap::App; /// # use clap::App;
/// # fn main() { /// # fn main() {
/// let matches = App::new("myapp") /// let matches = App::new("myapp")
/// .arg_from_usage("[length] 'Set the length to use as a pos whole num, i.e. 20'") /// .arg("[length] 'Set the length to use as a pos whole num, i.e. 20'")
/// .get_matches(); /// .get_matches();
/// ///
/// let len = value_t_or_exit!(matches.value_of("length"), u32); /// let len = value_t_or_exit!(matches.value_of("length"), u32);
@ -136,7 +136,7 @@ macro_rules! value_t_or_exit {
/// # use clap::App; /// # use clap::App;
/// # fn main() { /// # fn main() {
/// let matches = App::new("myapp") /// let matches = App::new("myapp")
/// .arg_from_usage("[seq]... 'A sequence of pos whole nums, i.e. 20 45'") /// .arg("[seq]... 'A sequence of pos whole nums, i.e. 20 45'")
/// .get_matches(); /// .get_matches();
/// ///
/// let vals = values_t!(matches.values_of("seq"), u32).unwrap_or_else(|e| e.exit()); /// let vals = values_t!(matches.values_of("seq"), u32).unwrap_or_else(|e| e.exit());
@ -198,7 +198,7 @@ macro_rules! values_t {
/// # use clap::App; /// # use clap::App;
/// # fn main() { /// # fn main() {
/// let matches = App::new("myapp") /// let matches = App::new("myapp")
/// .arg_from_usage("[seq]... 'A sequence of pos whole nums, i.e. 20 45'") /// .arg("[seq]... 'A sequence of pos whole nums, i.e. 20 45'")
/// .get_matches(); /// .get_matches();
/// ///
/// let vals = values_t_or_exit!(matches.values_of("seq"), u32); /// let vals = values_t_or_exit!(matches.values_of("seq"), u32);
@ -578,7 +578,7 @@ macro_rules! app_from_crate {
}; };
} }
/// Build `App`, `Arg`s, `SubCommand`s and `Group`s with Usage-string like input /// Build `App`, `Arg`s, ``s and `Group`s with Usage-string like input
/// but without the associated parsing runtime cost. /// but without the associated parsing runtime cost.
/// ///
/// `clap_app!` also supports several shorthand syntaxes. /// `clap_app!` also supports several shorthand syntaxes.
@ -701,7 +701,7 @@ macro_rules! clap_app {
(@app ($builder:expr) (@subcommand $name:ident => $($tail:tt)*) $($tt:tt)*) => { (@app ($builder:expr) (@subcommand $name:ident => $($tail:tt)*) $($tt:tt)*) => {
clap_app!{ @app clap_app!{ @app
($builder.subcommand( ($builder.subcommand(
clap_app!{ @app ($crate::SubCommand::with_name(stringify!($name))) $($tail)* } clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)* }
)) ))
$($tt)* $($tt)*
} }
@ -789,7 +789,7 @@ macro_rules! clap_app {
// Build a subcommand outside of an app. // Build a subcommand outside of an app.
(@subcommand $name:ident => $($tail:tt)*) => { (@subcommand $name:ident => $($tail:tt)*) => {
clap_app!{ @app ($crate::SubCommand::with_name(stringify!($name))) $($tail)* } clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)* }
}; };
// Start the magic // Start the magic
(($name:expr) => $($tail:tt)*) => {{ (($name:expr) => $($tail:tt)*) => {{

View file

@ -31,7 +31,7 @@ pub enum ErrorKind {
/// .arg(Arg::with_name("speed") /// .arg(Arg::with_name("speed")
/// .possible_value("fast") /// .possible_value("fast")
/// .possible_value("slow")) /// .possible_value("slow"))
/// .get_matches_from_safe(vec!["prog", "other"]); /// .try_get_matches_from(vec!["prog", "other"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidValue); /// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidValue);
/// ``` /// ```
@ -46,13 +46,13 @@ pub enum ErrorKind {
/// # use clap::{App, Arg, ErrorKind}; /// # use clap::{App, Arg, ErrorKind};
/// let result = App::new("prog") /// let result = App::new("prog")
/// .arg(Arg::from("--flag 'some flag'")) /// .arg(Arg::from("--flag 'some flag'"))
/// .get_matches_from_safe(vec!["prog", "--other"]); /// .try_get_matches_from(vec!["prog", "--other"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::UnknownArgument); /// assert_eq!(result.unwrap_err().kind, ErrorKind::UnknownArgument);
/// ``` /// ```
UnknownArgument, UnknownArgument,
/// Occurs when the user provides an unrecognized [`SubCommand`] which meets the threshold for /// Occurs when the user provides an unrecognized [``] which meets the threshold for
/// being similar enough to an existing subcommand. /// being similar enough to an existing subcommand.
/// If it doesn't meet the threshold, or the 'suggestions' feature is disabled, /// If it doesn't meet the threshold, or the 'suggestions' feature is disabled,
/// the more general [`UnknownArgument`] error is returned. /// the more general [`UnknownArgument`] error is returned.
@ -61,22 +61,22 @@ pub enum ErrorKind {
/// ///
#[cfg_attr(not(feature = "suggestions"), doc = " ```no_run")] #[cfg_attr(not(feature = "suggestions"), doc = " ```no_run")]
#[cfg_attr(feature = "suggestions", doc = " ```")] #[cfg_attr(feature = "suggestions", doc = " ```")]
/// # use clap::{App, Arg, ErrorKind, SubCommand}; /// # use clap::{App, Arg, ErrorKind, };
/// let result = App::new("prog") /// let result = App::new("prog")
/// .subcommand(SubCommand::with_name("config") /// .subcommand(App::new("config")
/// .about("Used for configuration") /// .about("Used for configuration")
/// .arg(Arg::with_name("config_file") /// .arg(Arg::with_name("config_file")
/// .help("The configuration file to use") /// .help("The configuration file to use")
/// .index(1))) /// .index(1)))
/// .get_matches_from_safe(vec!["prog", "confi"]); /// .try_get_matches_from(vec!["prog", "confi"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidSubcommand); /// assert_eq!(result.unwrap_err().kind, ErrorKind::InvalidSubcommand);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`UnknownArgument`]: ./enum.ErrorKind.html#variant.UnknownArgument /// [`UnknownArgument`]: ./enum.ErrorKind.html#variant.UnknownArgument
InvalidSubcommand, InvalidSubcommand,
/// Occurs when the user provides an unrecognized [`SubCommand`] which either /// Occurs when the user provides an unrecognized [``] which either
/// doesn't meet the threshold for being similar enough to an existing subcommand, /// doesn't meet the threshold for being similar enough to an existing subcommand,
/// or the 'suggestions' feature is disabled. /// or the 'suggestions' feature is disabled.
/// Otherwise the more detailed [`InvalidSubcommand`] error is returned. /// Otherwise the more detailed [`InvalidSubcommand`] error is returned.
@ -87,18 +87,18 @@ pub enum ErrorKind {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, ErrorKind, SubCommand}; /// # use clap::{App, Arg, ErrorKind, };
/// let result = App::new("prog") /// let result = App::new("prog")
/// .subcommand(SubCommand::with_name("config") /// .subcommand(App::new("config")
/// .about("Used for configuration") /// .about("Used for configuration")
/// .arg(Arg::with_name("config_file") /// .arg(Arg::with_name("config_file")
/// .help("The configuration file to use") /// .help("The configuration file to use")
/// .index(1))) /// .index(1)))
/// .get_matches_from_safe(vec!["prog", "help", "nothing"]); /// .try_get_matches_from(vec!["prog", "help", "nothing"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand); /// assert_eq!(result.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`InvalidSubcommand`]: ./enum.ErrorKind.html#variant.InvalidSubcommand /// [`InvalidSubcommand`]: ./enum.ErrorKind.html#variant.InvalidSubcommand
/// [`UnknownArgument`]: ./enum.ErrorKind.html#variant.UnknownArgument /// [`UnknownArgument`]: ./enum.ErrorKind.html#variant.UnknownArgument
UnrecognizedSubcommand, UnrecognizedSubcommand,
@ -229,7 +229,7 @@ pub enum ErrorKind {
/// let result = App::new("prog") /// let result = App::new("prog")
/// .arg(Arg::with_name("debug") /// .arg(Arg::with_name("debug")
/// .required(true)) /// .required(true))
/// .get_matches_from_safe(vec!["prog"]); /// .try_get_matches_from(vec!["prog"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingRequiredArgument); /// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
/// ``` /// ```
@ -241,11 +241,11 @@ pub enum ErrorKind {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, AppSettings, SubCommand, ErrorKind}; /// # use clap::{App, AppSettings, ErrorKind};
/// let err = App::new("prog") /// let err = App::new("prog")
/// .setting(AppSettings::SubcommandRequired) /// .setting(AppSettings::SubcommandRequired)
/// .subcommand(SubCommand::with_name("test")) /// .subcommand(App::new("test"))
/// .get_matches_from_safe(vec![ /// .try_get_matches_from(vec![
/// "myprog", /// "myprog",
/// ]); /// ]);
/// assert!(err.is_err()); /// assert!(err.is_err());
@ -255,24 +255,24 @@ pub enum ErrorKind {
/// [`AppSettings::SubcommandRequired`]: ./enum.AppSettings.html#variant.SubcommandRequired /// [`AppSettings::SubcommandRequired`]: ./enum.AppSettings.html#variant.SubcommandRequired
MissingSubcommand, MissingSubcommand,
/// Occurs when either an argument or [`SubCommand`] is required, as defined by /// Occurs when either an argument or [``] is required, as defined by
/// [`AppSettings::ArgRequiredElseHelp`], but the user did not provide one. /// [`AppSettings::ArgRequiredElseHelp`], but the user did not provide one.
/// ///
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, AppSettings, ErrorKind, SubCommand}; /// # use clap::{App, Arg, AppSettings, ErrorKind, };
/// let result = App::new("prog") /// let result = App::new("prog")
/// .setting(AppSettings::ArgRequiredElseHelp) /// .setting(AppSettings::ArgRequiredElseHelp)
/// .subcommand(SubCommand::with_name("config") /// .subcommand(App::new("config")
/// .about("Used for configuration") /// .about("Used for configuration")
/// .arg(Arg::with_name("config_file") /// .arg(Arg::with_name("config_file")
/// .help("The configuration file to use"))) /// .help("The configuration file to use")))
/// .get_matches_from_safe(vec!["prog"]); /// .try_get_matches_from(vec!["prog"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingArgumentOrSubcommand); /// assert_eq!(result.unwrap_err().kind, ErrorKind::MissingArgumentOrSubcommand);
/// ``` /// ```
/// [`SubCommand`]: ./struct.SubCommand.html /// [``]: ./struct..html
/// [`AppSettings::ArgRequiredElseHelp`]: ./enum.AppSettings.html#variant.ArgRequiredElseHelp /// [`AppSettings::ArgRequiredElseHelp`]: ./enum.AppSettings.html#variant.ArgRequiredElseHelp
MissingArgumentOrSubcommand, MissingArgumentOrSubcommand,
@ -286,7 +286,7 @@ pub enum ErrorKind {
/// .arg(Arg::with_name("debug") /// .arg(Arg::with_name("debug")
/// .long("debug") /// .long("debug")
/// .multiple(false)) /// .multiple(false))
/// .get_matches_from_safe(vec!["prog", "--debug", "--debug"]); /// .try_get_matches_from(vec!["prog", "--debug", "--debug"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::UnexpectedMultipleUsage); /// assert_eq!(result.unwrap_err().kind, ErrorKind::UnexpectedMultipleUsage);
/// ``` /// ```
@ -311,7 +311,7 @@ pub enum ErrorKind {
/// .arg(Arg::with_name("utf8") /// .arg(Arg::with_name("utf8")
/// .short('u') /// .short('u')
/// .takes_value(true)) /// .takes_value(true))
/// .get_matches_from_safe(vec![OsString::from("myprog"), /// .try_get_matches_from(vec![OsString::from("myprog"),
/// OsString::from("-u"), /// OsString::from("-u"),
/// OsString::from_vec(vec![0xE9])]); /// OsString::from_vec(vec![0xE9])]);
/// assert!(result.is_err()); /// assert!(result.is_err());
@ -331,7 +331,7 @@ pub enum ErrorKind {
/// ```rust /// ```rust
/// # use clap::{App, Arg, ErrorKind}; /// # use clap::{App, Arg, ErrorKind};
/// let result = App::new("prog") /// let result = App::new("prog")
/// .get_matches_from_safe(vec!["prog", "--help"]); /// .try_get_matches_from(vec!["prog", "--help"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::HelpDisplayed); /// assert_eq!(result.unwrap_err().kind, ErrorKind::HelpDisplayed);
/// ``` /// ```
@ -345,7 +345,7 @@ pub enum ErrorKind {
/// ```rust /// ```rust
/// # use clap::{App, Arg, ErrorKind}; /// # use clap::{App, Arg, ErrorKind};
/// let result = App::new("prog") /// let result = App::new("prog")
/// .get_matches_from_safe(vec!["prog", "--version"]); /// .try_get_matches_from(vec!["prog", "--version"]);
/// assert!(result.is_err()); /// assert!(result.is_err());
/// assert_eq!(result.unwrap_err().kind, ErrorKind::VersionDisplayed); /// assert_eq!(result.unwrap_err().kind, ErrorKind::VersionDisplayed);
/// ``` /// ```

View file

@ -595,11 +595,11 @@ impl<'a> ArgMatches<'a> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let app_m = App::new("myprog") /// let app_m = App::new("myprog")
/// .arg(Arg::with_name("debug") /// .arg(Arg::with_name("debug")
/// .short('d')) /// .short('d'))
/// .subcommand(SubCommand::with_name("test") /// .subcommand(App::new("test")
/// .arg(Arg::with_name("opt") /// .arg(Arg::with_name("opt")
/// .long("option") /// .long("option")
/// .takes_value(true))) /// .takes_value(true)))
@ -616,7 +616,7 @@ impl<'a> ArgMatches<'a> {
/// assert_eq!(sub_m.value_of("opt"), Some("val")); /// assert_eq!(sub_m.value_of("opt"), Some("val"));
/// } /// }
/// ``` /// ```
/// [`Subcommand`]: ./struct.SubCommand.html /// [`Subcommand`]: ./struct..html
/// [`App`]: ./struct.App.html /// [`App`]: ./struct.App.html
/// [`ArgMatches`]: ./struct.ArgMatches.html /// [`ArgMatches`]: ./struct.ArgMatches.html
pub fn subcommand_matches<S: AsRef<str>>(&self, name: S) -> Option<&ArgMatches<'a>> { pub fn subcommand_matches<S: AsRef<str>>(&self, name: S) -> Option<&ArgMatches<'a>> {
@ -669,11 +669,11 @@ impl<'a> ArgMatches<'a> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let app_m = App::new("git") /// let app_m = App::new("git")
/// .subcommand(SubCommand::with_name("clone")) /// .subcommand(App::new("clone"))
/// .subcommand(SubCommand::with_name("push")) /// .subcommand(App::new("push"))
/// .subcommand(SubCommand::with_name("commit")) /// .subcommand(App::new("commit"))
/// .get_matches(); /// .get_matches();
/// ///
/// match app_m.subcommand_name() { /// match app_m.subcommand_name() {
@ -683,7 +683,7 @@ impl<'a> ArgMatches<'a> {
/// _ => {}, // Either no subcommand or one not tested for... /// _ => {}, // Either no subcommand or one not tested for...
/// } /// }
/// ``` /// ```
/// [`Subcommand`]: ./struct.SubCommand.html /// [`Subcommand`]: ./struct..html
/// [`App`]: ./struct.App.html /// [`App`]: ./struct.App.html
/// [`ArgMatches`]: ./struct.ArgMatches.html /// [`ArgMatches`]: ./struct.ArgMatches.html
pub fn subcommand_name(&self) -> Option<&str> { pub fn subcommand_name(&self) -> Option<&str> {
@ -696,11 +696,11 @@ impl<'a> ArgMatches<'a> {
/// # Examples /// # Examples
/// ///
/// ```no_run /// ```no_run
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// let app_m = App::new("git") /// let app_m = App::new("git")
/// .subcommand(SubCommand::with_name("clone")) /// .subcommand(App::new("clone"))
/// .subcommand(SubCommand::with_name("push")) /// .subcommand(App::new("push"))
/// .subcommand(SubCommand::with_name("commit")) /// .subcommand(App::new("commit"))
/// .get_matches(); /// .get_matches();
/// ///
/// match app_m.subcommand() { /// match app_m.subcommand() {

View file

@ -15,10 +15,10 @@ use ArgMatches;
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// # use clap::{App, Arg, SubCommand}; /// # use clap::{App, Arg, };
/// App::new("myprog") /// App::new("myprog")
/// .subcommand( /// .subcommand(
/// SubCommand::with_name("config") /// App::new("config")
/// .about("Used for configuration") /// .about("Used for configuration")
/// .arg(Arg::with_name("config_file") /// .arg(Arg::with_name("config_file")
/// .help("The configuration file to use") /// .help("The configuration file to use")
@ -35,24 +35,3 @@ pub struct SubCommand<'a> {
pub matches: ArgMatches<'a>, pub matches: ArgMatches<'a>,
} }
impl<'a> SubCommand<'a> {
// @TODO-v3-beta: remove
/// **Deprecated**
#[deprecated(
since = "2.32.0",
note = "Use App::new instead. Will be removed in v3-beta"
)]
pub fn with_name<'b>(name: &str) -> App<'a, 'b> { App::new(name) }
// @TODO-v3-beta: remove
/// **Deprecated**
#[cfg_attr(
feature = "yaml",
deprecated(
since = "2.32.0",
note = "Use App::from instead. Will be removed in v3-beta"
)
)]
#[cfg(feature = "yaml")]
pub fn from_yaml(yaml: &Yaml) -> App { App::from_yaml(yaml) }
}

View file

@ -8,6 +8,6 @@ mod validator;
pub use self::arg_matcher::ArgMatcher; pub use self::arg_matcher::ArgMatcher;
pub use self::matches::ArgMatches; pub use self::matches::ArgMatches;
pub use self::matches::{MatchedArg, OsValues, SubCommand, Values}; pub use self::matches::{MatchedArg, OsValues, Values, SubCommand};
pub use self::parser::{ParseResult, Parser}; pub use self::parser::{ParseResult, Parser};
pub use self::validator::Validator; pub use self::validator::Validator;

View file

@ -424,7 +424,7 @@ where
); );
if is_match { if is_match {
let sc_name = sc_name.expect(INTERNAL_ERROR_MSG); let sc_name = sc_name.expect(INTERNAL_ERROR_MSG);
if sc_name == "help" && self.is_set(AS::NeedsSubcommandHelp) { if sc_name == "help" && !self.is_set(AS::NoAutoHelp) {
self.parse_help_subcommand(it)?; self.parse_help_subcommand(it)?;
} }
subcmd_name = Some(sc_name.to_owned()); subcmd_name = Some(sc_name.to_owned());
@ -611,7 +611,7 @@ where
sc_m.add_val_to("", &a); sc_m.add_val_to("", &a);
} }
matcher.subcommand(SubCommand { matcher.subcommand( SubCommand{
name: sc_name, name: sc_name,
matches: sc_m.into(), matches: sc_m.into(),
}); });
@ -886,7 +886,7 @@ where
let name = sc.name.clone(); let name = sc.name.clone();
let mut p = Parser::new(sc); let mut p = Parser::new(sc);
p.get_matches_with(&mut sc_matcher, it)?; p.get_matches_with(&mut sc_matcher, it)?;
matcher.subcommand(SubCommand { matcher.subcommand( SubCommand{
name: name, name: name,
matches: sc_matcher.into(), matches: sc_matcher.into(),
}); });
@ -905,11 +905,11 @@ where
// Needs to use app.settings.is_set instead of just is_set() because is_set() checks // Needs to use app.settings.is_set instead of just is_set() because is_set() checks
// both global and local settings, we only want to check local // both global and local settings, we only want to check local
if arg == "help" && self.app.settings.is_set(AS::NeedsLongHelp) { if arg == "help" && !self.app.settings.is_set(AS::NoAutoHelp) {
sdebugln!("Help"); sdebugln!("Help");
return Err(self.help_err(true)); return Err(self.help_err(true));
} }
if arg == "version" && self.app.settings.is_set(AS::NeedsLongVersion) { if arg == "version" && !self.app.settings.is_set(AS::NoAutoVersion) {
sdebugln!("Version"); sdebugln!("Version");
return Err(self.version_err(true)); return Err(self.version_err(true));
} }
@ -926,16 +926,20 @@ where
); );
// Needs to use app.settings.is_set instead of just is_set() because is_set() checks // Needs to use app.settings.is_set instead of just is_set() because is_set() checks
// both global and local settings, we only want to check local // both global and local settings, we only want to check local
if let Some(h) = self.app.help_short { if let Some(help) = self.app.find("help") {
if arg == h && self.app.settings.is_set(AS::NeedsLongHelp) { if let Some(h) = help.short {
sdebugln!("Help"); if arg == h && !self.app.settings.is_set(AS::NoAutoHelp) {
return Err(self.help_err(false)); sdebugln!("Help");
return Err(self.help_err(false));
}
} }
} }
if let Some(v) = self.app.version_short { if let Some(version) = self.app.find("version") {
if arg == v && self.app.settings.is_set(AS::NeedsLongVersion) { if let Some(v) = version.short {
sdebugln!("Version"); if arg == v && !self.app.settings.is_set(AS::NoAutoVersion) {
return Err(self.version_err(false)); sdebugln!("Version");
return Err(self.version_err(false));
}
} }
} }
sdebugln!("Neither"); sdebugln!("Neither");

View file

@ -1,7 +1,7 @@
extern crate clap; extern crate clap;
extern crate regex; extern crate regex;
use clap::{App, AppSettings, Arg, ErrorKind, Propagation, SubCommand}; use clap::{App, AppSettings, Arg, ErrorKind, Propagation, };
include!("../clap-test.rs"); include!("../clap-test.rs");
@ -78,7 +78,7 @@ fn sub_command_negate_required() {
App::new("sub_command_negate") App::new("sub_command_negate")
.setting(AppSettings::SubcommandsNegateReqs) .setting(AppSettings::SubcommandsNegateReqs)
.arg(Arg::with_name("test").required(true).index(1)) .arg(Arg::with_name("test").required(true).index(1))
.subcommand(SubCommand::with_name("sub1")) .subcommand(App::new("sub1"))
.get_matches_from(vec!["myprog", "sub1"]); .get_matches_from(vec!["myprog", "sub1"]);
} }
@ -87,7 +87,7 @@ fn global_version() {
let mut app = App::new("global_version") let mut app = App::new("global_version")
.setting(AppSettings::GlobalVersion) .setting(AppSettings::GlobalVersion)
.version("1.1") .version("1.1")
.subcommand(SubCommand::with_name("sub1")); .subcommand(App::new("sub1"));
app._propagate(Propagation::NextLevel); app._propagate(Propagation::NextLevel);
assert_eq!(app.subcommands[0].version, Some("1.1")); assert_eq!(app.subcommands[0].version, Some("1.1"));
} }
@ -97,8 +97,8 @@ fn sub_command_negate_required_2() {
let result = App::new("sub_command_negate") let result = App::new("sub_command_negate")
.setting(AppSettings::SubcommandsNegateReqs) .setting(AppSettings::SubcommandsNegateReqs)
.arg(Arg::with_name("test").required(true).index(1)) .arg(Arg::with_name("test").required(true).index(1))
.subcommand(SubCommand::with_name("sub1")) .subcommand(App::new("sub1"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -108,8 +108,8 @@ fn sub_command_negate_required_2() {
fn sub_command_required() { fn sub_command_required() {
let result = App::new("sc_required") let result = App::new("sc_required")
.setting(AppSettings::SubcommandRequired) .setting(AppSettings::SubcommandRequired)
.subcommand(SubCommand::with_name("sub1")) .subcommand(App::new("sub1"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingSubcommand); assert_eq!(err.kind, ErrorKind::MissingSubcommand);
@ -120,7 +120,7 @@ fn arg_required_else_help() {
let result = App::new("arg_required") let result = App::new("arg_required")
.setting(AppSettings::ArgRequiredElseHelp) .setting(AppSettings::ArgRequiredElseHelp)
.arg(Arg::with_name("test").index(1)) .arg(Arg::with_name("test").index(1))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand); assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand);
@ -131,7 +131,7 @@ fn arg_required_else_help_over_reqs() {
let result = App::new("arg_required") let result = App::new("arg_required")
.setting(AppSettings::ArgRequiredElseHelp) .setting(AppSettings::ArgRequiredElseHelp)
.arg(Arg::with_name("test").index(1).required(true)) .arg(Arg::with_name("test").index(1).required(true))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand); assert_eq!(err.kind, ErrorKind::MissingArgumentOrSubcommand);
@ -142,9 +142,9 @@ fn arg_required_else_help_over_reqs() {
fn infer_subcommands_fail_no_args() { fn infer_subcommands_fail_no_args() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from_safe(vec!["prog", "te"]); .try_get_matches_from(vec!["prog", "te"]);
assert!(m.is_err(), "{:#?}", m.unwrap()); assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand); assert_eq!(m.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand);
} }
@ -154,9 +154,9 @@ fn infer_subcommands_fail_no_args() {
fn infer_subcommands_fail_no_args() { fn infer_subcommands_fail_no_args() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from_safe(vec!["prog", "te"]); .try_get_matches_from(vec!["prog", "te"]);
assert!(m.is_err(), "{:#?}", m.unwrap()); assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidSubcommand); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidSubcommand);
} }
@ -166,9 +166,9 @@ fn infer_subcommands_fail_with_args() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.arg(Arg::with_name("some")) .arg(Arg::with_name("some"))
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from_safe(vec!["prog", "t"]); .try_get_matches_from(vec!["prog", "t"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
assert_eq!(m.unwrap().value_of("some"), Some("t")); assert_eq!(m.unwrap().value_of("some"), Some("t"));
} }
@ -178,9 +178,9 @@ fn infer_subcommands_fail_with_args2() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.arg(Arg::with_name("some")) .arg(Arg::with_name("some"))
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from_safe(vec!["prog", "te"]); .try_get_matches_from(vec!["prog", "te"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
assert_eq!(m.unwrap().value_of("some"), Some("te")); assert_eq!(m.unwrap().value_of("some"), Some("te"));
} }
@ -189,7 +189,7 @@ fn infer_subcommands_fail_with_args2() {
fn infer_subcommands_pass() { fn infer_subcommands_pass() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.get_matches_from(vec!["prog", "te"]); .get_matches_from(vec!["prog", "te"]);
assert_eq!(m.subcommand_name(), Some("test")); assert_eq!(m.subcommand_name(), Some("test"));
} }
@ -198,8 +198,8 @@ fn infer_subcommands_pass() {
fn infer_subcommands_pass_close() { fn infer_subcommands_pass_close() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from(vec!["prog", "tes"]); .get_matches_from(vec!["prog", "tes"]);
assert_eq!(m.subcommand_name(), Some("test")); assert_eq!(m.subcommand_name(), Some("test"));
} }
@ -209,9 +209,9 @@ fn infer_subcommands_pass_close() {
fn infer_subcommands_fail_suggestions() { fn infer_subcommands_fail_suggestions() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from_safe(vec!["prog", "temps"]); .try_get_matches_from(vec!["prog", "temps"]);
assert!(m.is_err(), "{:#?}", m.unwrap()); assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidSubcommand); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidSubcommand);
} }
@ -221,9 +221,9 @@ fn infer_subcommands_fail_suggestions() {
fn infer_subcommands_fail_suggestions() { fn infer_subcommands_fail_suggestions() {
let m = App::new("prog") let m = App::new("prog")
.setting(AppSettings::InferSubcommands) .setting(AppSettings::InferSubcommands)
.subcommand(SubCommand::with_name("test")) .subcommand(App::new("test"))
.subcommand(SubCommand::with_name("temp")) .subcommand(App::new("temp"))
.get_matches_from_safe(vec!["prog", "temps"]); .try_get_matches_from(vec!["prog", "temps"]);
assert!(m.is_err(), "{:#?}", m.unwrap()); assert!(m.is_err(), "{:#?}", m.unwrap());
assert_eq!(m.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand); assert_eq!(m.unwrap_err().kind, ErrorKind::UnrecognizedSubcommand);
} }
@ -233,7 +233,7 @@ fn no_bin_name() {
let result = App::new("arg_required") let result = App::new("arg_required")
.setting(AppSettings::NoBinaryName) .setting(AppSettings::NoBinaryName)
.arg(Arg::with_name("test").required(true).index(1)) .arg(Arg::with_name("test").required(true).index(1))
.get_matches_from_safe(vec!["testing"]); .try_get_matches_from(vec!["testing"]);
assert!(result.is_ok()); assert!(result.is_ok());
let matches = result.unwrap(); let matches = result.unwrap();
assert_eq!(matches.value_of("test").unwrap(), "testing"); assert_eq!(matches.value_of("test").unwrap(), "testing");
@ -247,11 +247,9 @@ fn unified_help() {
.about("tests stuff") .about("tests stuff")
.version("1.3") .version("1.3")
.setting(AppSettings::UnifiedHelpMessage) .setting(AppSettings::UnifiedHelpMessage)
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("[arg1] 'some pos arg'")
[arg1] 'some pos arg' .arg("--option [opt] 'some option'");
--option [opt] 'some option'",
);
assert!(test::compare_output( assert!(test::compare_output(
app, app,
@ -285,7 +283,7 @@ fn skip_possible_values() {
fn global_setting() { fn global_setting() {
let mut app = App::new("test") let mut app = App::new("test")
.global_setting(AppSettings::ColoredHelp) .global_setting(AppSettings::ColoredHelp)
.subcommand(SubCommand::with_name("subcmd")); .subcommand(App::new("subcmd"));
app._propagate(Propagation::NextLevel); app._propagate(Propagation::NextLevel);
assert!( assert!(
app.subcommands app.subcommands
@ -300,8 +298,9 @@ fn global_setting() {
#[test] #[test]
fn global_settings() { fn global_settings() {
let mut app = App::new("test") let mut app = App::new("test")
.global_settings(&[AppSettings::ColoredHelp, AppSettings::TrailingVarArg]) .global_setting(AppSettings::ColoredHelp)
.subcommand(SubCommand::with_name("subcmd")); .global_setting(AppSettings::TrailingVarArg)
.subcommand(App::new("subcmd"));
app._propagate(Propagation::NextLevel); app._propagate(Propagation::NextLevel);
assert!( assert!(
app.subcommands app.subcommands
@ -329,7 +328,7 @@ fn stop_delim_values_only_pos_follows() {
Arg::from("-f [flag] 'some opt'"), Arg::from("-f [flag] 'some opt'"),
Arg::from("[arg]... 'some arg'"), Arg::from("[arg]... 'some arg'"),
]) ])
.get_matches_from_safe(vec!["", "--", "-f", "-g,x"]); .try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -361,7 +360,7 @@ fn delim_values_only_pos_follows() {
Arg::from("-f [flag] 'some opt'"), Arg::from("-f [flag] 'some opt'"),
Arg::from("[arg]... 'some arg'"), Arg::from("[arg]... 'some arg'"),
]) ])
.get_matches_from_safe(vec!["", "--", "-f", "-g,x"]); .try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -392,7 +391,7 @@ fn delim_values_only_pos_follows_with_delim() {
Arg::from("-f [flag] 'some opt'"), Arg::from("-f [flag] 'some opt'"),
Arg::from("[arg]... 'some arg'").use_delimiter(true), Arg::from("[arg]... 'some arg'").use_delimiter(true),
]) ])
.get_matches_from_safe(vec!["", "--", "-f", "-g,x"]); .try_get_matches_from(vec!["", "--", "-f", "-g,x"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -422,7 +421,7 @@ fn leading_hyphen_short() {
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::with_name("some")) .arg(Arg::with_name("some"))
.arg(Arg::with_name("other").short('o')) .arg(Arg::with_name("other").short('o'))
.get_matches_from_safe(vec!["", "-bar", "-o"]); .try_get_matches_from(vec!["", "-bar", "-o"]);
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("some")); assert!(m.is_present("some"));
@ -436,7 +435,7 @@ fn leading_hyphen_long() {
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::with_name("some")) .arg(Arg::with_name("some"))
.arg(Arg::with_name("other").short('o')) .arg(Arg::with_name("other").short('o'))
.get_matches_from_safe(vec!["", "--bar", "-o"]); .try_get_matches_from(vec!["", "--bar", "-o"]);
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("some")); assert!(m.is_present("some"));
@ -450,7 +449,7 @@ fn leading_hyphen_opt() {
.setting(AppSettings::AllowLeadingHyphen) .setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::with_name("some").takes_value(true).long("opt")) .arg(Arg::with_name("some").takes_value(true).long("opt"))
.arg(Arg::with_name("other").short('o')) .arg(Arg::with_name("other").short('o'))
.get_matches_from_safe(vec!["", "--opt", "--bar", "-o"]); .try_get_matches_from(vec!["", "--opt", "--bar", "-o"]);
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("some")); assert!(m.is_present("some"));
@ -464,7 +463,7 @@ fn allow_negative_numbers() {
.setting(AppSettings::AllowNegativeNumbers) .setting(AppSettings::AllowNegativeNumbers)
.arg(Arg::with_name("panum")) .arg(Arg::with_name("panum"))
.arg(Arg::with_name("onum").short('o').takes_value(true)) .arg(Arg::with_name("onum").short('o').takes_value(true))
.get_matches_from_safe(vec!["negnum", "-20", "-o", "-1.2"]); .try_get_matches_from(vec!["negnum", "-20", "-o", "-1.2"]);
assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind); assert!(res.is_ok(), "Error: {:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
assert_eq!(m.value_of("panum").unwrap(), "-20"); assert_eq!(m.value_of("panum").unwrap(), "-20");
@ -477,7 +476,7 @@ fn allow_negative_numbers_fail() {
.setting(AppSettings::AllowNegativeNumbers) .setting(AppSettings::AllowNegativeNumbers)
.arg(Arg::with_name("panum")) .arg(Arg::with_name("panum"))
.arg(Arg::with_name("onum").short('o').takes_value(true)) .arg(Arg::with_name("onum").short('o').takes_value(true))
.get_matches_from_safe(vec!["negnum", "--foo", "-o", "-1.2"]); .try_get_matches_from(vec!["negnum", "--foo", "-o", "-1.2"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument) assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument)
} }
@ -511,8 +510,9 @@ fn unset_settings() {
assert!(&m.is_set(AppSettings::AllowInvalidUtf8)); assert!(&m.is_set(AppSettings::AllowInvalidUtf8));
assert!(&m.is_set(AppSettings::ColorAuto)); assert!(&m.is_set(AppSettings::ColorAuto));
let m = m.unset_settings(&[AppSettings::AllowInvalidUtf8, AppSettings::ColorAuto]); let m = m.unset_setting(AppSettings::AllowInvalidUtf8)
assert!(!m.is_set(AppSettings::AllowInvalidUtf8), "{:?}", m.settings); .unset_setting(AppSettings::ColorAuto);
assert!(!m.is_set(AppSettings::AllowInvalidUtf8), "l: {:?}\ng:{:?}", m.settings, m.g_settings);
assert!(!m.is_set(AppSettings::ColorAuto)); assert!(!m.is_set(AppSettings::ColorAuto));
} }
@ -520,8 +520,8 @@ fn unset_settings() {
fn disable_help_subcommand() { fn disable_help_subcommand() {
let result = App::new("disablehelp") let result = App::new("disablehelp")
.setting(AppSettings::DisableHelpSubcommand) .setting(AppSettings::DisableHelpSubcommand)
.subcommand(SubCommand::with_name("sub1")) .subcommand(App::new("sub1"))
.get_matches_from_safe(vec!["", "help"]); .try_get_matches_from(vec!["", "help"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::UnknownArgument); assert_eq!(err.kind, ErrorKind::UnknownArgument);
@ -570,12 +570,12 @@ fn args_negate_subcommands_one_level() {
App::new("disablehelp") App::new("disablehelp")
.setting(AppSettings::ArgsNegateSubcommands) .setting(AppSettings::ArgsNegateSubcommands)
.setting(AppSettings::SubcommandsNegateReqs) .setting(AppSettings::SubcommandsNegateReqs)
.arg_from_usage("<arg1> 'some arg'") .arg("<arg1> 'some arg'")
.arg_from_usage("<arg2> 'some arg'") .arg("<arg2> 'some arg'")
.subcommand(SubCommand::with_name("sub1").subcommand( .subcommand(App::new("sub1").subcommand(
SubCommand::with_name("sub2").subcommand(SubCommand::with_name("sub3")), App::new("sub2").subcommand(App::new("sub3")),
)) ))
.get_matches_from_safe(vec!["", "pickles", "sub1"]); .try_get_matches_from(vec!["", "pickles", "sub1"]);
assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind); assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
assert_eq!(m.value_of("arg2"), Some("sub1")); assert_eq!(m.value_of("arg2"), Some("sub1"));
@ -586,17 +586,17 @@ fn args_negate_subcommands_two_levels() {
let res = App::new("disablehelp") let res = App::new("disablehelp")
.global_setting(AppSettings::ArgsNegateSubcommands) .global_setting(AppSettings::ArgsNegateSubcommands)
.global_setting(AppSettings::SubcommandsNegateReqs) .global_setting(AppSettings::SubcommandsNegateReqs)
.arg_from_usage("<arg1> 'some arg'") .arg("<arg1> 'some arg'")
.arg_from_usage("<arg2> 'some arg'") .arg("<arg2> 'some arg'")
.subcommand( .subcommand(
SubCommand::with_name("sub1") App::new("sub1")
.arg_from_usage("<arg> 'some'") .arg("<arg> 'some'")
.arg_from_usage("<arg2> 'some'") .arg("<arg2> 'some'")
.subcommand( .subcommand(
SubCommand::with_name("sub2").subcommand(SubCommand::with_name("sub3")), App::new("sub2").subcommand(App::new("sub3")),
), ),
) )
.get_matches_from_safe(vec!["", "sub1", "arg", "sub2"]); .try_get_matches_from(vec!["", "sub1", "arg", "sub2"]);
assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind); assert!(res.is_ok(), "error: {:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
assert_eq!( assert_eq!(
@ -609,8 +609,8 @@ fn args_negate_subcommands_two_levels() {
fn propagate_vals_down() { fn propagate_vals_down() {
let m = App::new("myprog") let m = App::new("myprog")
.arg(Arg::from("[cmd] 'command to run'").global(true)) .arg(Arg::from("[cmd] 'command to run'").global(true))
.subcommand(SubCommand::with_name("foo")) .subcommand(App::new("foo"))
.get_matches_from_safe(vec!["myprog", "set", "foo"]); .try_get_matches_from(vec!["myprog", "set", "foo"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
assert_eq!(m.value_of("cmd"), Some("set")); assert_eq!(m.value_of("cmd"), Some("set"));
@ -623,8 +623,8 @@ fn allow_missing_positional() {
let m = App::new("test") let m = App::new("test")
.setting(AppSettings::AllowMissingPositional) .setting(AppSettings::AllowMissingPositional)
.arg(Arg::from("[src] 'some file'").default_value("src")) .arg(Arg::from("[src] 'some file'").default_value("src"))
.arg_from_usage("<dest> 'some file'") .arg("<dest> 'some file'")
.get_matches_from_safe(vec!["test", "file"]); .try_get_matches_from(vec!["test", "file"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
assert_eq!(m.value_of("src"), Some("src")); assert_eq!(m.value_of("src"), Some("src"));
@ -636,8 +636,8 @@ fn allow_missing_positional_no_default() {
let m = App::new("test") let m = App::new("test")
.setting(AppSettings::AllowMissingPositional) .setting(AppSettings::AllowMissingPositional)
.arg(Arg::from("[src] 'some file'")) .arg(Arg::from("[src] 'some file'"))
.arg_from_usage("<dest> 'some file'") .arg("<dest> 'some file'")
.get_matches_from_safe(vec!["test", "file"]); .try_get_matches_from(vec!["test", "file"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
assert_eq!(m.value_of("src"), None); assert_eq!(m.value_of("src"), None);
@ -650,7 +650,7 @@ fn missing_positional_no_hyphen() {
.setting(AppSettings::AllowMissingPositional) .setting(AppSettings::AllowMissingPositional)
.arg(Arg::from("[BENCH] 'some bench'")) .arg(Arg::from("[BENCH] 'some bench'"))
.arg(Arg::from("[ARGS]... 'some args'")) .arg(Arg::from("[ARGS]... 'some args'"))
.get_matches_from_safe(vec!["bench", "foo", "arg1", "arg2", "arg3"]); .try_get_matches_from(vec!["bench", "foo", "arg1", "arg2", "arg3"]);
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind); assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
let m = r.unwrap(); let m = r.unwrap();
@ -671,7 +671,7 @@ fn missing_positional_hyphen() {
.setting(AppSettings::AllowMissingPositional) .setting(AppSettings::AllowMissingPositional)
.arg(Arg::from("[BENCH] 'some bench'")) .arg(Arg::from("[BENCH] 'some bench'"))
.arg(Arg::from("[ARGS]... 'some args'")) .arg(Arg::from("[ARGS]... 'some args'"))
.get_matches_from_safe(vec!["bench", "--", "arg1", "arg2", "arg3"]); .try_get_matches_from(vec!["bench", "--", "arg1", "arg2", "arg3"]);
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind); assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
let m = r.unwrap(); let m = r.unwrap();
@ -694,7 +694,7 @@ fn missing_positional_hyphen_far_back() {
.arg(Arg::from("[BENCH2] 'some bench'")) .arg(Arg::from("[BENCH2] 'some bench'"))
.arg(Arg::from("[BENCH3] 'some bench'")) .arg(Arg::from("[BENCH3] 'some bench'"))
.arg(Arg::from("[ARGS]... 'some args'")) .arg(Arg::from("[ARGS]... 'some args'"))
.get_matches_from_safe(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]); .try_get_matches_from(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]);
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind); assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
let m = r.unwrap(); let m = r.unwrap();
@ -720,7 +720,7 @@ fn missing_positional_hyphen_req_error() {
.arg(Arg::from("[BENCH1] 'some bench'")) .arg(Arg::from("[BENCH1] 'some bench'"))
.arg(Arg::from("<BENCH2> 'some bench'")) .arg(Arg::from("<BENCH2> 'some bench'"))
.arg(Arg::from("[ARGS]... 'some args'")) .arg(Arg::from("[ARGS]... 'some args'"))
.get_matches_from_safe(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]); .try_get_matches_from(vec!["bench", "foo", "--", "arg1", "arg2", "arg3"]);
assert!(r.is_err()); assert!(r.is_err());
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
} }
@ -730,7 +730,7 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args() {
let res = App::new("prog") let res = App::new("prog")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::from("--some-argument")) .arg(Arg::from("--some-argument"))
.get_matches_from_safe(vec!["prog", "hello"]); .try_get_matches_from(vec!["prog", "hello"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
@ -741,7 +741,7 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args_no_vals() {
let res = App::new("prog") let res = App::new("prog")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::from("--some-argument")) .arg(Arg::from("--some-argument"))
.get_matches_from_safe(vec!["prog", "--hello"]); .try_get_matches_from(vec!["prog", "--hello"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
@ -752,7 +752,7 @@ fn issue_1066_allow_leading_hyphen_and_unknown_args_option() {
let res = App::new("prog") let res = App::new("prog")
.global_setting(AppSettings::AllowLeadingHyphen) .global_setting(AppSettings::AllowLeadingHyphen)
.arg(Arg::from("--some-argument=[val]")) .arg(Arg::from("--some-argument=[val]"))
.get_matches_from_safe(vec!["prog", "-hello"]); .try_get_matches_from(vec!["prog", "-hello"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::UnknownArgument);
@ -776,8 +776,8 @@ fn external_subcommand_looks_like_built_in() {
let res = App::new("cargo") let res = App::new("cargo")
.version("1.26.0") .version("1.26.0")
.setting(AppSettings::AllowExternalSubcommands) .setting(AppSettings::AllowExternalSubcommands)
.subcommand(SubCommand::with_name("install")) .subcommand(App::new("install"))
.get_matches_from_safe(vec!["cargo", "install-update", "foo"]); .try_get_matches_from(vec!["cargo", "install-update", "foo"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
match m.subcommand() { match m.subcommand() {
@ -795,7 +795,7 @@ fn aaos_flags() {
let res = App::new("posix") let res = App::new("posix")
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--flag 'some flag'")) .arg(Arg::from("--flag 'some flag'"))
.get_matches_from_safe(vec!["", "--flag", "--flag"]); .try_get_matches_from(vec!["", "--flag", "--flag"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("flag")); assert!(m.is_present("flag"));
@ -808,7 +808,7 @@ fn aaos_flags_mult() {
let res = App::new("posix") let res = App::new("posix")
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--flag... 'some flag'")) .arg(Arg::from("--flag... 'some flag'"))
.get_matches_from_safe(vec!["", "--flag", "--flag", "--flag", "--flag"]); .try_get_matches_from(vec!["", "--flag", "--flag", "--flag", "--flag"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("flag")); assert!(m.is_present("flag"));
@ -821,7 +821,7 @@ fn aaos_opts() {
let res = App::new("posix") let res = App::new("posix")
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--opt [val] 'some option'")) .arg(Arg::from("--opt [val] 'some option'"))
.get_matches_from_safe(vec!["", "--opt=some", "--opt=other"]); .try_get_matches_from(vec!["", "--opt=some", "--opt=other"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -836,7 +836,7 @@ fn aaos_opts_w_other_overrides() {
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--opt [val] 'some option'")) .arg(Arg::from("--opt [val] 'some option'"))
.arg(Arg::from("--other [val] 'some other option'").overrides_with("opt")) .arg(Arg::from("--other [val] 'some other option'").overrides_with("opt"))
.get_matches_from_safe(vec!["", "--opt=some", "--other=test", "--opt=other"]); .try_get_matches_from(vec!["", "--opt=some", "--other=test", "--opt=other"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -852,7 +852,7 @@ fn aaos_opts_w_other_overrides_rev() {
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--opt [val] 'some option'")) .arg(Arg::from("--opt [val] 'some option'"))
.arg(Arg::from("--other [val] 'some other option'").overrides_with("opt")) .arg(Arg::from("--other [val] 'some other option'").overrides_with("opt"))
.get_matches_from_safe(vec!["", "--opt=some", "--opt=other", "--other=val"]); .try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--other=val"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(!m.is_present("opt")); assert!(!m.is_present("opt"));
@ -867,7 +867,7 @@ fn aaos_opts_w_other_overrides_2() {
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--opt [val] 'some option'").overrides_with("other")) .arg(Arg::from("--opt [val] 'some option'").overrides_with("other"))
.arg(Arg::from("--other [val] 'some other option'")) .arg(Arg::from("--other [val] 'some other option'"))
.get_matches_from_safe(vec!["", "--opt=some", "--other=test", "--opt=other"]); .try_get_matches_from(vec!["", "--opt=some", "--other=test", "--opt=other"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -883,7 +883,7 @@ fn aaos_opts_w_other_overrides_rev_2() {
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--opt [val] 'some option'").overrides_with("other")) .arg(Arg::from("--opt [val] 'some option'").overrides_with("other"))
.arg(Arg::from("--other [val] 'some other option'")) .arg(Arg::from("--other [val] 'some other option'"))
.get_matches_from_safe(vec!["", "--opt=some", "--opt=other", "--other=val"]); .try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--other=val"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(!m.is_present("opt")); assert!(!m.is_present("opt"));
@ -901,7 +901,7 @@ fn aaos_opts_mult() {
.number_of_values(1) .number_of_values(1)
.require_delimiter(true), .require_delimiter(true),
) )
.get_matches_from_safe(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]); .try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -918,7 +918,7 @@ fn aaos_opts_mult_req_delims() {
let res = App::new("posix") let res = App::new("posix")
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("--opt [val]... 'some option'")) .arg(Arg::from("--opt [val]... 'some option'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "--opt", "first", "overides", "--opt", "some", "other", "val", "", "--opt", "first", "overides", "--opt", "some", "other", "val",
]); ]);
assert!(res.is_ok()); assert!(res.is_ok());
@ -937,7 +937,7 @@ fn aaos_pos_mult() {
let res = App::new("posix") let res = App::new("posix")
.setting(AppSettings::AllArgsOverrideSelf) .setting(AppSettings::AllArgsOverrideSelf)
.arg(Arg::from("[val]... 'some pos'")) .arg(Arg::from("[val]... 'some pos'"))
.get_matches_from_safe(vec!["", "some", "other", "value"]); .try_get_matches_from(vec!["", "some", "other", "value"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("val")); assert!(m.is_present("val"));

View file

@ -3,7 +3,7 @@ extern crate regex;
include!("../clap-test.rs"); include!("../clap-test.rs");
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, };
static SC_VISIBLE_ALIAS_HELP: &'static str = "ct-test 1.2 static SC_VISIBLE_ALIAS_HELP: &'static str = "ct-test 1.2
Some help Some help
@ -43,7 +43,7 @@ fn single_alias_of_option() {
.help("single alias") .help("single alias")
.alias("new-opt"), .alias("new-opt"),
) )
.get_matches_from_safe(vec!["", "--new-opt", "cool"]); .try_get_matches_from(vec!["", "--new-opt", "cool"]);
assert!(a.is_ok()); assert!(a.is_ok());
let a = a.unwrap(); let a = a.unwrap();
assert!(a.is_present("alias")); assert!(a.is_present("alias"));
@ -61,25 +61,25 @@ fn multiple_aliases_of_option() {
); );
let long = a let long = a
.clone() .clone()
.get_matches_from_safe(vec!["", "--aliases", "value"]); .try_get_matches_from(vec!["", "--aliases", "value"]);
assert!(long.is_ok()); assert!(long.is_ok());
let long = long.unwrap(); let long = long.unwrap();
let als1 = a let als1 = a
.clone() .clone()
.get_matches_from_safe(vec!["", "--alias1", "value"]); .try_get_matches_from(vec!["", "--alias1", "value"]);
assert!(als1.is_ok()); assert!(als1.is_ok());
let als1 = als1.unwrap(); let als1 = als1.unwrap();
let als2 = a let als2 = a
.clone() .clone()
.get_matches_from_safe(vec!["", "--alias2", "value"]); .try_get_matches_from(vec!["", "--alias2", "value"]);
assert!(als2.is_ok()); assert!(als2.is_ok());
let als2 = als2.unwrap(); let als2 = als2.unwrap();
let als3 = a let als3 = a
.clone() .clone()
.get_matches_from_safe(vec!["", "--alias3", "value"]); .try_get_matches_from(vec!["", "--alias3", "value"]);
assert!(als3.is_ok()); assert!(als3.is_ok());
let als3 = als3.unwrap(); let als3 = als3.unwrap();
@ -97,7 +97,7 @@ fn multiple_aliases_of_option() {
fn single_alias_of_flag() { fn single_alias_of_flag() {
let a = App::new("test") let a = App::new("test")
.arg(Arg::with_name("flag").long("flag").alias("alias")) .arg(Arg::with_name("flag").long("flag").alias("alias"))
.get_matches_from_safe(vec!["", "--alias"]); .try_get_matches_from(vec!["", "--alias"]);
assert!(a.is_ok()); assert!(a.is_ok());
let a = a.unwrap(); let a = a.unwrap();
assert!(a.is_present("flag")); assert!(a.is_present("flag"));
@ -113,19 +113,19 @@ fn multiple_aliases_of_flag() {
"aliases", "aliases",
])); ]));
let flag = a.clone().get_matches_from_safe(vec!["", "--flag"]); let flag = a.clone().try_get_matches_from(vec!["", "--flag"]);
assert!(flag.is_ok()); assert!(flag.is_ok());
let flag = flag.unwrap(); let flag = flag.unwrap();
let inv = a.clone().get_matches_from_safe(vec!["", "--invisible"]); let inv = a.clone().try_get_matches_from(vec!["", "--invisible"]);
assert!(inv.is_ok()); assert!(inv.is_ok());
let inv = inv.unwrap(); let inv = inv.unwrap();
let cool = a.clone().get_matches_from_safe(vec!["", "--cool"]); let cool = a.clone().try_get_matches_from(vec!["", "--cool"]);
assert!(cool.is_ok()); assert!(cool.is_ok());
let cool = cool.unwrap(); let cool = cool.unwrap();
let als = a.clone().get_matches_from_safe(vec!["", "--aliases"]); let als = a.clone().try_get_matches_from(vec!["", "--aliases"]);
assert!(als.is_ok()); assert!(als.is_ok());
let als = als.unwrap(); let als = als.unwrap();
@ -139,7 +139,7 @@ fn multiple_aliases_of_flag() {
fn alias_on_a_subcommand_option() { fn alias_on_a_subcommand_option() {
let m = App::new("test") let m = App::new("test")
.subcommand( .subcommand(
SubCommand::with_name("some").arg( App::new("some").arg(
Arg::with_name("test") Arg::with_name("test")
.short('t') .short('t')
.long("test") .long("test")
@ -164,7 +164,7 @@ fn alias_on_a_subcommand_option() {
#[test] #[test]
fn invisible_arg_aliases_help_output() { fn invisible_arg_aliases_help_output() {
let app = App::new("ct").author("Salim Afiune").subcommand( let app = App::new("ct").author("Salim Afiune").subcommand(
SubCommand::with_name("test") App::new("test")
.about("Some help") .about("Some help")
.version("1.2") .version("1.2")
.arg( .arg(
@ -187,7 +187,7 @@ fn invisible_arg_aliases_help_output() {
#[test] #[test]
fn visible_arg_aliases_help_output() { fn visible_arg_aliases_help_output() {
let app = App::new("ct").author("Salim Afiune").subcommand( let app = App::new("ct").author("Salim Afiune").subcommand(
SubCommand::with_name("test") App::new("test")
.about("Some help") .about("Some help")
.version("1.2") .version("1.2")
.arg( .arg(

View file

@ -1,7 +1,7 @@
extern crate clap; extern crate clap;
extern crate regex; extern crate regex;
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, };
include!("../clap-test.rs"); include!("../clap-test.rs");
@ -19,7 +19,7 @@ fn borrowed_args() {
.arg(Arg::with_name("test").index(1)) .arg(Arg::with_name("test").index(1))
.arg(&arg) .arg(&arg)
.arg(&arg2) .arg(&arg2)
.subcommand(SubCommand::with_name("sub1").arg(&arg)) .subcommand(App::new("sub1").arg(&arg))
.get_matches_from_safe(vec!["prog"]); .try_get_matches_from(vec!["prog"]);
assert!(result.is_ok()); assert!(result.is_ok());
} }

View file

@ -1,899 +0,0 @@
extern crate clap;
extern crate regex;
use clap::{App, Arg, Shell, SubCommand};
use regex::Regex;
static BASH: &'static str = r#"_myapp() {
local i cur prev opts cmds
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""
for i in ${COMP_WORDS[@]}
do
case "${i}" in
myapp)
cmd="myapp"
;;
help)
cmd+="__help"
;;
test)
cmd+="__test"
;;
*)
;;
esac
done
case "${cmd}" in
myapp)
opts=" -h -V --help --version <file> test help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
myapp__help)
opts=" -h -V --help --version "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
myapp__test)
opts=" -h -V --case --help --version "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
--case)
COMPREPLY=($(compgen -f ${cur}))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
esac
}
complete -F _myapp -o bashdefault -o default myapp
"#;
static ZSH: &'static str = r#"#compdef myapp
autoload -U is-at-least
_myapp() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1
if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi
local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
'::file -- some input file:_files' \
":: :_myapp_commands" \
"*::: :->myapp" \
&& ret=0
case $state in
(myapp)
words=($line[2] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:myapp-command-$line[2]:"
case $line[2] in
(test)
_arguments "${_arguments_options[@]}" \
'--case=[the case to test]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
;;
esac
;;
esac
}
(( $+functions[_myapp_commands] )) ||
_myapp_commands() {
local commands; commands=(
"test:tests things" \
"help:Prints this message or the help of the given subcommand(s)" \
)
_describe -t commands 'myapp commands' commands "$@"
}
(( $+functions[_myapp__help_commands] )) ||
_myapp__help_commands() {
local commands; commands=(
)
_describe -t commands 'myapp help commands' commands "$@"
}
(( $+functions[_myapp__test_commands] )) ||
_myapp__test_commands() {
local commands; commands=(
)
_describe -t commands 'myapp test commands' commands "$@"
}
_myapp "$@""#;
static FISH: &'static str = r#"complete -c myapp -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
complete -c myapp -n "__fish_use_subcommand" -s V -l version -d 'Prints version information'
complete -c myapp -n "__fish_use_subcommand" -f -a "test" -d 'tests things'
complete -c myapp -n "__fish_use_subcommand" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)'
complete -c myapp -n "__fish_seen_subcommand_from test" -l case -d 'the case to test'
complete -c myapp -n "__fish_seen_subcommand_from test" -s h -l help -d 'Prints help information'
complete -c myapp -n "__fish_seen_subcommand_from test" -s V -l version -d 'Prints version information'
complete -c myapp -n "__fish_seen_subcommand_from help" -s h -l help -d 'Prints help information'
complete -c myapp -n "__fish_seen_subcommand_from help" -s V -l version -d 'Prints version information'
"#;
static POWERSHELL: &'static str = r#"
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Register-ArgumentCompleter -Native -CommandName 'my_app' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$command = @(
'my_app'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-')) {
break
}
$element.Value
}) -join ';'
$completions = @(switch ($command) {
'my_app' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Prints this message or the help of the given subcommand(s)')
break
}
'my_app;test' {
[CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
break
}
'my_app;help' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
break
}
})
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
"#;
static ELVISH: &'static str = r#"
edit:completion:arg-completer[my_app] = [@words]{
fn spaces [n]{
repeat $n ' ' | joins ''
}
fn cand [text desc]{
edit:complex-candidate $text &display-suffix=' '(spaces (- 14 (wcswidth $text)))$desc
}
command = 'my_app'
for word $words[1:-1] {
if (has-prefix $word '-') {
break
}
command = $command';'$word
}
completions = [
&'my_app'= {
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
cand test 'tests things'
cand help 'Prints this message or the help of the given subcommand(s)'
}
&'my_app;test'= {
cand --case 'the case to test'
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
}
&'my_app;help'= {
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
}
]
$completions[$command]
}
"#;
static ELVISH_SPECIAL_CMDS: &'static str = r#"
edit:completion:arg-completer[my_app] = [@words]{
fn spaces [n]{
repeat $n ' ' | joins ''
}
fn cand [text desc]{
edit:complex-candidate $text &display-suffix=' '(spaces (- 14 (wcswidth $text)))$desc
}
command = 'my_app'
for word $words[1:-1] {
if (has-prefix $word '-') {
break
}
command = $command';'$word
}
completions = [
&'my_app'= {
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
cand test 'tests things'
cand some_cmd 'tests other things'
cand some-cmd-with-hypens 'some-cmd-with-hypens'
cand help 'Prints this message or the help of the given subcommand(s)'
}
&'my_app;test'= {
cand --case 'the case to test'
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
}
&'my_app;some_cmd'= {
cand --config 'the other case to test'
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
}
&'my_app;some-cmd-with-hypens'= {
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
}
&'my_app;help'= {
cand -h 'Prints help information'
cand --help 'Prints help information'
cand -V 'Prints version information'
cand --version 'Prints version information'
}
]
$completions[$command]
}
"#;
static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
using namespace System.Management.Automation
using namespace System.Management.Automation.Language
Register-ArgumentCompleter -Native -CommandName 'my_app' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
$commandElements = $commandAst.CommandElements
$command = @(
'my_app'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-')) {
break
}
$element.Value
}) -join ';'
$completions = @(switch ($command) {
'my_app' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'tests things')
[CompletionResult]::new('some_cmd', 'some_cmd', [CompletionResultType]::ParameterValue, 'tests other things')
[CompletionResult]::new('some-cmd-with-hypens', 'some-cmd-with-hypens', [CompletionResultType]::ParameterValue, 'some-cmd-with-hypens')
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Prints this message or the help of the given subcommand(s)')
break
}
'my_app;test' {
[CompletionResult]::new('--case', 'case', [CompletionResultType]::ParameterName, 'the case to test')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
break
}
'my_app;some_cmd' {
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'the other case to test')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
break
}
'my_app;some-cmd-with-hypens' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
break
}
'my_app;help' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Prints help information')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Prints version information')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Prints version information')
break
}
})
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
"#;
static ZSH_SPECIAL_CMDS: &'static str = r#"#compdef my_app
autoload -U is-at-least
_my_app() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1
if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi
local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
'::file -- some input file:_files' \
":: :_my_app_commands" \
"*::: :->my_app" \
&& ret=0
case $state in
(my_app)
words=($line[2] "${words[@]}")
(( CURRENT += 1 ))
curcontext="${curcontext%:*:*}:my_app-command-$line[2]:"
case $line[2] in
(test)
_arguments "${_arguments_options[@]}" \
'--case=[the case to test]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
;;
(some_cmd)
_arguments "${_arguments_options[@]}" \
'--config=[the other case to test]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
;;
(some-cmd-with-hypens)
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
;;
esac
;;
esac
}
(( $+functions[_my_app_commands] )) ||
_my_app_commands() {
local commands; commands=(
"test:tests things" \
"some_cmd:tests other things" \
"some-cmd-with-hypens:" \
"help:Prints this message or the help of the given subcommand(s)" \
)
_describe -t commands 'my_app commands' commands "$@"
}
(( $+functions[_my_app__help_commands] )) ||
_my_app__help_commands() {
local commands; commands=(
)
_describe -t commands 'my_app help commands' commands "$@"
}
(( $+functions[_my_app__some-cmd-with-hypens_commands] )) ||
_my_app__some-cmd-with-hypens_commands() {
local commands; commands=(
)
_describe -t commands 'my_app some-cmd-with-hypens commands' commands "$@"
}
(( $+functions[_my_app__some_cmd_commands] )) ||
_my_app__some_cmd_commands() {
local commands; commands=(
)
_describe -t commands 'my_app some_cmd commands' commands "$@"
}
(( $+functions[_my_app__test_commands] )) ||
_my_app__test_commands() {
local commands; commands=(
)
_describe -t commands 'my_app test commands' commands "$@"
}
_my_app "$@""#;
static FISH_SPECIAL_CMDS: &'static str = r#"complete -c my_app -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
complete -c my_app -n "__fish_use_subcommand" -s V -l version -d 'Prints version information'
complete -c my_app -n "__fish_use_subcommand" -f -a "test" -d 'tests things'
complete -c my_app -n "__fish_use_subcommand" -f -a "some_cmd" -d 'tests other things'
complete -c my_app -n "__fish_use_subcommand" -f -a "some-cmd-with-hypens"
complete -c my_app -n "__fish_use_subcommand" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)'
complete -c my_app -n "__fish_seen_subcommand_from test" -l case -d 'the case to test'
complete -c my_app -n "__fish_seen_subcommand_from test" -s h -l help -d 'Prints help information'
complete -c my_app -n "__fish_seen_subcommand_from test" -s V -l version -d 'Prints version information'
complete -c my_app -n "__fish_seen_subcommand_from some_cmd" -l config -d 'the other case to test'
complete -c my_app -n "__fish_seen_subcommand_from some_cmd" -s h -l help -d 'Prints help information'
complete -c my_app -n "__fish_seen_subcommand_from some_cmd" -s V -l version -d 'Prints version information'
complete -c my_app -n "__fish_seen_subcommand_from some-cmd-with-hypens" -s h -l help -d 'Prints help information'
complete -c my_app -n "__fish_seen_subcommand_from some-cmd-with-hypens" -s V -l version -d 'Prints version information'
complete -c my_app -n "__fish_seen_subcommand_from help" -s h -l help -d 'Prints help information'
complete -c my_app -n "__fish_seen_subcommand_from help" -s V -l version -d 'Prints version information'
"#;
static BASH_SPECIAL_CMDS: &'static str = r#"_my_app() {
local i cur prev opts cmds
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""
for i in ${COMP_WORDS[@]}
do
case "${i}" in
my_app)
cmd="my_app"
;;
help)
cmd+="__help"
;;
some-cmd-with-hypens)
cmd+="__some__cmd__with__hypens"
;;
some_cmd)
cmd+="__some_cmd"
;;
test)
cmd+="__test"
;;
*)
;;
esac
done
case "${cmd}" in
my_app)
opts=" -h -V --help --version <file> test some_cmd some-cmd-with-hypens help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
my_app__help)
opts=" -h -V --help --version "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
my_app__some__cmd__with__hypens)
opts=" -h -V --help --version "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
my_app__some_cmd)
opts=" -h -V --config --help --version "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
--config)
COMPREPLY=($(compgen -f ${cur}))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
my_app__test)
opts=" -h -V --case --help --version "
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
fi
case "${prev}" in
--case)
COMPREPLY=($(compgen -f ${cur}))
return 0
;;
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
;;
esac
}
complete -F _my_app -o bashdefault -o default my_app
"#;
static FISH_SPECIAL_HELP: &'static str = r#"complete -c my_app -n "__fish_use_subcommand" -l single-quotes -d 'Can be \'always\', \'auto\', or \'never\''
complete -c my_app -n "__fish_use_subcommand" -l double-quotes -d 'Can be "always", "auto", or "never"'
complete -c my_app -n "__fish_use_subcommand" -l backticks -d 'For more information see `echo test`'
complete -c my_app -n "__fish_use_subcommand" -l backslash -d 'Avoid \'\\n\''
complete -c my_app -n "__fish_use_subcommand" -l brackets -d 'List packages [filter]'
complete -c my_app -n "__fish_use_subcommand" -l expansions -d 'Execute the shell command with $SHELL'
complete -c my_app -n "__fish_use_subcommand" -s h -l help -d 'Prints help information'
complete -c my_app -n "__fish_use_subcommand" -s V -l version -d 'Prints version information'
"#;
static ZSH_SPECIAL_HELP: &'static str = r#"#compdef my_app
autoload -U is-at-least
_my_app() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1
if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi
local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
'--double-quotes[Can be "always", "auto", or "never"]' \
'--backticks[For more information see `echo test`]' \
'--backslash[Avoid '\''\\n'\'']' \
'--brackets[List packages \[filter\]]' \
'--expansions[Execute the shell command with $SHELL]' \
'-h[Prints help information]' \
'--help[Prints help information]' \
'-V[Prints version information]' \
'--version[Prints version information]' \
&& ret=0
}
(( $+functions[_my_app_commands] )) ||
_my_app_commands() {
local commands; commands=(
)
_describe -t commands 'my_app commands' commands "$@"
}
_my_app "$@""#;
fn compare(left: &str, right: &str) -> bool {
let b = left == right;
if !b {
let re = Regex::new(" ").unwrap();
println!("");
println!("--> left");
println!("{}", re.replace_all(left, "\u{2022}"));
println!("--> right");
println!("{}", re.replace_all(right, "\u{2022}"));
println!("--")
}
b
}
fn build_app() -> App<'static, 'static> { build_app_with_name("myapp") }
fn build_app_with_name(s: &'static str) -> App<'static, 'static> {
App::new(s)
.about("Tests completions")
.arg(Arg::with_name("file").help("some input file"))
.subcommand(
SubCommand::with_name("test").about("tests things").arg(
Arg::with_name("case")
.long("case")
.takes_value(true)
.help("the case to test"),
),
)
}
fn build_app_special_commands() -> App<'static, 'static> {
build_app_with_name("my_app")
.subcommand(
SubCommand::with_name("some_cmd")
.about("tests other things")
.arg(
Arg::with_name("config")
.long("--config")
.takes_value(true)
.help("the other case to test"),
),
)
.subcommand(SubCommand::with_name("some-cmd-with-hypens"))
}
fn build_app_special_help() -> App<'static, 'static> {
App::new("my_app")
.arg(
Arg::with_name("single-quotes")
.long("single-quotes")
.help("Can be 'always', 'auto', or 'never'"),
)
.arg(
Arg::with_name("double-quotes")
.long("double-quotes")
.help("Can be \"always\", \"auto\", or \"never\""),
)
.arg(
Arg::with_name("backticks")
.long("backticks")
.help("For more information see `echo test`"),
)
.arg(
Arg::with_name("backslash")
.long("backslash")
.help("Avoid '\\n'"),
)
.arg(
Arg::with_name("brackets")
.long("brackets")
.help("List packages [filter]"),
)
.arg(
Arg::with_name("expansions")
.long("expansions")
.help("Execute the shell command with $SHELL"),
)
}
#[test]
fn bash() {
let mut app = build_app();
let mut buf = vec![];
app.gen_completions_to("myapp", Shell::Bash, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, BASH));
}
#[test]
fn zsh() {
let mut app = build_app();
let mut buf = vec![];
app.gen_completions_to("myapp", Shell::Zsh, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, ZSH));
}
#[test]
fn fish() {
let mut app = build_app();
let mut buf = vec![];
app.gen_completions_to("myapp", Shell::Fish, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, FISH));
}
#[test]
fn powershell() {
let mut app = build_app();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::PowerShell, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, POWERSHELL));
}
#[test]
fn elvish() {
let mut app = build_app();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Elvish, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, ELVISH));
}
#[test]
fn elvish_with_special_commands() {
let mut app = build_app_special_commands();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Elvish, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, ELVISH_SPECIAL_CMDS));
}
#[test]
fn powershell_with_special_commands() {
let mut app = build_app_special_commands();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::PowerShell, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, POWERSHELL_SPECIAL_CMDS));
}
#[test]
fn bash_with_special_commands() {
let mut app = build_app_special_commands();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Bash, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, BASH_SPECIAL_CMDS));
}
#[test]
fn fish_with_special_commands() {
let mut app = build_app_special_commands();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Fish, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, FISH_SPECIAL_CMDS));
}
#[test]
fn zsh_with_special_commands() {
let mut app = build_app_special_commands();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Zsh, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, ZSH_SPECIAL_CMDS));
}
#[test]
fn fish_with_special_help() {
let mut app = build_app_special_help();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Fish, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, FISH_SPECIAL_HELP));
}
#[test]
fn zsh_with_special_help() {
let mut app = build_app_special_help();
let mut buf = vec![];
app.gen_completions_to("my_app", Shell::Zsh, &mut buf);
let string = String::from_utf8(buf).unwrap();
assert!(compare(&*string, ZSH_SPECIAL_HELP));
}

View file

@ -24,7 +24,7 @@ fn flag_conflict() {
let result = App::new("flag_conflict") let result = App::new("flag_conflict")
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other")) .arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other"))
.arg(Arg::from("-o, --other 'some flag'")) .arg(Arg::from("-o, --other 'some flag'"))
.get_matches_from_safe(vec!["myprog", "-f", "-o"]); .try_get_matches_from(vec!["myprog", "-f", "-o"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -35,7 +35,7 @@ fn flag_conflict_2() {
let result = App::new("flag_conflict") let result = App::new("flag_conflict")
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other")) .arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other"))
.arg(Arg::from("-o, --other 'some flag'")) .arg(Arg::from("-o, --other 'some flag'"))
.get_matches_from_safe(vec!["myprog", "-o", "-f"]); .try_get_matches_from(vec!["myprog", "-o", "-f"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -53,7 +53,7 @@ fn group_conflict() {
) )
.arg(Arg::from("--some 'some arg'")) .arg(Arg::from("--some 'some arg'"))
.arg(Arg::from("--other 'other arg'")) .arg(Arg::from("--other 'other arg'"))
.get_matches_from_safe(vec!["myprog", "--other", "-f"]); .try_get_matches_from(vec!["myprog", "--other", "-f"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -71,7 +71,7 @@ fn group_conflict_2() {
) )
.arg(Arg::from("--some 'some arg'")) .arg(Arg::from("--some 'some arg'"))
.arg(Arg::from("--other 'other arg'")) .arg(Arg::from("--other 'other arg'"))
.get_matches_from_safe(vec!["myprog", "-f", "--some"]); .try_get_matches_from(vec!["myprog", "-f", "--some"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -102,7 +102,7 @@ fn conflict_with_unused_default_value() {
let result = App::new("conflict") let result = App::new("conflict")
.arg(Arg::from("-o, --opt=[opt] 'some opt'").default_value("default")) .arg(Arg::from("-o, --opt=[opt] 'some opt'").default_value("default"))
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("opt")) .arg(Arg::from("-f, --flag 'some flag'").conflicts_with("opt"))
.get_matches_from_safe(vec!["myprog", "-f"]); .try_get_matches_from(vec!["myprog", "-f"]);
assert!(result.is_ok()); assert!(result.is_ok());
let m = result.unwrap(); let m = result.unwrap();
assert_eq!(m.value_of("opt"), Some("default")); assert_eq!(m.value_of("opt"), Some("default"));

View file

@ -9,7 +9,7 @@ use clap::{App, Arg, ErrorKind};
fn opts() { fn opts() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("-o [opt] 'some opt'").default_value("default")) .arg(Arg::from("-o [opt] 'some opt'").default_value("default"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("o")); assert!(m.is_present("o"));
@ -20,7 +20,7 @@ fn opts() {
fn opt_user_override() { fn opt_user_override() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'").default_value("default")) .arg(Arg::from("--opt [FILE] 'some arg'").default_value("default"))
.get_matches_from_safe(vec!["", "--opt", "value"]); .try_get_matches_from(vec!["", "--opt", "value"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -31,7 +31,7 @@ fn opt_user_override() {
fn positionals() { fn positionals() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").default_value("default")) .arg(Arg::from("[arg] 'some opt'").default_value("default"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -42,7 +42,7 @@ fn positionals() {
fn positional_user_override() { fn positional_user_override() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some arg'").default_value("default")) .arg(Arg::from("[arg] 'some arg'").default_value("default"))
.get_matches_from_safe(vec!["", "value"]); .try_get_matches_from(vec!["", "value"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -58,7 +58,7 @@ fn osstr_opts() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("-o [opt] 'some opt'").default_value_os(expected)) .arg(Arg::from("-o [opt] 'some opt'").default_value_os(expected))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("o")); assert!(m.is_present("o"));
@ -72,7 +72,7 @@ fn osstr_opt_user_override() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'").default_value_os(default)) .arg(Arg::from("--opt [FILE] 'some arg'").default_value_os(default))
.get_matches_from_safe(vec!["", "--opt", "value"]); .try_get_matches_from(vec!["", "--opt", "value"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -86,7 +86,7 @@ fn osstr_positionals() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").default_value_os(expected)) .arg(Arg::from("[arg] 'some opt'").default_value_os(expected))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -100,7 +100,7 @@ fn osstr_positional_user_override() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some arg'").default_value_os(default)) .arg(Arg::from("[arg] 'some arg'").default_value_os(default))
.get_matches_from_safe(vec!["", "value"]); .try_get_matches_from(vec!["", "value"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -114,7 +114,7 @@ fn default_if_arg_present_no_default() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'")) .arg(Arg::from("--opt [FILE] 'some arg'"))
.arg(Arg::from("[arg] 'some arg'").default_value_if("opt", None, "default")) .arg(Arg::from("[arg] 'some arg'").default_value_if("opt", None, "default"))
.get_matches_from_safe(vec!["", "--opt", "some"]); .try_get_matches_from(vec!["", "--opt", "some"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -126,7 +126,7 @@ fn default_if_arg_present_no_default_user_override() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'")) .arg(Arg::from("--opt [FILE] 'some arg'"))
.arg(Arg::from("[arg] 'some arg'").default_value_if("opt", None, "default")) .arg(Arg::from("[arg] 'some arg'").default_value_if("opt", None, "default"))
.get_matches_from_safe(vec!["", "--opt", "some", "other"]); .try_get_matches_from(vec!["", "--opt", "some", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -142,7 +142,7 @@ fn default_if_arg_present_no_arg_with_default() {
.default_value("first") .default_value("first")
.default_value_if("opt", None, "default"), .default_value_if("opt", None, "default"),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -158,7 +158,7 @@ fn default_if_arg_present_with_default() {
.default_value("first") .default_value("first")
.default_value_if("opt", None, "default"), .default_value_if("opt", None, "default"),
) )
.get_matches_from_safe(vec!["", "--opt", "some"]); .try_get_matches_from(vec!["", "--opt", "some"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -174,7 +174,7 @@ fn default_if_arg_present_with_default_user_override() {
.default_value("first") .default_value("first")
.default_value_if("opt", None, "default"), .default_value_if("opt", None, "default"),
) )
.get_matches_from_safe(vec!["", "--opt", "some", "other"]); .try_get_matches_from(vec!["", "--opt", "some", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -190,7 +190,7 @@ fn default_if_arg_present_no_arg_with_default_user_override() {
.default_value("first") .default_value("first")
.default_value_if("opt", None, "default"), .default_value_if("opt", None, "default"),
) )
.get_matches_from_safe(vec!["", "other"]); .try_get_matches_from(vec!["", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -204,7 +204,7 @@ fn default_if_arg_present_with_value_no_default() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'")) .arg(Arg::from("--opt [FILE] 'some arg'"))
.arg(Arg::from("[arg] 'some arg'").default_value_if("opt", Some("value"), "default")) .arg(Arg::from("[arg] 'some arg'").default_value_if("opt", Some("value"), "default"))
.get_matches_from_safe(vec!["", "--opt", "value"]); .try_get_matches_from(vec!["", "--opt", "value"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -216,7 +216,7 @@ fn default_if_arg_present_with_value_no_default_fail() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'")) .arg(Arg::from("--opt [FILE] 'some arg'"))
.arg(Arg::from("[arg] 'some arg'").default_value_if("opt", Some("value"), "default")) .arg(Arg::from("[arg] 'some arg'").default_value_if("opt", Some("value"), "default"))
.get_matches_from_safe(vec!["", "--opt", "other"]); .try_get_matches_from(vec!["", "--opt", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(!m.is_present("arg")); assert!(!m.is_present("arg"));
@ -227,7 +227,7 @@ fn default_if_arg_present_with_value_no_default_user_override() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'")) .arg(Arg::from("--opt [FILE] 'some arg'"))
.arg(Arg::from("[arg] 'some arg'").default_value_if("opt", Some("some"), "default")) .arg(Arg::from("[arg] 'some arg'").default_value_if("opt", Some("some"), "default"))
.get_matches_from_safe(vec!["", "--opt", "some", "other"]); .try_get_matches_from(vec!["", "--opt", "some", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -243,7 +243,7 @@ fn default_if_arg_present_with_value_no_arg_with_default() {
.default_value("first") .default_value("first")
.default_value_if("opt", Some("some"), "default"), .default_value_if("opt", Some("some"), "default"),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -259,7 +259,7 @@ fn default_if_arg_present_with_value_no_arg_with_default_fail() {
.default_value("first") .default_value("first")
.default_value_if("opt", Some("some"), "default"), .default_value_if("opt", Some("some"), "default"),
) )
.get_matches_from_safe(vec!["", "--opt", "other"]); .try_get_matches_from(vec!["", "--opt", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -275,7 +275,7 @@ fn default_if_arg_present_with_value_with_default() {
.default_value("first") .default_value("first")
.default_value_if("opt", Some("some"), "default"), .default_value_if("opt", Some("some"), "default"),
) )
.get_matches_from_safe(vec!["", "--opt", "some"]); .try_get_matches_from(vec!["", "--opt", "some"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -291,7 +291,7 @@ fn default_if_arg_present_with_value_with_default_user_override() {
.default_value("first") .default_value("first")
.default_value_if("opt", Some("some"), "default"), .default_value_if("opt", Some("some"), "default"),
) )
.get_matches_from_safe(vec!["", "--opt", "some", "other"]); .try_get_matches_from(vec!["", "--opt", "some", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -307,7 +307,7 @@ fn default_if_arg_present_no_arg_with_value_with_default_user_override() {
.default_value("first") .default_value("first")
.default_value_if("opt", Some("some"), "default"), .default_value_if("opt", Some("some"), "default"),
) )
.get_matches_from_safe(vec!["", "other"]); .try_get_matches_from(vec!["", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -323,7 +323,7 @@ fn default_if_arg_present_no_arg_with_value_with_default_user_override_fail() {
.default_value("first") .default_value("first")
.default_value_if("opt", Some("some"), "default"), .default_value_if("opt", Some("some"), "default"),
) )
.get_matches_from_safe(vec!["", "--opt", "value", "other"]); .try_get_matches_from(vec!["", "--opt", "value", "other"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -342,7 +342,7 @@ fn default_ifs_arg_present() {
.default_value("first") .default_value("first")
.default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]), .default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]),
) )
.get_matches_from_safe(vec!["", "--flag"]); .try_get_matches_from(vec!["", "--flag"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -359,7 +359,7 @@ fn default_ifs_arg_present_user_override() {
.default_value("first") .default_value("first")
.default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]), .default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]),
) )
.get_matches_from_safe(vec!["", "--flag", "value"]); .try_get_matches_from(vec!["", "--flag", "value"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -376,7 +376,7 @@ fn default_ifs_arg_present_order() {
.default_value("first") .default_value("first")
.default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]), .default_value_ifs(&[("opt", Some("some"), "default"), ("flag", None, "flg")]),
) )
.get_matches_from_safe(vec!["", "--opt=some", "--flag"]); .try_get_matches_from(vec!["", "--opt=some", "--flag"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -408,7 +408,7 @@ fn conditional_reqs_fail() {
.required_if("target", "file") .required_if("target", "file")
.long("output"), .long("output"),
) )
.get_matches_from_safe(vec!["test", "--input", "some"]); .try_get_matches_from(vec!["test", "--input", "some"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(m.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -439,7 +439,7 @@ fn conditional_reqs_pass() {
.required_if("target", "file") .required_if("target", "file")
.long("output"), .long("output"),
) )
.get_matches_from_safe(vec!["test", "--input", "some", "--output", "other"]); .try_get_matches_from(vec!["test", "--input", "some", "--output", "other"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -458,7 +458,7 @@ fn issue_1050_num_vals_and_defaults() {
.number_of_values(1) .number_of_values(1)
.default_value("0"), .default_value("0"),
) )
.get_matches_from_safe(vec!["hello", "--exit-code=1"]); .try_get_matches_from(vec!["hello", "--exit-code=1"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert_eq!(m.value_of("exit-code"), Some("1")); assert_eq!(m.value_of("exit-code"), Some("1"));

View file

@ -3,7 +3,7 @@ extern crate regex;
use std::str; use std::str;
use clap::{App, AppSettings, Arg, SubCommand}; use clap::{App, AppSettings, Arg, };
include!("../clap-test.rs"); include!("../clap-test.rs");
@ -225,7 +225,7 @@ fn derive_order_subcommand_propagate() {
.global_setting(AppSettings::DeriveDisplayOrder) .global_setting(AppSettings::DeriveDisplayOrder)
.version("1.2") .version("1.2")
.subcommand( .subcommand(
SubCommand::with_name("sub").version("1.2").args(&[ App::new("sub").version("1.2").args(&[
Arg::with_name("flag_b").long("flag_b").help("first flag"), Arg::with_name("flag_b").long("flag_b").help("first flag"),
Arg::with_name("option_b") Arg::with_name("option_b")
.long("option_b") .long("option_b")
@ -252,7 +252,7 @@ fn unified_help_subcommand_propagate() {
let app = App::new("test") let app = App::new("test")
.global_setting(AppSettings::UnifiedHelpMessage) .global_setting(AppSettings::UnifiedHelpMessage)
.subcommand( .subcommand(
SubCommand::with_name("sub").version("1.2").args(&[ App::new("sub").version("1.2").args(&[
Arg::with_name("flag_b").long("flag_b").help("first flag"), Arg::with_name("flag_b").long("flag_b").help("first flag"),
Arg::with_name("option_b") Arg::with_name("option_b")
.long("option_b") .long("option_b")
@ -280,7 +280,7 @@ fn unified_help_and_derive_order_subcommand_propagate() {
.global_setting(AppSettings::DeriveDisplayOrder) .global_setting(AppSettings::DeriveDisplayOrder)
.global_setting(AppSettings::UnifiedHelpMessage) .global_setting(AppSettings::UnifiedHelpMessage)
.subcommand( .subcommand(
SubCommand::with_name("sub").version("1.2").args(&[ App::new("sub").version("1.2").args(&[
Arg::with_name("flag_b").long("flag_b").help("first flag"), Arg::with_name("flag_b").long("flag_b").help("first flag"),
Arg::with_name("option_b") Arg::with_name("option_b")
.long("option_b") .long("option_b")
@ -308,7 +308,7 @@ fn unified_help_and_derive_order_subcommand_propagate_with_explicit_display_orde
.global_setting(AppSettings::DeriveDisplayOrder) .global_setting(AppSettings::DeriveDisplayOrder)
.global_setting(AppSettings::UnifiedHelpMessage) .global_setting(AppSettings::UnifiedHelpMessage)
.subcommand( .subcommand(
SubCommand::with_name("sub").version("1.2").args(&[ App::new("sub").version("1.2").args(&[
Arg::with_name("flag_b").long("flag_b").help("first flag"), Arg::with_name("flag_b").long("flag_b").help("first flag"),
Arg::with_name("option_b") Arg::with_name("option_b")
.long("option_b") .long("option_b")

View file

@ -11,7 +11,7 @@ fn env() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV")) .arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -22,11 +22,11 @@ fn env() {
#[test] #[test]
fn env_os() { fn env_os() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_OS", "env");
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env_os(OsStr::new("CLP_TEST_ENV"))) .arg(Arg::from("[arg] 'some opt'").env_os(OsStr::new("CLP_TEST_ENV_OS")))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -43,7 +43,7 @@ fn no_env() {
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_NONE")) .arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_NONE"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -54,15 +54,15 @@ fn no_env() {
#[test] #[test]
fn with_default() { fn with_default() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_WD", "env");
let r = App::new("df") let r = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_WD")
.default_value("default"), .default_value("default"),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -73,11 +73,11 @@ fn with_default() {
#[test] #[test]
fn opt_user_override() { fn opt_user_override() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_OR", "env");
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("--arg [FILE] 'some arg'").env("CLP_TEST_ENV")) .arg(Arg::from("--arg [FILE] 'some arg'").env("CLP_TEST_ENV_OR"))
.get_matches_from_safe(vec!["", "--arg", "opt"]); .try_get_matches_from(vec!["", "--arg", "opt"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -88,11 +88,11 @@ fn opt_user_override() {
#[test] #[test]
fn positionals() { fn positionals() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_P", "env");
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV")) .arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_P"))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -103,11 +103,11 @@ fn positionals() {
#[test] #[test]
fn positionals_user_override() { fn positionals_user_override() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_POR", "env");
let r = App::new("df") let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV")) .arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_POR"))
.get_matches_from_safe(vec!["", "opt"]); .try_get_matches_from(vec!["", "opt"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -118,16 +118,16 @@ fn positionals_user_override() {
#[test] #[test]
fn multiple_one() { fn multiple_one() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_MO", "env");
let r = App::new("df") let r = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_MO")
.use_delimiter(true) .use_delimiter(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -147,7 +147,7 @@ fn multiple_three() {
.use_delimiter(true) .use_delimiter(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -169,7 +169,7 @@ fn multiple_no_delimiter() {
.env("CLP_TEST_ENV_MULTI2") .env("CLP_TEST_ENV_MULTI2")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -183,15 +183,15 @@ fn multiple_no_delimiter() {
#[test] #[test]
fn possible_value() { fn possible_value() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_PV", "env");
let r = App::new("df") let r = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_PV")
.possible_value("env"), .possible_value("env"),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -202,27 +202,27 @@ fn possible_value() {
#[test] #[test]
fn not_possible_value() { fn not_possible_value() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_NPV", "env");
let r = App::new("df") let r = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_NPV")
.possible_value("never"), .possible_value("never"),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_err()); assert!(r.is_err());
} }
#[test] #[test]
fn validator() { fn validator() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_VDOR", "env");
let r = App::new("df") let r = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_VDOR")
.validator(|s| { .validator(|s| {
if s == "env" { if s == "env" {
Ok(()) Ok(())
@ -231,7 +231,7 @@ fn validator() {
} }
}), }),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -242,12 +242,12 @@ fn validator() {
#[test] #[test]
fn validator_output() { fn validator_output() {
env::set_var("CLP_TEST_ENV", "42"); env::set_var("CLP_TEST_ENV_VO", "42");
let m = App::new("df") let m = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_VO")
.validator(|s| s.parse::<i32>()), .validator(|s| s.parse::<i32>()),
) )
.get_matches_from(vec![""]); .get_matches_from(vec![""]);
@ -257,12 +257,12 @@ fn validator_output() {
#[test] #[test]
fn validator_invalid() { fn validator_invalid() {
env::set_var("CLP_TEST_ENV", "env"); env::set_var("CLP_TEST_ENV_IV", "env");
let r = App::new("df") let r = App::new("df")
.arg( .arg(
Arg::from("[arg] 'some opt'") Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV") .env("CLP_TEST_ENV_IV")
.validator(|s| { .validator(|s| {
if s != "env" { if s != "env" {
Ok(()) Ok(())
@ -271,7 +271,7 @@ fn validator_invalid() {
} }
}), }),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(r.is_err()); assert!(r.is_err());
} }

View file

@ -18,7 +18,7 @@ fn flag_using_short() {
fn lots_o_flags_sep() { fn lots_o_flags_sep() {
let r = App::new("opts") let r = App::new("opts")
.arg(Arg::from("-o... 'some flag'")) .arg(Arg::from("-o... 'some flag'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
"-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o", "-o",
@ -52,7 +52,7 @@ fn lots_o_flags_sep() {
fn lots_o_flags_combined() { fn lots_o_flags_combined() {
let r = App::new("opts") let r = App::new("opts")
.arg(Arg::from("-o... 'some flag'")) .arg(Arg::from("-o... 'some flag'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "",
"-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", "-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
"-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo", "-oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",

View file

@ -4,7 +4,7 @@ extern crate regex;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
include!("../clap-test.rs"); include!("../clap-test.rs");
use clap::{App, Arg, SubCommand}; use clap::{App, Arg, };
fn get_app() -> App<'static, 'static> { fn get_app() -> App<'static, 'static> {
App::new("myprog") App::new("myprog")
@ -23,14 +23,14 @@ mod tests {
.multiple(true) .multiple(true)
.global(true), .global(true),
) )
.subcommand(SubCommand::with_name("outer").subcommand(SubCommand::with_name("inner"))) .subcommand(App::new("outer").subcommand(App::new("inner")))
} }
#[test] #[test]
fn issue_1076() { fn issue_1076() {
let mut app = get_app(); let mut app = get_app();
let _ = app.get_matches_from_safe_borrow(vec!["myprog"]); let _ = app.try_get_matches_from_mut(vec!["myprog"]);
let _ = app.get_matches_from_safe_borrow(vec!["myprog"]); let _ = app.try_get_matches_from_mut(vec!["myprog"]);
let _ = app.get_matches_from_safe_borrow(vec!["myprog"]); let _ = app.try_get_matches_from_mut(vec!["myprog"]);
} }
} }

View file

@ -32,16 +32,14 @@ For more information try --help";
#[test] #[test]
fn required_group_missing_arg() { fn required_group_missing_arg() {
let result = App::new("group") let result = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg(" -c, --color 'some other flag'")
-c, --color 'some other flag'",
)
.group( .group(
ArgGroup::with_name("req") ArgGroup::with_name("req")
.args(&["flag", "color"]) .args(&["flag", "color"])
.required(true), .required(true),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -51,27 +49,23 @@ fn required_group_missing_arg() {
#[should_panic] #[should_panic]
fn non_existing_arg() { fn non_existing_arg() {
let _ = App::new("group") let _ = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color 'some other flag'")
-c, --color 'some other flag'",
)
.group( .group(
ArgGroup::with_name("req") ArgGroup::with_name("req")
.args(&["flg", "color"]) .args(&["flg", "color"])
.required(true), .required(true),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
} }
#[test] #[test]
fn group_single_value() { fn group_single_value() {
let res = App::new("group") let res = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color [color] 'some option'")
-c, --color [color] 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"])) .group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec!["", "-c", "blue"]); .try_get_matches_from(vec!["", "-c", "blue"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -82,12 +76,10 @@ fn group_single_value() {
#[test] #[test]
fn group_single_flag() { fn group_single_flag() {
let res = App::new("group") let res = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color [color] 'some option'")
-c, --color [color] 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"])) .group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec!["", "-f"]); .try_get_matches_from(vec!["", "-f"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -98,12 +90,10 @@ fn group_single_flag() {
#[test] #[test]
fn group_empty() { fn group_empty() {
let res = App::new("group") let res = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color [color] 'some option'")
-c, --color [color] 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"])) .group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -114,16 +104,14 @@ fn group_empty() {
#[test] #[test]
fn group_reqired_flags_empty() { fn group_reqired_flags_empty() {
let result = App::new("group") let result = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color 'some option'")
-c, --color 'some option'",
)
.group( .group(
ArgGroup::with_name("grp") ArgGroup::with_name("grp")
.required(true) .required(true)
.args(&["flag", "color"]), .args(&["flag", "color"]),
) )
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -132,12 +120,10 @@ fn group_reqired_flags_empty() {
#[test] #[test]
fn group_multi_value_single_arg() { fn group_multi_value_single_arg() {
let res = App::new("group") let res = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color [color]... 'some option'")
-c, --color [color]... 'some option'",
)
.group(ArgGroup::with_name("grp").args(&["flag", "color"])) .group(ArgGroup::with_name("grp").args(&["flag", "color"]))
.get_matches_from_safe(vec!["", "-c", "blue", "red", "green"]); .try_get_matches_from(vec!["", "-c", "blue", "red", "green"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind); assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
@ -153,7 +139,7 @@ fn empty_group() {
let r = App::new("empty_group") let r = App::new("empty_group")
.arg(Arg::from("-f, --flag 'some flag'")) .arg(Arg::from("-f, --flag 'some flag'"))
.group(ArgGroup::with_name("vers").required(true)) .group(ArgGroup::with_name("vers").required(true))
.get_matches_from_safe(vec!["empty_prog"]); .try_get_matches_from(vec!["empty_prog"]);
assert!(r.is_err()); assert!(r.is_err());
let err = r.err().unwrap(); let err = r.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -162,10 +148,8 @@ fn empty_group() {
#[test] #[test]
fn req_group_usage_string() { fn req_group_usage_string() {
let app = App::new("req_group") let app = App::new("req_group")
.args_from_usage( .arg("[base] 'Base commit'")
"[base] 'Base commit' .arg("-d, --delete 'Remove the base commit information'")
-d, --delete 'Remove the base commit information'",
)
.group( .group(
ArgGroup::with_name("base_or_delete") ArgGroup::with_name("base_or_delete")
.args(&["base", "delete"]) .args(&["base", "delete"])
@ -205,17 +189,15 @@ fn req_group_with_conflict_usage_string() {
#[test] #[test]
fn required_group_multiple_args() { fn required_group_multiple_args() {
let result = App::new("group") let result = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color 'some other flag'")
-c, --color 'some other flag'",
)
.group( .group(
ArgGroup::with_name("req") ArgGroup::with_name("req")
.args(&["flag", "color"]) .args(&["flag", "color"])
.required(true) .required(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["group", "-f", "-c"]); .try_get_matches_from(vec!["group", "-f", "-c"]);
assert!(result.is_ok()); assert!(result.is_ok());
let m = result.unwrap(); let m = result.unwrap();
assert!(m.is_present("flag")); assert!(m.is_present("flag"));
@ -225,12 +207,10 @@ fn required_group_multiple_args() {
#[test] #[test]
fn group_multiple_args_error() { fn group_multiple_args_error() {
let result = App::new("group") let result = App::new("group")
.args_from_usage( .arg("-f, --flag 'some flag'")
"-f, --flag 'some flag' .arg("-c, --color 'some other flag'")
-c, --color 'some other flag'",
)
.group(ArgGroup::with_name("req").args(&["flag", "color"])) .group(ArgGroup::with_name("req").args(&["flag", "color"]))
.get_matches_from_safe(vec!["group", "-f", "-c"]); .try_get_matches_from(vec!["group", "-f", "-c"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.unwrap_err(); let err = result.unwrap_err();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);

View file

@ -4,7 +4,7 @@ extern crate regex;
include!("../clap-test.rs"); include!("../clap-test.rs");
use clap::{App, AppSettings, Arg, ArgSettings, ErrorKind, SubCommand}; use clap::{App, AppSettings, Arg, ArgSettings, ErrorKind, };
static REQUIRE_DELIM_HELP: &'static str = "test 1.3 static REQUIRE_DELIM_HELP: &'static str = "test 1.3
Kevin K. Kevin K.
@ -440,8 +440,8 @@ ARGS:
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
-V, --version Prints version information
-v, --verbose Prints out more stuff. -v, --verbose Prints out more stuff.
-V, --version Prints version information
OPTIONS: OPTIONS:
-f, --frequency <HERTZ> The sampling frequency. -f, --frequency <HERTZ> The sampling frequency.
@ -541,7 +541,7 @@ fn setup() -> App<'static, 'static> {
#[test] #[test]
fn help_short() { fn help_short() {
let m = setup().get_matches_from_safe(vec!["myprog", "-h"]); let m = setup().try_get_matches_from(vec!["myprog", "-h"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -549,7 +549,7 @@ fn help_short() {
#[test] #[test]
fn help_long() { fn help_long() {
let m = setup().get_matches_from_safe(vec!["myprog", "--help"]); let m = setup().try_get_matches_from(vec!["myprog", "--help"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -557,7 +557,7 @@ fn help_long() {
#[test] #[test]
fn help_no_subcommand() { fn help_no_subcommand() {
let m = setup().get_matches_from_safe(vec!["myprog", "help"]); let m = setup().try_get_matches_from(vec!["myprog", "help"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::UnknownArgument); assert_eq!(m.unwrap_err().kind, ErrorKind::UnknownArgument);
@ -567,11 +567,11 @@ fn help_no_subcommand() {
fn help_subcommand() { fn help_subcommand() {
let m = setup() let m = setup()
.subcommand( .subcommand(
SubCommand::with_name("test") App::new("test")
.about("tests things") .about("tests things")
.arg_from_usage("-v --verbose 'with verbosity'"), .arg("-v --verbose 'with verbosity'"),
) )
.get_matches_from_safe(vec!["myprog", "help"]); .try_get_matches_from(vec!["myprog", "help"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -643,7 +643,7 @@ fn args_with_last_usage() {
#[test] #[test]
fn subcommand_short_help() { fn subcommand_short_help() {
let m = test::complex_app().get_matches_from_safe(vec!["clap-test", "subcmd", "-h"]); let m = test::complex_app().try_get_matches_from(vec!["clap-test", "subcmd", "-h"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -651,7 +651,7 @@ fn subcommand_short_help() {
#[test] #[test]
fn subcommand_long_help() { fn subcommand_long_help() {
let m = test::complex_app().get_matches_from_safe(vec!["clap-test", "subcmd", "--help"]); let m = test::complex_app().try_get_matches_from(vec!["clap-test", "subcmd", "--help"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -659,7 +659,7 @@ fn subcommand_long_help() {
#[test] #[test]
fn subcommand_help_rev() { fn subcommand_help_rev() {
let m = test::complex_app().get_matches_from_safe(vec!["clap-test", "help", "subcmd"]); let m = test::complex_app().try_get_matches_from(vec!["clap-test", "help", "subcmd"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -693,15 +693,13 @@ fn after_and_before_help_output() {
#[test] #[test]
fn multi_level_sc_help() { fn multi_level_sc_help() {
let app = App::new("ctest").subcommand( let app = App::new("ctest").subcommand(
SubCommand::with_name("subcmd").subcommand( App::new("subcmd").subcommand(
SubCommand::with_name("multi") App::new("multi")
.about("tests subcommands") .about("tests subcommands")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.version("0.1") .version("0.1")
.args_from_usage( .arg("-f, --flag 'tests flags'")
"-f, --flag 'tests flags' .arg("-o, --option [scoption]... 'tests options'")
-o, --option [scoption]... 'tests options'",
),
), ),
); );
assert!(test::compare_output( assert!(test::compare_output(
@ -714,7 +712,7 @@ fn multi_level_sc_help() {
#[test] #[test]
fn no_wrap_help() { fn no_wrap_help() {
let app = App::new("ctest").set_term_width(0).help(MULTI_SC_HELP); let app = App::new("ctest").set_term_width(0).override_help(MULTI_SC_HELP);
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"ctest --help", "ctest --help",
@ -836,7 +834,7 @@ fn issue_626_variable_panic() {
d'Afrique et d'Asie, dans des plantations qui sont cultivées pour les marchés d'exportation. \ d'Afrique et d'Asie, dans des plantations qui sont cultivées pour les marchés d'exportation. \
Le café est souvent une contribution majeure aux exportations des régions productrices.") Le café est souvent une contribution majeure aux exportations des régions productrices.")
.takes_value(true)) .takes_value(true))
.get_matches_from_safe(vec!["ctest", "--help"]); .try_get_matches_from(vec!["ctest", "--help"]);
} }
} }
@ -993,7 +991,7 @@ fn issue_760() {
#[test] #[test]
fn ripgrep_usage() { fn ripgrep_usage() {
let app = App::new("ripgrep").version("0.5").usage( let app = App::new("ripgrep").version("0.5").override_usage(
"rg [OPTIONS] <pattern> [<path> ...] "rg [OPTIONS] <pattern> [<path> ...]
rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...] rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...]
rg [OPTIONS] --files [<path> ...] rg [OPTIONS] --files [<path> ...]
@ -1007,14 +1005,14 @@ fn ripgrep_usage() {
fn ripgrep_usage_using_templates() { fn ripgrep_usage_using_templates() {
let app = App::new("ripgrep") let app = App::new("ripgrep")
.version("0.5") .version("0.5")
.usage( .override_usage(
" "
rg [OPTIONS] <pattern> [<path> ...] rg [OPTIONS] <pattern> [<path> ...]
rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...] rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...]
rg [OPTIONS] --files [<path> ...] rg [OPTIONS] --files [<path> ...]
rg [OPTIONS] --type-list", rg [OPTIONS] --type-list",
) )
.template( .help_template(
"\ "\
{bin} {version} {bin} {version}
@ -1032,9 +1030,9 @@ fn sc_negates_reqs() {
let app = App::new("prog") let app = App::new("prog")
.version("1.0") .version("1.0")
.setting(AppSettings::SubcommandsNegateReqs) .setting(AppSettings::SubcommandsNegateReqs)
.arg_from_usage("-o, --opt <FILE> 'tests options'") .arg("-o, --opt <FILE> 'tests options'")
.arg(Arg::with_name("PATH").help("help")) .arg(Arg::with_name("PATH").help("help"))
.subcommand(SubCommand::with_name("test")); .subcommand(App::new("test"));
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"prog --help", "prog --help",
@ -1047,10 +1045,8 @@ fn sc_negates_reqs() {
fn hidden_args() { fn hidden_args() {
let app = App::new("prog") let app = App::new("prog")
.version("1.0") .version("1.0")
.args_from_usage( .arg("-f, --flag 'testing flags'")
"-f, --flag 'testing flags' .arg("-o, --opt [FILE] 'tests options'")
-o, --opt [FILE] 'tests options'",
)
.arg(Arg::with_name("pos").hidden(true)); .arg(Arg::with_name("pos").hidden(true));
assert!(test::compare_output(app, "prog --help", HIDDEN_ARGS, false)); assert!(test::compare_output(app, "prog --help", HIDDEN_ARGS, false));
} }
@ -1060,12 +1056,10 @@ fn args_negate_sc() {
let app = App::new("prog") let app = App::new("prog")
.version("1.0") .version("1.0")
.setting(AppSettings::ArgsNegateSubcommands) .setting(AppSettings::ArgsNegateSubcommands)
.args_from_usage( .arg("-f, --flag 'testing flags'")
"-f, --flag 'testing flags' .arg("-o, --opt [FILE] 'tests options'")
-o, --opt [FILE] 'tests options'",
)
.arg(Arg::with_name("PATH").help("help")) .arg(Arg::with_name("PATH").help("help"))
.subcommand(SubCommand::with_name("test")); .subcommand(App::new("test"));
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"prog --help", "prog --help",
@ -1078,12 +1072,10 @@ fn args_negate_sc() {
fn issue_1046_hidden_scs() { fn issue_1046_hidden_scs() {
let app = App::new("prog") let app = App::new("prog")
.version("1.0") .version("1.0")
.args_from_usage( .arg("-f, --flag 'testing flags'")
"-f, --flag 'testing flags' .arg("-o, --opt [FILE] 'tests options'")
-o, --opt [FILE] 'tests options'",
)
.arg(Arg::with_name("PATH").help("some")) .arg(Arg::with_name("PATH").help("some"))
.subcommand(SubCommand::with_name("test").setting(AppSettings::Hidden)); .subcommand(App::new("test").setting(AppSettings::Hidden));
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"prog --help", "prog --help",
@ -1108,10 +1100,8 @@ fn customize_version_and_help() {
.version("0.1") .version("0.1")
.author("Nobody <odysseus@example.com>") .author("Nobody <odysseus@example.com>")
.about("You can customize the version and help text") .about("You can customize the version and help text")
.help_short("H") .mut_arg("help", |h| h.short('H').long("help").help("Print help information"))
.help_message("Print help information") .mut_arg("version", |v| v.short('v').long("version").help("Print version information"));
.version_short("v")
.version_message("Print version information");
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"customize --help", "customize --help",
@ -1170,7 +1160,7 @@ fn last_arg_mult_usage_req_with_sc() {
.required(true) .required(true)
.help("some"), .help("some"),
) )
.subcommand(SubCommand::with_name("test").about("some")); .subcommand(App::new("test").about("some"));
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"last --help", "last --help",
@ -1192,7 +1182,7 @@ fn last_arg_mult_usage_with_sc() {
.last(true) .last(true)
.help("some"), .help("some"),
) )
.subcommand(SubCommand::with_name("test").about("some")); .subcommand(App::new("test").about("some"));
assert!(test::compare_output(app, "last --help", LAST_ARG_SC, false)); assert!(test::compare_output(app, "last --help", LAST_ARG_SC, false));
} }
@ -1231,13 +1221,14 @@ fn issue_1112_setup() -> App<'static, 'static> {
.author("Kevin K.") .author("Kevin K.")
.about("tests stuff") .about("tests stuff")
.version("1.3") .version("1.3")
.global_setting(AppSettings::NoAutoHelp)
.arg(Arg::from("-h, --help 'some help'")) .arg(Arg::from("-h, --help 'some help'"))
.subcommand(SubCommand::with_name("foo").arg(Arg::from("-h, --help 'some help'"))) .subcommand(App::new("foo").arg(Arg::from("-h, --help 'some help'")))
} }
#[test] #[test]
fn issue_1112_override_help_long() { fn issue_1112_override_help_long() {
let m = issue_1112_setup().get_matches_from_safe(vec!["test", "--help"]); let m = issue_1112_setup().try_get_matches_from(vec!["test", "--help"]);
assert!(m.is_ok()); assert!(m.is_ok());
assert!(m.unwrap().is_present("help")); assert!(m.unwrap().is_present("help"));
@ -1245,7 +1236,7 @@ fn issue_1112_override_help_long() {
#[test] #[test]
fn issue_1112_override_help_short() { fn issue_1112_override_help_short() {
let m = issue_1112_setup().get_matches_from_safe(vec!["test", "-h"]); let m = issue_1112_setup().try_get_matches_from(vec!["test", "-h"]);
assert!(m.is_ok()); assert!(m.is_ok());
assert!(m.unwrap().is_present("help")); assert!(m.unwrap().is_present("help"));
@ -1253,7 +1244,7 @@ fn issue_1112_override_help_short() {
#[test] #[test]
fn issue_1112_override_help_subcmd_long() { fn issue_1112_override_help_subcmd_long() {
let m = issue_1112_setup().get_matches_from_safe(vec!["test", "foo", "--help"]); let m = issue_1112_setup().try_get_matches_from(vec!["test", "foo", "--help"]);
assert!(m.is_ok()); assert!(m.is_ok());
assert!( assert!(
@ -1266,7 +1257,7 @@ fn issue_1112_override_help_subcmd_long() {
#[test] #[test]
fn issue_1112_override_help_subcmd_short() { fn issue_1112_override_help_subcmd_short() {
let m = issue_1112_setup().get_matches_from_safe(vec!["test", "foo", "-h"]); let m = issue_1112_setup().try_get_matches_from(vec!["test", "foo", "-h"]);
assert!(m.is_ok()); assert!(m.is_ok());
assert!( assert!(
@ -1470,7 +1461,7 @@ FLAGS:
#[test] #[test]
fn show_long_about_issue_897() { fn show_long_about_issue_897() {
let app = App::new("ctest").version("0.1").subcommand( let app = App::new("ctest").version("0.1").subcommand(
SubCommand::with_name("foo") App::new("foo")
.version("0.1") .version("0.1")
.about("About foo") .about("About foo")
.long_about("Long about foo"), .long_about("Long about foo"),
@ -1496,7 +1487,7 @@ FLAGS:
#[test] #[test]
fn show_short_about_issue_897() { fn show_short_about_issue_897() {
let app = App::new("ctest").version("0.1").subcommand( let app = App::new("ctest").version("0.1").subcommand(
SubCommand::with_name("foo") App::new("foo")
.version("0.1") .version("0.1")
.about("About foo") .about("About foo")
.long_about("Long about foo"), .long_about("Long about foo"),

View file

@ -110,7 +110,7 @@ fn quoted_arg_long_name() {
); );
let matches = app let matches = app
.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"]) .try_get_matches_from(vec!["bin_name", "value1", "value2", "--long-option-2"])
.expect("Expected to successfully match the given args."); .expect("Expected to successfully match the given args.");
assert!(matches.is_present("option2")); assert!(matches.is_present("option2"));
} }
@ -148,7 +148,7 @@ fn quoted_arg_name() {
); );
let matches = app let matches = app
.get_matches_from_safe(vec!["bin_name", "value1", "value2", "--long-option-2"]) .try_get_matches_from(vec!["bin_name", "value1", "value2", "--long-option-2"])
.expect("Expected to successfully match the given args."); .expect("Expected to successfully match the given args.");
assert!(matches.is_present("option2")); assert!(matches.is_present("option2"));
} }
@ -166,7 +166,7 @@ fn group_macro() {
) )
); );
let result = app.get_matches_from_safe(vec!["bin_name", "--hard"]); let result = app.try_get_matches_from(vec!["bin_name", "--hard"]);
assert!(result.is_ok()); assert!(result.is_ok());
let matches = result.expect("Expected to successfully match the given args."); let matches = result.expect("Expected to successfully match the given args.");
assert!(matches.is_present("difficulty")); assert!(matches.is_present("difficulty"));
@ -186,7 +186,7 @@ fn group_macro_set_multiple() {
) )
); );
let result = app.get_matches_from_safe(vec!["bin_name", "--hard", "--easy"]); let result = app.try_get_matches_from(vec!["bin_name", "--hard", "--easy"]);
assert!(result.is_ok()); assert!(result.is_ok());
let matches = result.expect("Expected to successfully match the given args."); let matches = result.expect("Expected to successfully match the given args.");
assert!(matches.is_present("difficulty")); assert!(matches.is_present("difficulty"));
@ -208,7 +208,7 @@ fn group_macro_set_not_multiple() {
) )
); );
let result = app.get_matches_from_safe(vec!["bin_name", "--hard", "--easy"]); let result = app.try_get_matches_from(vec!["bin_name", "--hard", "--easy"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.unwrap_err(); let err = result.unwrap_err();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -227,7 +227,7 @@ fn group_macro_set_required() {
) )
); );
let result = app.get_matches_from_safe(vec!["bin_name"]); let result = app.try_get_matches_from(vec!["bin_name"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.unwrap_err(); let err = result.unwrap_err();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -246,7 +246,7 @@ fn group_macro_set_not_required() {
) )
); );
let result = app.get_matches_from_safe(vec!["bin_name"]); let result = app.try_get_matches_from(vec!["bin_name"]);
assert!(result.is_ok()); assert!(result.is_ok());
let matches = result.expect("Expected to successfully match the given args."); let matches = result.expect("Expected to successfully match the given args.");
assert!(!matches.is_present("difficulty")); assert!(!matches.is_present("difficulty"));

View file

@ -1,6 +1,6 @@
extern crate clap; extern crate clap;
use clap::{App, Arg, ErrorKind, SubCommand}; use clap::{App, Arg, ErrorKind, };
#[test] #[test]
fn option_long() { fn option_long() {
@ -12,7 +12,7 @@ fn option_long() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "--option", "val1", "--option", "val2", "--option", "val3", "", "--option", "val1", "--option", "val2", "--option", "val3",
]); ]);
@ -37,7 +37,7 @@ fn option_short() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -61,7 +61,7 @@ fn option_mixed() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "-o", "val1", "--option", "val2", "--option", "val3", "-o", "val4", "", "-o", "val1", "--option", "val2", "--option", "val3", "-o", "val4",
]); ]);
@ -87,7 +87,7 @@ fn option_exact_exact() {
.multiple(true) .multiple(true)
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -110,7 +110,7 @@ fn option_exact_exact_not_mult() {
.takes_value(true) .takes_value(true)
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "val2", "val3"]); .try_get_matches_from(vec!["", "-o", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -134,7 +134,7 @@ fn option_exact_exact_mult() {
.multiple(true) .multiple(true)
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "-o", "val1", "val2", "val3", "-o", "val4", "val5", "val6", "", "-o", "val1", "val2", "val3", "-o", "val4", "val5", "val6",
]); ]);
@ -160,7 +160,7 @@ fn option_exact_less() {
.multiple(true) .multiple(true)
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumberOfValues); assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumberOfValues);
@ -177,7 +177,7 @@ fn option_exact_more() {
.multiple(true) .multiple(true)
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4", "", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4",
]); ]);
@ -196,7 +196,7 @@ fn option_min_exact() {
.multiple(true) .multiple(true)
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -220,7 +220,7 @@ fn option_min_less() {
.multiple(true) .multiple(true)
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::TooFewValues); assert_eq!(m.unwrap_err().kind, ErrorKind::TooFewValues);
@ -238,7 +238,7 @@ fn option_short_min_more_mult_occurs() {
.multiple(true) .multiple(true)
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "pos", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4", "", "pos", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4",
]); ]);
@ -267,7 +267,7 @@ fn option_short_min_more_single_occur() {
.multiple(true) .multiple(true)
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec!["", "pos", "-o", "val1", "val2", "val3", "val4"]); .try_get_matches_from(vec!["", "pos", "-o", "val1", "val2", "val3", "val4"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind); assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
@ -293,7 +293,7 @@ fn option_max_exact() {
.multiple(true) .multiple(true)
.max_values(3), .max_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2", "-o", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -317,7 +317,7 @@ fn option_max_less() {
.multiple(true) .multiple(true)
.max_values(3), .max_values(3),
) )
.get_matches_from_safe(vec!["", "-o", "val1", "-o", "val2"]); .try_get_matches_from(vec!["", "-o", "val1", "-o", "val2"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -341,7 +341,7 @@ fn option_max_more() {
.multiple(true) .multiple(true)
.max_values(3), .max_values(3),
) )
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4", "", "-o", "val1", "-o", "val2", "-o", "val3", "-o", "val4",
]); ]);
@ -357,7 +357,7 @@ fn positional() {
.help("multiple positionals") .help("multiple positionals")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -378,7 +378,7 @@ fn positional_exact_exact() {
.help("multiple positionals") .help("multiple positionals")
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -399,7 +399,7 @@ fn positional_exact_less() {
.help("multiple positionals") .help("multiple positionals")
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2"]); .try_get_matches_from(vec!["myprog", "val1", "val2"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumberOfValues); assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumberOfValues);
@ -413,7 +413,7 @@ fn positional_exact_more() {
.help("multiple positionals") .help("multiple positionals")
.number_of_values(3), .number_of_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3", "val4"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3", "val4"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumberOfValues); assert_eq!(m.unwrap_err().kind, ErrorKind::WrongNumberOfValues);
@ -427,7 +427,7 @@ fn positional_min_exact() {
.help("multiple positionals") .help("multiple positionals")
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -448,7 +448,7 @@ fn positional_min_less() {
.help("multiple positionals") .help("multiple positionals")
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2"]); .try_get_matches_from(vec!["myprog", "val1", "val2"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::TooFewValues); assert_eq!(m.unwrap_err().kind, ErrorKind::TooFewValues);
@ -462,7 +462,7 @@ fn positional_min_more() {
.help("multiple positionals") .help("multiple positionals")
.min_values(3), .min_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3", "val4"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3", "val4"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -483,7 +483,7 @@ fn positional_max_exact() {
.help("multiple positionals") .help("multiple positionals")
.max_values(3), .max_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -504,7 +504,7 @@ fn positional_max_less() {
.help("multiple positionals") .help("multiple positionals")
.max_values(3), .max_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2"]); .try_get_matches_from(vec!["myprog", "val1", "val2"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -525,7 +525,7 @@ fn positional_max_more() {
.help("multiple positionals") .help("multiple positionals")
.max_values(3), .max_values(3),
) )
.get_matches_from_safe(vec!["myprog", "val1", "val2", "val3", "val4"]); .try_get_matches_from(vec!["myprog", "val1", "val2", "val3", "val4"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::TooManyValues); assert_eq!(m.unwrap_err().kind, ErrorKind::TooManyValues);
@ -542,7 +542,7 @@ fn sep_long_equals() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "--option=val1,val2,val3"]); .try_get_matches_from(vec!["", "--option=val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -566,7 +566,7 @@ fn sep_long_space() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "--option", "val1,val2,val3"]); .try_get_matches_from(vec!["", "--option", "val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -590,7 +590,7 @@ fn sep_short_equals() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "-o=val1,val2,val3"]); .try_get_matches_from(vec!["", "-o=val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -614,7 +614,7 @@ fn sep_short_space() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "-o", "val1,val2,val3"]); .try_get_matches_from(vec!["", "-o", "val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -638,7 +638,7 @@ fn sep_short_no_space() {
.takes_value(true) .takes_value(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "-oval1,val2,val3"]); .try_get_matches_from(vec!["", "-oval1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -660,7 +660,7 @@ fn sep_positional() {
.use_delimiter(true) .use_delimiter(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "val1,val2,val3"]); .try_get_matches_from(vec!["", "val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -683,7 +683,7 @@ fn different_sep() {
.takes_value(true) .takes_value(true)
.value_delimiter(";"), .value_delimiter(";"),
) )
.get_matches_from_safe(vec!["", "--option=val1;val2;val3"]); .try_get_matches_from(vec!["", "--option=val1;val2;val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -704,7 +704,7 @@ fn different_sep_positional() {
.help("multiple options") .help("multiple options")
.value_delimiter(";"), .value_delimiter(";"),
) )
.get_matches_from_safe(vec!["", "val1;val2;val3"]); .try_get_matches_from(vec!["", "val1;val2;val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -727,7 +727,7 @@ fn no_sep() {
.takes_value(true) .takes_value(true)
.use_delimiter(false), .use_delimiter(false),
) )
.get_matches_from_safe(vec!["", "--option=val1,val2,val3"]); .try_get_matches_from(vec!["", "--option=val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -745,7 +745,7 @@ fn no_sep_positional() {
.help("multiple options") .help("multiple options")
.use_delimiter(false), .use_delimiter(false),
) )
.get_matches_from_safe(vec!["", "val1,val2,val3"]); .try_get_matches_from(vec!["", "val1,val2,val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -767,7 +767,7 @@ fn req_delimiter_long() {
.takes_value(true), .takes_value(true),
) )
.arg(Arg::with_name("args").multiple(true).index(1)) .arg(Arg::with_name("args").multiple(true).index(1))
.get_matches_from_safe(vec!["", "--option", "val1", "val2", "val3"]); .try_get_matches_from(vec!["", "--option", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -796,7 +796,7 @@ fn req_delimiter_long_with_equal() {
.takes_value(true), .takes_value(true),
) )
.arg(Arg::with_name("args").multiple(true).index(1)) .arg(Arg::with_name("args").multiple(true).index(1))
.get_matches_from_safe(vec!["", "--option=val1", "val2", "val3"]); .try_get_matches_from(vec!["", "--option=val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -825,7 +825,7 @@ fn req_delimiter_short_with_space() {
.takes_value(true), .takes_value(true),
) )
.arg(Arg::with_name("args").multiple(true).index(1)) .arg(Arg::with_name("args").multiple(true).index(1))
.get_matches_from_safe(vec!["", "-o", "val1", "val2", "val3"]); .try_get_matches_from(vec!["", "-o", "val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -854,7 +854,7 @@ fn req_delimiter_short_with_no_space() {
.takes_value(true), .takes_value(true),
) )
.arg(Arg::with_name("args").multiple(true).index(1)) .arg(Arg::with_name("args").multiple(true).index(1))
.get_matches_from_safe(vec!["", "-oval1", "val2", "val3"]); .try_get_matches_from(vec!["", "-oval1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -883,7 +883,7 @@ fn req_delimiter_short_with_equal() {
.takes_value(true), .takes_value(true),
) )
.arg(Arg::with_name("args").multiple(true).index(1)) .arg(Arg::with_name("args").multiple(true).index(1))
.get_matches_from_safe(vec!["", "-o=val1", "val2", "val3"]); .try_get_matches_from(vec!["", "-o=val1", "val2", "val3"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -913,7 +913,7 @@ fn req_delimiter_complex() {
.takes_value(true), .takes_value(true),
) )
.arg(Arg::with_name("args").multiple(true).index(1)) .arg(Arg::with_name("args").multiple(true).index(1))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "",
"val1", "val1",
"-oval2", "-oval2",
@ -974,7 +974,7 @@ fn low_index_positional_not_required() {
.multiple(true), .multiple(true),
) )
.arg(Arg::with_name("target").index(2)) .arg(Arg::with_name("target").index(2))
.get_matches_from_safe(vec!["lip", "file1", "file2", "file3", "target"]); .try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target"]);
} }
#[test] #[test]
@ -993,7 +993,7 @@ fn low_index_positional_last_multiple_too() {
.required(true) .required(true)
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["lip", "file1", "file2", "file3", "target"]); .try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target"]);
} }
#[test] #[test]
@ -1008,7 +1008,7 @@ fn low_index_positional_too_far_back() {
) )
.arg(Arg::with_name("target").required(true).index(2)) .arg(Arg::with_name("target").required(true).index(2))
.arg(Arg::with_name("target2").required(true).index(3)) .arg(Arg::with_name("target2").required(true).index(3))
.get_matches_from_safe(vec!["lip", "file1", "file2", "file3", "target"]); .try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target"]);
} }
#[test] #[test]
@ -1021,7 +1021,7 @@ fn low_index_positional() {
.multiple(true), .multiple(true),
) )
.arg(Arg::with_name("target").index(2).required(true)) .arg(Arg::with_name("target").index(2).required(true))
.get_matches_from_safe(vec!["lip", "file1", "file2", "file3", "target"]); .try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
@ -1041,7 +1041,7 @@ fn low_index_positional() {
fn low_index_positional_in_subcmd() { fn low_index_positional_in_subcmd() {
let m = App::new("lip") let m = App::new("lip")
.subcommand( .subcommand(
SubCommand::with_name("test") App::new("test")
.arg( .arg(
Arg::with_name("files") Arg::with_name("files")
.index(1) .index(1)
@ -1050,7 +1050,7 @@ fn low_index_positional_in_subcmd() {
) )
.arg(Arg::with_name("target").index(2).required(true)), .arg(Arg::with_name("target").index(2).required(true)),
) )
.get_matches_from_safe(vec!["lip", "test", "file1", "file2", "file3", "target"]); .try_get_matches_from(vec!["lip", "test", "file1", "file2", "file3", "target"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
@ -1078,7 +1078,7 @@ fn low_index_positional_with_option() {
) )
.arg(Arg::with_name("target").index(2).required(true)) .arg(Arg::with_name("target").index(2).required(true))
.arg(Arg::with_name("opt").long("option").takes_value(true)) .arg(Arg::with_name("opt").long("option").takes_value(true))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"lip", "file1", "file2", "file3", "target", "--option", "test", "lip", "file1", "file2", "file3", "target", "--option", "test",
]); ]);
@ -1108,7 +1108,7 @@ fn low_index_positional_with_flag() {
) )
.arg(Arg::with_name("target").index(2).required(true)) .arg(Arg::with_name("target").index(2).required(true))
.arg(Arg::with_name("flg").long("flag")) .arg(Arg::with_name("flg").long("flag"))
.get_matches_from_safe(vec!["lip", "file1", "file2", "file3", "target", "--flag"]); .try_get_matches_from(vec!["lip", "file1", "file2", "file3", "target", "--flag"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
@ -1135,7 +1135,7 @@ fn multiple_value_terminator_option() {
.multiple(true), .multiple(true),
) )
.arg(Arg::with_name("other")) .arg(Arg::with_name("other"))
.get_matches_from_safe(vec!["lip", "-f", "val1", "val2", ";", "otherval"]); .try_get_matches_from(vec!["lip", "-f", "val1", "val2", ";", "otherval"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
@ -1161,7 +1161,7 @@ fn multiple_value_terminator_option_other_arg() {
) )
.arg(Arg::with_name("other")) .arg(Arg::with_name("other"))
.arg(Arg::with_name("flag").short('F')) .arg(Arg::with_name("flag").short('F'))
.get_matches_from_safe(vec!["lip", "-f", "val1", "val2", "-F", "otherval"]); .try_get_matches_from(vec!["lip", "-f", "val1", "val2", "-F", "otherval"]);
assert!(m.is_ok(), "{:?}", m.unwrap_err().kind); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
let m = m.unwrap(); let m = m.unwrap();
@ -1186,7 +1186,7 @@ fn multiple_vals_with_hyphen() {
.value_terminator(";"), .value_terminator(";"),
) )
.arg(Arg::with_name("location")) .arg(Arg::with_name("location"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"do", "do",
"find", "find",
"-type", "-type",

View file

@ -318,7 +318,7 @@ fn leading_hyphen_fail() {
fn leading_hyphen_with_flag_after() { fn leading_hyphen_with_flag_after() {
let r = App::new("mvae") let r = App::new("mvae")
.arg(Arg::from("-o [opt]... 'some opt'").setting(ArgSettings::AllowHyphenValues)) .arg(Arg::from("-o [opt]... 'some opt'").setting(ArgSettings::AllowHyphenValues))
.arg_from_usage("-f 'some flag'") .arg("-f 'some flag'")
.try_get_matches_from(vec!["", "-o", "-2", "-f"]); .try_get_matches_from(vec!["", "-o", "-2", "-f"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -331,7 +331,7 @@ fn leading_hyphen_with_flag_after() {
fn leading_hyphen_with_flag_before() { fn leading_hyphen_with_flag_before() {
let r = App::new("mvae") let r = App::new("mvae")
.arg(Arg::from("-o [opt]... 'some opt'").setting(ArgSettings::AllowHyphenValues)) .arg(Arg::from("-o [opt]... 'some opt'").setting(ArgSettings::AllowHyphenValues))
.arg_from_usage("-f 'some flag'") .arg("-f 'some flag'")
.try_get_matches_from(vec!["", "-f", "-o", "-2"]); .try_get_matches_from(vec!["", "-f", "-o", "-2"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
@ -348,7 +348,7 @@ fn leading_hyphen_with_only_pos_follows() {
.number_of_values(1) .number_of_values(1)
.setting(ArgSettings::AllowHyphenValues), .setting(ArgSettings::AllowHyphenValues),
) )
.arg_from_usage("[arg] 'some arg'") .arg("[arg] 'some arg'")
.try_get_matches_from(vec!["", "-o", "-2", "--", "val"]); .try_get_matches_from(vec!["", "-o", "-2", "--", "val"]);
assert!(r.is_ok(), "{:?}", r); assert!(r.is_ok(), "{:?}", r);
let m = r.unwrap(); let m = r.unwrap();
@ -371,7 +371,7 @@ fn did_you_mean() {
#[test] #[test]
fn issue_665() { fn issue_665() {
let res = App::new("tester") let res = App::new("tester")
.arg_from_usage("-v, --reroll-count=[N] 'Mark the patch series as PATCH vN'") .arg("-v, --reroll-count=[N] 'Mark the patch series as PATCH vN'")
.arg(Arg::from( .arg(Arg::from(
"--subject-prefix [Subject-Prefix] 'Use [Subject-Prefix] instead of the standard [PATCH] prefix'") ) "--subject-prefix [Subject-Prefix] 'Use [Subject-Prefix] instead of the standard [PATCH] prefix'") )
.try_get_matches_from(vec!["test", "--subject-prefix", "-v", "2"]); .try_get_matches_from(vec!["test", "--subject-prefix", "-v", "2"]);

View file

@ -9,7 +9,7 @@ fn only_pos_follow() {
Arg::from("-f [flag] 'some opt'"), Arg::from("-f [flag] 'some opt'"),
Arg::from("[arg] 'some arg'"), Arg::from("[arg] 'some arg'"),
]) ])
.get_matches_from_safe(vec!["", "--", "-f"]); .try_get_matches_from(vec!["", "--", "-f"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -21,14 +21,14 @@ fn only_pos_follow() {
fn issue_946() { fn issue_946() {
let r = App::new("compiletest") let r = App::new("compiletest")
.setting(clap::AppSettings::AllowLeadingHyphen) .setting(clap::AppSettings::AllowLeadingHyphen)
.args_from_usage("--exact 'filters match exactly'") .arg("--exact 'filters match exactly'")
.arg( .arg(
clap::Arg::with_name("filter") clap::Arg::with_name("filter")
.index(1) .index(1)
.takes_value(true) .takes_value(true)
.help("filters to apply to output"), .help("filters to apply to output"),
) )
.get_matches_from_safe(vec!["compiletest", "--exact"]); .try_get_matches_from(vec!["compiletest", "--exact"]);
assert!(r.is_ok(), "{:#?}", r); assert!(r.is_ok(), "{:#?}", r);
let matches = r.unwrap(); let matches = r.unwrap();
@ -43,7 +43,7 @@ fn positional() {
Arg::from("-f, --flag 'some flag'"), Arg::from("-f, --flag 'some flag'"),
Arg::with_name("positional").index(1), Arg::with_name("positional").index(1),
]) ])
.get_matches_from_safe(vec!["", "-f", "test"]); .try_get_matches_from(vec!["", "-f", "test"]);
assert!(r.is_ok(), "{:#?}", r); assert!(r.is_ok(), "{:#?}", r);
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("positional")); assert!(m.is_present("positional"));
@ -65,7 +65,7 @@ fn positional() {
fn lots_o_vals() { fn lots_o_vals() {
let r = App::new("opts") let r = App::new("opts")
.arg(Arg::from("[opt]... 'some pos'")) .arg(Arg::from("[opt]... 'some pos'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
"some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some", "some",
@ -108,7 +108,7 @@ fn positional_multiple() {
Arg::from("-f, --flag 'some flag'"), Arg::from("-f, --flag 'some flag'"),
Arg::with_name("positional").index(1).multiple(true), Arg::with_name("positional").index(1).multiple(true),
]) ])
.get_matches_from_safe(vec!["", "-f", "test1", "test2", "test3"]); .try_get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
assert!(r.is_ok(), "{:#?}", r); assert!(r.is_ok(), "{:#?}", r);
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("positional")); assert!(m.is_present("positional"));
@ -126,7 +126,7 @@ fn positional_multiple_3() {
Arg::from("-f, --flag 'some flag'"), Arg::from("-f, --flag 'some flag'"),
Arg::with_name("positional").index(1).multiple(true), Arg::with_name("positional").index(1).multiple(true),
]) ])
.get_matches_from_safe(vec!["", "test1", "test2", "test3", "--flag"]); .try_get_matches_from(vec!["", "test1", "test2", "test3", "--flag"]);
assert!(r.is_ok(), "{:#?}", r); assert!(r.is_ok(), "{:#?}", r);
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("positional")); assert!(m.is_present("positional"));
@ -144,7 +144,7 @@ fn positional_multiple_2() {
Arg::from("-f, --flag 'some flag'"), Arg::from("-f, --flag 'some flag'"),
Arg::with_name("positional").index(1), Arg::with_name("positional").index(1),
]) ])
.get_matches_from_safe(vec!["", "-f", "test1", "test2", "test3"]); .try_get_matches_from(vec!["", "-f", "test1", "test2", "test3"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::UnknownArgument); assert_eq!(err.kind, ErrorKind::UnknownArgument);
@ -159,7 +159,7 @@ fn positional_possible_values() {
.index(1) .index(1)
.possible_value("test123"), .possible_value("test123"),
]) ])
.get_matches_from_safe(vec!["", "-f", "test123"]); .try_get_matches_from(vec!["", "-f", "test123"]);
assert!(r.is_ok(), "{:#?}", r); assert!(r.is_ok(), "{:#?}", r);
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("positional")); assert!(m.is_present("positional"));
@ -186,35 +186,35 @@ fn positional_hyphen_does_not_panic() {
#[test] #[test]
fn single_positional_usage_string() { fn single_positional_usage_string() {
let mut app = App::new("test").arg_from_usage("[FILE] 'some file'"); let mut app = App::new("test").arg("[FILE] 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test [FILE]"); assert_eq!(app.generate_usage(), "USAGE:\n test [FILE]");
} }
#[test] #[test]
fn single_positional_multiple_usage_string() { fn single_positional_multiple_usage_string() {
let mut app = App::new("test").arg_from_usage("[FILE]... 'some file'"); let mut app = App::new("test").arg("[FILE]... 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test [FILE]..."); assert_eq!(app.generate_usage(), "USAGE:\n test [FILE]...");
} }
#[test] #[test]
fn multiple_positional_usage_string() { fn multiple_positional_usage_string() {
let mut app = App::new("test") let mut app = App::new("test")
.arg_from_usage("[FILE] 'some file'") .arg("[FILE] 'some file'")
.arg_from_usage("[FILES]... 'some file'"); .arg("[FILES]... 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test [ARGS]"); assert_eq!(app.generate_usage(), "USAGE:\n test [ARGS]");
} }
#[test] #[test]
fn multiple_positional_one_required_usage_string() { fn multiple_positional_one_required_usage_string() {
let mut app = App::new("test") let mut app = App::new("test")
.arg_from_usage("<FILE> 'some file'") .arg("<FILE> 'some file'")
.arg_from_usage("[FILES]... 'some file'"); .arg("[FILES]... 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test <FILE> [FILES]..."); assert_eq!(app.generate_usage(), "USAGE:\n test <FILE> [FILES]...");
} }
#[test] #[test]
fn single_positional_required_usage_string() { fn single_positional_required_usage_string() {
let mut app = App::new("test").arg_from_usage("<FILE> 'some file'"); let mut app = App::new("test").arg("<FILE> 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test <FILE>"); assert_eq!(app.generate_usage(), "USAGE:\n test <FILE>");
} }
@ -222,9 +222,9 @@ fn single_positional_required_usage_string() {
#[should_panic] #[should_panic]
fn missing_required() { fn missing_required() {
let r = App::new("test") let r = App::new("test")
.arg_from_usage("[FILE1] 'some file'") .arg("[FILE1] 'some file'")
.arg_from_usage("<FILE2> 'some file'") .arg("<FILE2> 'some file'")
.get_matches_from_safe(vec!["test", "file"]); .try_get_matches_from(vec!["test", "file"]);
assert!(r.is_err()); assert!(r.is_err());
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
} }
@ -232,9 +232,9 @@ fn missing_required() {
#[test] #[test]
fn missing_required_2() { fn missing_required_2() {
let r = App::new("test") let r = App::new("test")
.arg_from_usage("<FILE1> 'some file'") .arg("<FILE1> 'some file'")
.arg_from_usage("<FILE2> 'some file'") .arg("<FILE2> 'some file'")
.get_matches_from_safe(vec!["test", "file"]); .try_get_matches_from(vec!["test", "file"]);
assert!(r.is_err()); assert!(r.is_err());
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
} }
@ -242,10 +242,10 @@ fn missing_required_2() {
#[test] #[test]
fn last_positional() { fn last_positional() {
let r = App::new("test") let r = App::new("test")
.arg_from_usage("<TARGET> 'some target'") .arg("<TARGET> 'some target'")
.arg_from_usage("[CORPUS] 'some corpus'") .arg("[CORPUS] 'some corpus'")
.arg(Arg::from("[ARGS]... 'some file'").last(true)) .arg(Arg::from("[ARGS]... 'some file'").last(true))
.get_matches_from_safe(vec!["test", "tgt", "--", "arg"]); .try_get_matches_from(vec!["test", "tgt", "--", "arg"]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert_eq!(m.values_of("ARGS").unwrap().collect::<Vec<_>>(), &["arg"]); assert_eq!(m.values_of("ARGS").unwrap().collect::<Vec<_>>(), &["arg"]);
@ -254,10 +254,10 @@ fn last_positional() {
#[test] #[test]
fn last_positional_no_double_dash() { fn last_positional_no_double_dash() {
let r = App::new("test") let r = App::new("test")
.arg_from_usage("<TARGET> 'some target'") .arg("<TARGET> 'some target'")
.arg_from_usage("[CORPUS] 'some corpus'") .arg("[CORPUS] 'some corpus'")
.arg(Arg::from("[ARGS]... 'some file'").last(true)) .arg(Arg::from("[ARGS]... 'some file'").last(true))
.get_matches_from_safe(vec!["test", "tgt", "crp", "arg"]); .try_get_matches_from(vec!["test", "tgt", "crp", "arg"]);
assert!(r.is_err()); assert!(r.is_err());
assert_eq!(r.unwrap_err().kind, ErrorKind::UnknownArgument); assert_eq!(r.unwrap_err().kind, ErrorKind::UnknownArgument);
} }
@ -265,9 +265,9 @@ fn last_positional_no_double_dash() {
#[test] #[test]
fn last_positional_second_to_last_mult() { fn last_positional_second_to_last_mult() {
let r = App::new("test") let r = App::new("test")
.arg_from_usage("<TARGET> 'some target'") .arg("<TARGET> 'some target'")
.arg_from_usage("[CORPUS]... 'some corpus'") .arg("[CORPUS]... 'some corpus'")
.arg(Arg::from("[ARGS]... 'some file'").last(true)) .arg(Arg::from("[ARGS]... 'some file'").last(true))
.get_matches_from_safe(vec!["test", "tgt", "crp1", "crp2", "--", "arg"]); .try_get_matches_from(vec!["test", "tgt", "crp1", "crp2", "--", "arg"]);
assert!(r.is_ok(), "{:?}", r.unwrap_err().kind); assert!(r.is_ok(), "{:?}", r.unwrap_err().kind);
} }

View file

@ -5,7 +5,7 @@ use clap::{App, Arg, ErrorKind};
fn flag_overrides_itself() { fn flag_overrides_itself() {
let res = App::new("posix") let res = App::new("posix")
.arg(Arg::from("--flag 'some flag'").overrides_with("flag")) .arg(Arg::from("--flag 'some flag'").overrides_with("flag"))
.get_matches_from_safe(vec!["", "--flag", "--flag"]); .try_get_matches_from(vec!["", "--flag", "--flag"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("flag")); assert!(m.is_present("flag"));
@ -16,7 +16,7 @@ fn flag_overrides_itself() {
fn mult_flag_overrides_itself() { fn mult_flag_overrides_itself() {
let res = App::new("posix") let res = App::new("posix")
.arg(Arg::from("--flag... 'some flag'").overrides_with("flag")) .arg(Arg::from("--flag... 'some flag'").overrides_with("flag"))
.get_matches_from_safe(vec!["", "--flag", "--flag", "--flag", "--flag"]); .try_get_matches_from(vec!["", "--flag", "--flag", "--flag", "--flag"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("flag")); assert!(m.is_present("flag"));
@ -27,7 +27,7 @@ fn mult_flag_overrides_itself() {
fn option_overrides_itself() { fn option_overrides_itself() {
let res = App::new("posix") let res = App::new("posix")
.arg(Arg::from("--opt [val] 'some option'").overrides_with("opt")) .arg(Arg::from("--opt [val] 'some option'").overrides_with("opt"))
.get_matches_from_safe(vec!["", "--opt=some", "--opt=other"]); .try_get_matches_from(vec!["", "--opt=some", "--opt=other"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -44,7 +44,7 @@ fn mult_option_require_delim_overrides_itself() {
.number_of_values(1) .number_of_values(1)
.require_delimiter(true), .require_delimiter(true),
) )
.get_matches_from_safe(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]); .try_get_matches_from(vec!["", "--opt=some", "--opt=other", "--opt=one,two"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("opt")); assert!(m.is_present("opt"));
@ -59,7 +59,7 @@ fn mult_option_require_delim_overrides_itself() {
fn mult_option_overrides_itself() { fn mult_option_overrides_itself() {
let res = App::new("posix") let res = App::new("posix")
.arg(Arg::from("--opt [val]... 'some option'").overrides_with("opt")) .arg(Arg::from("--opt [val]... 'some option'").overrides_with("opt"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"", "--opt", "first", "overides", "--opt", "some", "other", "val", "", "--opt", "first", "overides", "--opt", "some", "other", "val",
]); ]);
assert!(res.is_ok()); assert!(res.is_ok());
@ -90,7 +90,7 @@ fn pos_mult_overrides_itself() {
// opts with multiple // opts with multiple
let res = App::new("posix") let res = App::new("posix")
.arg(Arg::from("[val]... 'some pos'").overrides_with("val")) .arg(Arg::from("[val]... 'some pos'").overrides_with("val"))
.get_matches_from_safe(vec!["", "some", "other", "value"]); .try_get_matches_from(vec!["", "some", "other", "value"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
assert!(m.is_present("val")); assert!(m.is_present("val"));
@ -224,7 +224,7 @@ fn conflict_overriden_2() {
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("debug")) .arg(Arg::from("-f, --flag 'some flag'").conflicts_with("debug"))
.arg(Arg::from("-d, --debug 'other flag'")) .arg(Arg::from("-d, --debug 'other flag'"))
.arg(Arg::from("-c, --color 'third flag'").overrides_with("flag")) .arg(Arg::from("-c, --color 'third flag'").overrides_with("flag"))
.get_matches_from_safe(vec!["", "-f", "-d", "-c"]); .try_get_matches_from(vec!["", "-f", "-d", "-c"]);
assert!(result.is_ok()); assert!(result.is_ok());
let m = result.unwrap(); let m = result.unwrap();
assert!(m.is_present("color")); assert!(m.is_present("color"));
@ -238,7 +238,7 @@ fn conflict_overriden_3() {
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("debug")) .arg(Arg::from("-f, --flag 'some flag'").conflicts_with("debug"))
.arg(Arg::from("-d, --debug 'other flag'")) .arg(Arg::from("-d, --debug 'other flag'"))
.arg(Arg::from("-c, --color 'third flag'").overrides_with("flag")) .arg(Arg::from("-c, --color 'third flag'").overrides_with("flag"))
.get_matches_from_safe(vec!["", "-d", "-c", "-f"]); .try_get_matches_from(vec!["", "-d", "-c", "-f"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict); assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -261,7 +261,7 @@ fn pos_required_overridden_by_flag() {
let result = App::new("require_overriden") let result = App::new("require_overriden")
.arg(Arg::with_name("pos").index(1).required(true)) .arg(Arg::with_name("pos").index(1).required(true))
.arg(Arg::from("-c, --color 'some flag'").overrides_with("pos")) .arg(Arg::from("-c, --color 'some flag'").overrides_with("pos"))
.get_matches_from_safe(vec!["", "test", "-c"]); .try_get_matches_from(vec!["", "test", "-c"]);
assert!(result.is_ok(), "{:?}", result.unwrap_err()); assert!(result.is_ok(), "{:?}", result.unwrap_err());
} }
@ -293,7 +293,7 @@ fn require_overriden_4() {
.arg(Arg::from("-f, --flag 'some flag'").requires("debug")) .arg(Arg::from("-f, --flag 'some flag'").requires("debug"))
.arg(Arg::from("-d, --debug 'other flag'")) .arg(Arg::from("-d, --debug 'other flag'"))
.arg(Arg::from("-c, --color 'third flag'").overrides_with("flag")) .arg(Arg::from("-c, --color 'third flag'").overrides_with("flag"))
.get_matches_from_safe(vec!["", "-c", "-f"]); .try_get_matches_from(vec!["", "-c", "-f"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);

View file

@ -37,7 +37,7 @@ fn possible_values_of_positional() {
.index(1) .index(1)
.possible_value("test123"), .possible_value("test123"),
) )
.get_matches_from_safe(vec!["myprog", "test123"]); .try_get_matches_from(vec!["myprog", "test123"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -54,7 +54,7 @@ fn possible_values_of_positional_fail() {
.index(1) .index(1)
.possible_value("test123"), .possible_value("test123"),
) )
.get_matches_from_safe(vec!["myprog", "notest"]); .try_get_matches_from(vec!["myprog", "notest"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
@ -70,7 +70,7 @@ fn possible_values_of_positional_multiple() {
.possible_value("test321") .possible_value("test321")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["myprog", "test123", "test321"]); .try_get_matches_from(vec!["myprog", "test123", "test321"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -92,7 +92,7 @@ fn possible_values_of_positional_multiple_fail() {
.possible_value("test321") .possible_value("test321")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["myprog", "test123", "notest"]); .try_get_matches_from(vec!["myprog", "test123", "notest"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
@ -108,7 +108,7 @@ fn possible_values_of_option() {
.takes_value(true) .takes_value(true)
.possible_value("test123"), .possible_value("test123"),
) )
.get_matches_from_safe(vec!["myprog", "--option", "test123"]); .try_get_matches_from(vec!["myprog", "--option", "test123"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -127,7 +127,7 @@ fn possible_values_of_option_fail() {
.takes_value(true) .takes_value(true)
.possible_value("test123"), .possible_value("test123"),
) )
.get_matches_from_safe(vec!["myprog", "--option", "notest"]); .try_get_matches_from(vec!["myprog", "--option", "notest"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
@ -145,7 +145,7 @@ fn possible_values_of_option_multiple() {
.possible_value("test321") .possible_value("test321")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "--option", "test123", "--option", "test321"]); .try_get_matches_from(vec!["", "--option", "test123", "--option", "test321"]);
assert!(m.is_ok()); assert!(m.is_ok());
let m = m.unwrap(); let m = m.unwrap();
@ -169,7 +169,7 @@ fn possible_values_of_option_multiple_fail() {
.possible_value("test321") .possible_value("test321")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["", "--option", "test123", "--option", "notest"]); .try_get_matches_from(vec!["", "--option", "test123", "--option", "notest"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
@ -197,7 +197,7 @@ fn case_insensitive() {
.possible_value("test321") .possible_value("test321")
.case_insensitive(true), .case_insensitive(true),
) )
.get_matches_from_safe(vec!["pv", "--option", "TeSt123"]); .try_get_matches_from(vec!["pv", "--option", "TeSt123"]);
assert!(m.is_ok()); assert!(m.is_ok());
assert!( assert!(
@ -219,7 +219,7 @@ fn case_insensitive_faili() {
.possible_value("test123") .possible_value("test123")
.possible_value("test321"), .possible_value("test321"),
) )
.get_matches_from_safe(vec!["pv", "--option", "TeSt123"]); .try_get_matches_from(vec!["pv", "--option", "TeSt123"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);
@ -238,7 +238,7 @@ fn case_insensitive_multiple() {
.multiple(true) .multiple(true)
.case_insensitive(true), .case_insensitive(true),
) )
.get_matches_from_safe(vec!["pv", "--option", "TeSt123", "teST123", "tESt321"]); .try_get_matches_from(vec!["pv", "--option", "TeSt123", "teST123", "tESt321"]);
assert!(m.is_ok()); assert!(m.is_ok());
assert_eq!( assert_eq!(
@ -259,7 +259,7 @@ fn case_insensitive_multiple_fail() {
.possible_value("test321") .possible_value("test321")
.multiple(true), .multiple(true),
) )
.get_matches_from_safe(vec!["pv", "--option", "test123", "teST123", "test321"]); .try_get_matches_from(vec!["pv", "--option", "test123", "teST123", "test321"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidValue);

View file

@ -4,7 +4,7 @@ extern crate regex;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
include!("../clap-test.rs"); include!("../clap-test.rs");
use clap::{App, Arg, ArgMatches, ArgSettings, SubCommand}; use clap::{App, Arg, ArgMatches, ArgSettings, };
fn get_app() -> App<'static, 'static> { fn get_app() -> App<'static, 'static> {
App::new("myprog") App::new("myprog")
@ -23,7 +23,7 @@ mod tests {
.setting(ArgSettings::MultipleOccurrences) .setting(ArgSettings::MultipleOccurrences)
.setting(ArgSettings::Global), .setting(ArgSettings::Global),
) )
.subcommand(SubCommand::with_name("outer").subcommand(SubCommand::with_name("inner"))) .subcommand(App::new("outer").subcommand(App::new("inner")))
} }
fn get_matches(app: App<'static, 'static>, argv: &'static str) -> ArgMatches<'static> { fn get_matches(app: App<'static, 'static>, argv: &'static str) -> ArgMatches<'static> {

View file

@ -36,7 +36,7 @@ fn flag_required() {
let result = App::new("flag_required") let result = App::new("flag_required")
.arg(Arg::from("-f, --flag 'some flag'").requires("color")) .arg(Arg::from("-f, --flag 'some flag'").requires("color"))
.arg(Arg::from("-c, --color 'third flag'")) .arg(Arg::from("-c, --color 'third flag'"))
.get_matches_from_safe(vec!["", "-f"]); .try_get_matches_from(vec!["", "-f"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -57,7 +57,7 @@ fn option_required() {
let result = App::new("option_required") let result = App::new("option_required")
.arg(Arg::from("-f [flag] 'some flag'").requires("c")) .arg(Arg::from("-f [flag] 'some flag'").requires("c"))
.arg(Arg::from("-c [color] 'third flag'")) .arg(Arg::from("-c [color] 'third flag'"))
.get_matches_from_safe(vec!["", "-f", "val"]); .try_get_matches_from(vec!["", "-f", "val"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -79,7 +79,7 @@ fn option_required_2() {
fn positional_required() { fn positional_required() {
let result = App::new("positional_required") let result = App::new("positional_required")
.arg(Arg::with_name("flag").index(1).required(true)) .arg(Arg::with_name("flag").index(1).required(true))
.get_matches_from_safe(vec![""]); .try_get_matches_from(vec![""]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -106,7 +106,7 @@ fn group_required() {
) )
.arg(Arg::from("--some 'some arg'")) .arg(Arg::from("--some 'some arg'"))
.arg(Arg::from("--other 'other arg'")) .arg(Arg::from("--other 'other arg'"))
.get_matches_from_safe(vec!["", "-f"]); .try_get_matches_from(vec!["", "-f"]);
assert!(result.is_err()); assert!(result.is_err());
let err = result.err().unwrap(); let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument); assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -210,7 +210,7 @@ fn issue_753() {
) )
.arg(Arg::from("-s, --server=[SERVER_IP] 'NTP server IP address'").required_unless("list")) .arg(Arg::from("-s, --server=[SERVER_IP] 'NTP server IP address'").required_unless("list"))
.arg(Arg::from("-p, --port=[SERVER_PORT] 'NTP server port'").default_value("123")) .arg(Arg::from("-p, --port=[SERVER_PORT] 'NTP server port'").default_value("123"))
.get_matches_from_safe(vec!["test", "--list"]); .try_get_matches_from(vec!["test", "--list"]);
assert!(m.is_ok()); assert!(m.is_ok());
} }
@ -224,7 +224,7 @@ fn required_unless() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.get_matches_from_safe(vec!["unlesstest", "--debug"]); .try_get_matches_from(vec!["unlesstest", "--debug"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -242,7 +242,7 @@ fn required_unless_err() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.get_matches_from_safe(vec!["unlesstest"]); .try_get_matches_from(vec!["unlesstest"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -261,7 +261,7 @@ fn required_unless_all() {
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.arg(Arg::with_name("infile").short('i').takes_value(true)) .arg(Arg::with_name("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessall", "--debug", "-i", "file"]); .try_get_matches_from(vec!["unlessall", "--debug", "-i", "file"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -281,7 +281,7 @@ fn required_unless_all_err() {
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.arg(Arg::with_name("infile").short('i').takes_value(true)) .arg(Arg::with_name("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessall", "--debug"]); .try_get_matches_from(vec!["unlessall", "--debug"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -300,7 +300,7 @@ fn required_unless_one() {
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.arg(Arg::with_name("infile").short('i').takes_value(true)) .arg(Arg::with_name("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessone", "--debug"]); .try_get_matches_from(vec!["unlessone", "--debug"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -321,7 +321,7 @@ fn required_unless_one_2() {
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.arg(Arg::with_name("infile").short('i').takes_value(true)) .arg(Arg::with_name("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessone", "-i", "file"]); .try_get_matches_from(vec!["unlessone", "-i", "file"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -340,7 +340,7 @@ fn required_unless_one_works_with_short() {
.short('x') .short('x')
.required_unless_one(&["a", "b"]), .required_unless_one(&["a", "b"]),
) )
.get_matches_from_safe(vec!["unlessone", "-a"]); .try_get_matches_from(vec!["unlessone", "-a"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -355,7 +355,7 @@ fn required_unless_one_works_with_short_err() {
.short('x') .short('x')
.required_unless_one(&["a", "b"]), .required_unless_one(&["a", "b"]),
) )
.get_matches_from_safe(vec!["unlessone"]); .try_get_matches_from(vec!["unlessone"]);
assert!(!res.is_ok()); assert!(!res.is_ok());
} }
@ -366,7 +366,7 @@ fn required_unless_one_works_without() {
.arg(Arg::with_name("a").conflicts_with("b").short('a')) .arg(Arg::with_name("a").conflicts_with("b").short('a'))
.arg(Arg::with_name("b").short('b')) .arg(Arg::with_name("b").short('b'))
.arg(Arg::with_name("x").required_unless_one(&["a", "b"])) .arg(Arg::with_name("x").required_unless_one(&["a", "b"]))
.get_matches_from_safe(vec!["unlessone", "-a"]); .try_get_matches_from(vec!["unlessone", "-a"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -381,7 +381,7 @@ fn required_unless_one_works_with_long() {
.long("x_is_the_option") .long("x_is_the_option")
.required_unless_one(&["a", "b"]), .required_unless_one(&["a", "b"]),
) )
.get_matches_from_safe(vec!["unlessone", "-a"]); .try_get_matches_from(vec!["unlessone", "-a"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -397,7 +397,7 @@ fn required_unless_one_1() {
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.arg(Arg::with_name("infile").short('i').takes_value(true)) .arg(Arg::with_name("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessone", "--debug"]); .try_get_matches_from(vec!["unlessone", "--debug"]);
assert!(res.is_ok()); assert!(res.is_ok());
let m = res.unwrap(); let m = res.unwrap();
@ -417,7 +417,7 @@ fn required_unless_one_err() {
) )
.arg(Arg::with_name("dbg").long("debug")) .arg(Arg::with_name("dbg").long("debug"))
.arg(Arg::with_name("infile").short('i').takes_value(true)) .arg(Arg::with_name("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessone"]); .try_get_matches_from(vec!["unlessone"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -445,7 +445,7 @@ fn requires_if_present_val() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("extra").long("extra")) .arg(Arg::with_name("extra").long("extra"))
.get_matches_from_safe(vec!["unlessone", "--config=my.cfg"]); .try_get_matches_from(vec!["unlessone", "--config=my.cfg"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -462,7 +462,7 @@ fn requires_if_present_mult() {
) )
.arg(Arg::with_name("extra").long("extra")) .arg(Arg::with_name("extra").long("extra"))
.arg(Arg::with_name("other").long("other")) .arg(Arg::with_name("other").long("other"))
.get_matches_from_safe(vec!["unlessone", "--config=other.cfg"]); .try_get_matches_from(vec!["unlessone", "--config=other.cfg"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -479,7 +479,7 @@ fn requires_if_present_mult_pass() {
) )
.arg(Arg::with_name("extra").long("extra")) .arg(Arg::with_name("extra").long("extra"))
.arg(Arg::with_name("other").long("other")) .arg(Arg::with_name("other").long("other"))
.get_matches_from_safe(vec!["unlessone", "--config=some.cfg"]); .try_get_matches_from(vec!["unlessone", "--config=some.cfg"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -494,7 +494,7 @@ fn requires_if_present_val_no_present_pass() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("extra").long("extra")) .arg(Arg::with_name("extra").long("extra"))
.get_matches_from_safe(vec!["unlessone"]); .try_get_matches_from(vec!["unlessone"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -511,7 +511,7 @@ fn required_if_val_present_pass() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.get_matches_from_safe(vec!["ri", "--extra", "val", "--config", "my.cfg"]); .try_get_matches_from(vec!["ri", "--extra", "val", "--config", "my.cfg"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -526,7 +526,7 @@ fn required_if_val_present_fail() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.get_matches_from_safe(vec!["ri", "--extra", "val"]); .try_get_matches_from(vec!["ri", "--extra", "val"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -576,7 +576,7 @@ fn required_if_wrong_val() {
.long("config"), .long("config"),
) )
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.get_matches_from_safe(vec!["ri", "--extra", "other"]); .try_get_matches_from(vec!["ri", "--extra", "other"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -592,7 +592,7 @@ fn required_ifs_val_present_pass() {
) )
.arg(Arg::with_name("option").takes_value(true).long("option")) .arg(Arg::with_name("option").takes_value(true).long("option"))
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.get_matches_from_safe(vec!["ri", "--option", "spec", "--config", "my.cfg"]); .try_get_matches_from(vec!["ri", "--option", "spec", "--config", "my.cfg"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -608,7 +608,7 @@ fn required_ifs_val_present_fail() {
) )
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.arg(Arg::with_name("option").takes_value(true).long("option")) .arg(Arg::with_name("option").takes_value(true).long("option"))
.get_matches_from_safe(vec!["ri", "--option", "spec"]); .try_get_matches_from(vec!["ri", "--option", "spec"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -625,7 +625,7 @@ fn required_ifs_wrong_val() {
) )
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.arg(Arg::with_name("option").takes_value(true).long("option")) .arg(Arg::with_name("option").takes_value(true).long("option"))
.get_matches_from_safe(vec!["ri", "--option", "other"]); .try_get_matches_from(vec!["ri", "--option", "other"]);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -641,7 +641,7 @@ fn required_ifs_wrong_val_mult_fail() {
) )
.arg(Arg::with_name("extra").takes_value(true).long("extra")) .arg(Arg::with_name("extra").takes_value(true).long("extra"))
.arg(Arg::with_name("option").takes_value(true).long("option")) .arg(Arg::with_name("option").takes_value(true).long("option"))
.get_matches_from_safe(vec!["ri", "--extra", "other", "--option", "spec"]); .try_get_matches_from(vec!["ri", "--extra", "other", "--option", "spec"]);
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument); assert_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -674,19 +674,19 @@ For more information try --help";
fn issue_1158_app() -> App<'static, 'static> { fn issue_1158_app() -> App<'static, 'static> {
App::new("example") App::new("example")
.arg( .arg(
Arg::from_usage("-c, --config [FILE] 'Custom config file.'") Arg::from("-c, --config [FILE] 'Custom config file.'")
.required_unless("ID") .required_unless("ID")
.conflicts_with("ID"), .conflicts_with("ID"),
) )
.arg( .arg(
Arg::from_usage("[ID] 'ID'") Arg::from("[ID] 'ID'")
.required_unless("config") .required_unless("config")
.conflicts_with("config") .conflicts_with("config")
.requires_all(&["x", "y", "z"]), .requires_all(&["x", "y", "z"]),
) )
.arg(Arg::from_usage("-x [X] 'X'")) .arg(Arg::from("-x [X] 'X'"))
.arg(Arg::from_usage("-y [Y] 'Y'")) .arg(Arg::from("-y [Y] 'Y'"))
.arg(Arg::from_usage("-z [Z] 'Z'")) .arg(Arg::from("-z [Z] 'Z'"))
} }
#[test] #[test]

View file

@ -3,7 +3,7 @@ extern crate regex;
include!("../clap-test.rs"); include!("../clap-test.rs");
use clap::{App, Arg, ErrorKind, SubCommand}; use clap::{App, Arg, ErrorKind, };
static VISIBLE_ALIAS_HELP: &'static str = "clap-test 2.6 static VISIBLE_ALIAS_HELP: &'static str = "clap-test 2.6
@ -56,7 +56,7 @@ For more information try --help";
fn subcommand() { fn subcommand() {
let m = App::new("test") let m = App::new("test")
.subcommand( .subcommand(
SubCommand::with_name("some").arg( App::new("some").arg(
Arg::with_name("test") Arg::with_name("test")
.short('t') .short('t')
.long("test") .long("test")
@ -77,7 +77,7 @@ fn subcommand() {
fn subcommand_none_given() { fn subcommand_none_given() {
let m = App::new("test") let m = App::new("test")
.subcommand( .subcommand(
SubCommand::with_name("some").arg( App::new("some").arg(
Arg::with_name("test") Arg::with_name("test")
.short('t') .short('t')
.long("test") .long("test")
@ -95,14 +95,14 @@ fn subcommand_none_given() {
fn subcommand_multiple() { fn subcommand_multiple() {
let m = App::new("test") let m = App::new("test")
.subcommands(vec![ .subcommands(vec![
SubCommand::with_name("some").arg( App::new("some").arg(
Arg::with_name("test") Arg::with_name("test")
.short('t') .short('t')
.long("test") .long("test")
.takes_value(true) .takes_value(true)
.help("testing testing"), .help("testing testing"),
), ),
SubCommand::with_name("add").arg(Arg::with_name("roster").short('r')), App::new("add").arg(Arg::with_name("roster").short('r')),
]) ])
.arg(Arg::with_name("other").long("other")) .arg(Arg::with_name("other").long("other"))
.get_matches_from(vec!["myprog", "some", "--test", "testing"]); .get_matches_from(vec!["myprog", "some", "--test", "testing"]);
@ -118,7 +118,7 @@ fn subcommand_multiple() {
#[test] #[test]
fn single_alias() { fn single_alias() {
let m = App::new("myprog") let m = App::new("myprog")
.subcommand(SubCommand::with_name("test").alias("do-stuff")) .subcommand(App::new("test").alias("do-stuff"))
.get_matches_from(vec!["myprog", "do-stuff"]); .get_matches_from(vec!["myprog", "do-stuff"]);
assert_eq!(m.subcommand_name(), Some("test")); assert_eq!(m.subcommand_name(), Some("test"));
} }
@ -126,7 +126,7 @@ fn single_alias() {
#[test] #[test]
fn multiple_aliases() { fn multiple_aliases() {
let m = App::new("myprog") let m = App::new("myprog")
.subcommand(SubCommand::with_name("test").aliases(&["do-stuff", "test-stuff"])) .subcommand(App::new("test").aliases(&["do-stuff", "test-stuff"]))
.get_matches_from(vec!["myprog", "test-stuff"]); .get_matches_from(vec!["myprog", "test-stuff"]);
assert_eq!(m.subcommand_name(), Some("test")); assert_eq!(m.subcommand_name(), Some("test"));
} }
@ -134,7 +134,7 @@ fn multiple_aliases() {
#[test] #[test]
#[cfg(feature = "suggestions")] #[cfg(feature = "suggestions")]
fn subcmd_did_you_mean_output() { fn subcmd_did_you_mean_output() {
let app = App::new("dym").subcommand(SubCommand::with_name("subcmd")); let app = App::new("dym").subcommand(App::new("subcmd"));
assert!(test::compare_output(app, "dym subcm", DYM_SUBCMD, true)); assert!(test::compare_output(app, "dym subcm", DYM_SUBCMD, true));
} }
@ -142,7 +142,7 @@ fn subcmd_did_you_mean_output() {
#[cfg(feature = "suggestions")] #[cfg(feature = "suggestions")]
fn subcmd_did_you_mean_output_arg() { fn subcmd_did_you_mean_output_arg() {
let app = App::new("dym").subcommand( let app = App::new("dym").subcommand(
SubCommand::with_name("subcmd").arg_from_usage("-s --subcmdarg [subcmdarg] 'tests'"), App::new("subcmd").arg("-s --subcmdarg [subcmdarg] 'tests'"),
); );
assert!(test::compare_output(app, "dym --subcm foo", DYM_ARG, true)); assert!(test::compare_output(app, "dym --subcm foo", DYM_ARG, true));
} }
@ -150,8 +150,8 @@ fn subcmd_did_you_mean_output_arg() {
#[test] #[test]
fn alias_help() { fn alias_help() {
let m = App::new("myprog") let m = App::new("myprog")
.subcommand(SubCommand::with_name("test").alias("do-stuff")) .subcommand(App::new("test").alias("do-stuff"))
.get_matches_from_safe(vec!["myprog", "help", "do-stuff"]); .try_get_matches_from(vec!["myprog", "help", "do-stuff"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
} }
@ -159,7 +159,7 @@ fn alias_help() {
#[test] #[test]
fn visible_aliases_help_output() { fn visible_aliases_help_output() {
let app = App::new("clap-test").version("2.6").subcommand( let app = App::new("clap-test").version("2.6").subcommand(
SubCommand::with_name("test") App::new("test")
.about("Some help") .about("Some help")
.alias("invisible") .alias("invisible")
.visible_alias("dongle") .visible_alias("dongle")
@ -176,7 +176,7 @@ fn visible_aliases_help_output() {
#[test] #[test]
fn invisible_aliases_help_output() { fn invisible_aliases_help_output() {
let app = App::new("clap-test").version("2.6").subcommand( let app = App::new("clap-test").version("2.6").subcommand(
SubCommand::with_name("test") App::new("test")
.about("Some help") .about("Some help")
.alias("invisible"), .alias("invisible"),
); );
@ -192,8 +192,8 @@ fn invisible_aliases_help_output() {
fn issue_1031_args_with_same_name() { fn issue_1031_args_with_same_name() {
let res = App::new("prog") let res = App::new("prog")
.arg(Arg::from("--ui-path=<PATH>")) .arg(Arg::from("--ui-path=<PATH>"))
.subcommand(SubCommand::with_name("signer")) .subcommand(App::new("signer"))
.get_matches_from_safe(vec!["prog", "--ui-path", "signer"]); .try_get_matches_from(vec!["prog", "--ui-path", "signer"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind); assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
@ -204,8 +204,8 @@ fn issue_1031_args_with_same_name() {
fn issue_1031_args_with_same_name_no_more_vals() { fn issue_1031_args_with_same_name_no_more_vals() {
let res = App::new("prog") let res = App::new("prog")
.arg(Arg::from("--ui-path=<PATH>")) .arg(Arg::from("--ui-path=<PATH>"))
.subcommand(SubCommand::with_name("signer")) .subcommand(App::new("signer"))
.get_matches_from_safe(vec!["prog", "--ui-path", "value", "signer"]); .try_get_matches_from(vec!["prog", "--ui-path", "value", "signer"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind); assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap(); let m = res.unwrap();
@ -220,7 +220,7 @@ fn issue_1161_multiple_hyphen_hyphen() {
.arg(Arg::with_name("eff").short('f')) .arg(Arg::with_name("eff").short('f'))
.arg(Arg::with_name("pea").short('p').takes_value(true)) .arg(Arg::with_name("pea").short('p').takes_value(true))
.arg(Arg::with_name("slop").multiple(true).last(true)) .arg(Arg::with_name("slop").multiple(true).last(true))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
"-f", "-f",
"-p=bob", "-p=bob",
"--", "--",

View file

@ -1,7 +1,7 @@
extern crate clap; extern crate clap;
extern crate regex; extern crate regex;
use clap::{App, SubCommand}; use clap::{App, };
include!("../clap-test.rs"); include!("../clap-test.rs");
@ -51,7 +51,7 @@ SUBCOMMANDS:
#[test] #[test]
fn with_template() { fn with_template() {
let app = app_example1().template(EXAMPLE1_TMPL_S); let app = app_example1().help_template(EXAMPLE1_TMPL_S);
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"MyApp --help", "MyApp --help",
@ -62,7 +62,7 @@ fn with_template() {
#[test] #[test]
fn custom_template() { fn custom_template() {
let app = app_example1().template(EXAMPLE1_TMPS_F); let app = app_example1().help_template(EXAMPLE1_TMPS_F);
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"MyApp --help", "MyApp --help",
@ -77,7 +77,7 @@ fn template_empty() {
.version("1.0") .version("1.0")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things") .about("Does awesome things")
.template(""); .help_template("");
assert!(test::compare_output(app, "MyApp --help", "", false)); assert!(test::compare_output(app, "MyApp --help", "", false));
} }
@ -87,7 +87,7 @@ fn template_notag() {
.version("1.0") .version("1.0")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things") .about("Does awesome things")
.template("test no tag test"); .help_template("test no tag test");
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"MyApp --help", "MyApp --help",
@ -102,7 +102,7 @@ fn template_unknowntag() {
.version("1.0") .version("1.0")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things") .about("Does awesome things")
.template("test {unknown_tag} test"); .help_template("test {unknown_tag} test");
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"MyApp --help", "MyApp --help",
@ -117,7 +117,7 @@ fn template_author_version() {
.version("1.0") .version("1.0")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things") .about("Does awesome things")
.template("{author}\n{version}\n{about}\n{bin}"); .help_template("{author}\n{version}\n{about}\n{bin}");
assert!(test::compare_output( assert!(test::compare_output(
app, app,
"MyApp --help", "MyApp --help",
@ -133,14 +133,12 @@ fn app_example1<'b, 'c>() -> App<'b, 'c> {
.version("1.0") .version("1.0")
.author("Kevin K. <kbknapp@gmail.com>") .author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things") .about("Does awesome things")
.args_from_usage( .arg("-c, --config=[FILE] 'Sets a custom config file'")
"-c, --config=[FILE] 'Sets a custom config file' .arg("<output> 'Sets an optional output file'")
<output> 'Sets an optional output file' .arg("-d... 'Turn debugging information on'")
-d... 'Turn debugging information on'",
)
.subcommand( .subcommand(
SubCommand::with_name("test") App::new("test")
.about("does testing things") .about("does testing things")
.arg_from_usage("-l, --list 'lists test values'"), .arg("-l, --list 'lists test values'"),
) )
} }

View file

@ -10,7 +10,7 @@ fn unique_arg_names() {
Arg::with_name("arg").short('a'), Arg::with_name("arg").short('a'),
Arg::with_name("arg").short('b'), Arg::with_name("arg").short('b'),
]) ])
.get_matches_safe(); .try_get_matches();
} }
#[test] #[test]
@ -21,7 +21,7 @@ fn unique_arg_shorts() {
Arg::with_name("arg1").short('a'), Arg::with_name("arg1").short('a'),
Arg::with_name("arg2").short('a'), Arg::with_name("arg2").short('a'),
]) ])
.get_matches_safe(); .try_get_matches();
} }
#[test] #[test]
@ -32,5 +32,5 @@ fn unique_arg_longs() {
Arg::with_name("arg1").long("long"), Arg::with_name("arg1").long("long"),
Arg::with_name("arg2").long("long"), Arg::with_name("arg2").long("long"),
]) ])
.get_matches_safe(); .try_get_matches();
} }

View file

@ -11,7 +11,7 @@ fn invalid_utf8_strict_positional() {
let m = App::new("bad_utf8") let m = App::new("bad_utf8")
.arg(Arg::from("<arg> 'some arg'")) .arg(Arg::from("<arg> 'some arg'"))
.setting(AppSettings::StrictUtf8) .setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]); .try_get_matches_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8); assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
} }
@ -21,7 +21,7 @@ fn invalid_utf8_strict_option_short_space() {
let m = App::new("bad_utf8") let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8) .setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from("-a"), OsString::from("-a"),
OsString::from_vec(vec![0xe9]), OsString::from_vec(vec![0xe9]),
@ -35,7 +35,7 @@ fn invalid_utf8_strict_option_short_equals() {
let m = App::new("bad_utf8") let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8) .setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]), OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
]); ]);
@ -48,7 +48,7 @@ fn invalid_utf8_strict_option_short_no_space() {
let m = App::new("bad_utf8") let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8) .setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]), OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]); ]);
@ -61,7 +61,7 @@ fn invalid_utf8_strict_option_long_space() {
let m = App::new("bad_utf8") let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8) .setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from("--arg"), OsString::from("--arg"),
OsString::from_vec(vec![0xe9]), OsString::from_vec(vec![0xe9]),
@ -75,7 +75,7 @@ fn invalid_utf8_strict_option_long_equals() {
let m = App::new("bad_utf8") let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8) .setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]), OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
]); ]);
@ -87,7 +87,7 @@ fn invalid_utf8_strict_option_long_equals() {
fn invalid_utf8_lossy_positional() { fn invalid_utf8_lossy_positional() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("<arg> 'some arg'")) .arg(Arg::from("<arg> 'some arg'"))
.get_matches_from_safe(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]); .try_get_matches_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -98,7 +98,7 @@ fn invalid_utf8_lossy_positional() {
fn invalid_utf8_lossy_option_short_space() { fn invalid_utf8_lossy_option_short_space() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from("-a"), OsString::from("-a"),
OsString::from_vec(vec![0xe9]), OsString::from_vec(vec![0xe9]),
@ -113,7 +113,7 @@ fn invalid_utf8_lossy_option_short_space() {
fn invalid_utf8_lossy_option_short_equals() { fn invalid_utf8_lossy_option_short_equals() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]), OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
]); ]);
@ -127,7 +127,7 @@ fn invalid_utf8_lossy_option_short_equals() {
fn invalid_utf8_lossy_option_short_no_space() { fn invalid_utf8_lossy_option_short_no_space() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]), OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]); ]);
@ -141,7 +141,7 @@ fn invalid_utf8_lossy_option_short_no_space() {
fn invalid_utf8_lossy_option_long_space() { fn invalid_utf8_lossy_option_long_space() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from("--arg"), OsString::from("--arg"),
OsString::from_vec(vec![0xe9]), OsString::from_vec(vec![0xe9]),
@ -156,7 +156,7 @@ fn invalid_utf8_lossy_option_long_space() {
fn invalid_utf8_lossy_option_long_equals() { fn invalid_utf8_lossy_option_long_equals() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]), OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
]); ]);
@ -170,7 +170,7 @@ fn invalid_utf8_lossy_option_long_equals() {
fn invalid_utf8_positional() { fn invalid_utf8_positional() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("<arg> 'some arg'")) .arg(Arg::from("<arg> 'some arg'"))
.get_matches_from_safe(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]); .try_get_matches_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
assert!(r.is_ok()); assert!(r.is_ok());
let m = r.unwrap(); let m = r.unwrap();
assert!(m.is_present("arg")); assert!(m.is_present("arg"));
@ -184,7 +184,7 @@ fn invalid_utf8_positional() {
fn invalid_utf8_option_short_space() { fn invalid_utf8_option_short_space() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from("-a"), OsString::from("-a"),
OsString::from_vec(vec![0xe9]), OsString::from_vec(vec![0xe9]),
@ -202,7 +202,7 @@ fn invalid_utf8_option_short_space() {
fn invalid_utf8_option_short_equals() { fn invalid_utf8_option_short_equals() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]), OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
]); ]);
@ -219,7 +219,7 @@ fn invalid_utf8_option_short_equals() {
fn invalid_utf8_option_short_no_space() { fn invalid_utf8_option_short_no_space() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]), OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]); ]);
@ -236,7 +236,7 @@ fn invalid_utf8_option_short_no_space() {
fn invalid_utf8_option_long_space() { fn invalid_utf8_option_long_space() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from("--arg"), OsString::from("--arg"),
OsString::from_vec(vec![0xe9]), OsString::from_vec(vec![0xe9]),
@ -254,7 +254,7 @@ fn invalid_utf8_option_long_space() {
fn invalid_utf8_option_long_equals() { fn invalid_utf8_option_long_equals() {
let r = App::new("bad_utf8") let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'")) .arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![ .try_get_matches_from(vec![
OsString::from(""), OsString::from(""),
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]), OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
]); ]);

View file

@ -3,7 +3,7 @@ extern crate regex;
use std::str; use std::str;
use clap::{App, Arg, ErrorKind}; use clap::{App, Arg, ErrorKind, AppSettings};
include!("../clap-test.rs"); include!("../clap-test.rs");
@ -15,7 +15,7 @@ fn version_short() {
.author("Kevin K.") .author("Kevin K.")
.about("tests stuff") .about("tests stuff")
.version("1.3") .version("1.3")
.get_matches_from_safe(vec!["myprog", "-V"]); .try_get_matches_from(vec!["myprog", "-V"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed);
@ -27,7 +27,7 @@ fn version_long() {
.author("Kevin K.") .author("Kevin K.")
.about("tests stuff") .about("tests stuff")
.version("1.3") .version("1.3")
.get_matches_from_safe(vec!["myprog", "--version"]); .try_get_matches_from(vec!["myprog", "--version"]);
assert!(m.is_err()); assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed); assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed);
@ -36,7 +36,7 @@ fn version_long() {
#[test] #[test]
fn complex_version_output() { fn complex_version_output() {
let mut a = App::new("clap-test").version("v1.4.8"); let mut a = App::new("clap-test").version("v1.4.8");
let _ = a.get_matches_from_safe_borrow(vec![""]); let _ = a.try_get_matches_from_mut(vec![""]);
// Now we check the output of print_version() // Now we check the output of print_version()
let mut ver = vec![]; let mut ver = vec![];
@ -47,12 +47,13 @@ fn complex_version_output() {
#[test] #[test]
fn override_ver() { fn override_ver() {
let m = App::new("test") let m = App::new("test")
.setting(AppSettings::NoAutoVersion)
.author("Kevin K.") .author("Kevin K.")
.about("tests stuff") .about("tests stuff")
.version("1.3") .version("1.3")
.arg(Arg::from("-v, --version 'some version'")) .mut_arg("version", |a| a.short('v').long("version").help("some version"))
.get_matches_from_safe(vec!["test", "--version"]); .try_get_matches_from(vec!["test", "--version"]);
assert!(m.is_ok()); assert!(m.is_ok(), "{:?}", m.unwrap_err().kind);
assert!(m.unwrap().is_present("version")); assert!(m.unwrap().is_present("version"));
} }

View file

@ -23,7 +23,7 @@ fn help_message() {
let yml = load_yaml!("app.yml"); let yml = load_yaml!("app.yml");
let mut app = App::from_yaml(yml); let mut app = App::from_yaml(yml);
// Generate the full help message! // Generate the full help message!
let _ = app.get_matches_from_safe_borrow(Vec::<String>::new()); let _ = app.try_get_matches_from_mut(Vec::<String>::new());
let mut help_buffer = Vec::new(); let mut help_buffer = Vec::new();
app.write_help(&mut help_buffer).unwrap(); app.write_help(&mut help_buffer).unwrap();
@ -38,7 +38,7 @@ fn author() {
let yml = load_yaml!("app.yml"); let yml = load_yaml!("app.yml");
let mut app = App::from_yaml(yml); let mut app = App::from_yaml(yml);
// Generate the full help message! // Generate the full help message!
let _ = app.get_matches_from_safe_borrow(Vec::<String>::new()); let _ = app.try_get_matches_from_mut(Vec::<String>::new());
let mut help_buffer = Vec::new(); let mut help_buffer = Vec::new();
app.write_help(&mut help_buffer).unwrap(); app.write_help(&mut help_buffer).unwrap();

View file

@ -11,6 +11,9 @@
* Ability to mutate args once they've been added to an `App` * Ability to mutate args once they've been added to an `App`
* `App::args` and `App::arg` are more generic * `App::args` and `App::arg` are more generic
* Can unset global settings * Can unset global settings
* Instead of adding arg with long `--help` or `--version` you can use `App::mut_arg` to override things
* Caution, must fully override
* No longer forces auto-handle of help/ver however if still desired `AppSettings::NoAuto{Help,Version}`
# How to Upgrade # How to Upgrade