Merge branch 'v3-master' into map

This commit is contained in:
Kevin K 2018-11-13 19:44:43 -05:00
commit 0de9e07412
No known key found for this signature in database
GPG key ID: 17218E4B3692F01A
73 changed files with 1205 additions and 6970 deletions

View file

@ -3,13 +3,8 @@ language: rust
cache: cargo
rust:
- nightly
- nightly-2018-06-19
- beta
- stable
- 1.20.0
matrix:
allow_failures:
- rust: nightly
before_script:
- |
pip install git+git://github.com/kbknapp/travis-cargo.git --user &&

View file

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

View file

@ -289,7 +289,7 @@ fn main() {
.about("controls testing features")
.version("1.3")
.author("Someone E. <someone_else@other.com>")
.arg_from_usage("-d, --debug 'Print debug information'"))
.arg("-d, --debug 'Print debug information'"))
.get_matches();
// 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:
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:
@ -405,7 +405,7 @@ To test out `clap`'s default auto-generated help/version follow these steps:
```toml
[dependencies]
clap = "3.0.0-alpha.1"
clap = "3.0.0-beta.1"
```
* 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
[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))
@ -458,7 +458,7 @@ To disable these, add this to your `Cargo.toml`:
```toml
[dependencies.clap]
version = "3.0.0-alpha.1"
version = "3.0.0-beta.1"
default-features = false
```
@ -466,7 +466,7 @@ You can also selectively enable only the features you'd like to include, by addi
```toml
[dependencies.clap]
version = "3.0.0-alpha.1"
version = "3.0.0-beta.1"
default-features = false
# 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
[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.
@ -526,11 +526,11 @@ Right now Cargo's version resolution is pretty naive, it's just a brute-force se
# In one Cargo.toml
[dependencies]
clap = "~3.0.0-alpha.1"
clap = "~3.0.0-beta.1"
# In another Cargo.toml
[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.

View file

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

View file

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

View file

@ -7,7 +7,7 @@
extern crate clap;
extern crate test;
use clap::{App, AppSettings, Arg, ArgGroup, ArgSettings, Shell, SubCommand};
use clap::{App, AppSettings, Arg, ArgGroup, ArgSettings, Shell, };
use test::Bencher;
@ -34,16 +34,16 @@ pub fn build_cli() -> App<'static, 'static> {
.help("Enable verbose output")
.short('v')
.long("verbose"))
.subcommand(SubCommand::with_name("show")
.subcommand(App::new("show")
.about("Show the active and installed toolchains")
.after_help(SHOW_HELP))
.subcommand(SubCommand::with_name("install")
.subcommand(App::new("install")
.about("Update Rust toolchains")
.after_help(TOOLCHAIN_INSTALL_HELP)
.setting(AppSettings::Hidden) // synonym for 'toolchain install'
.arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("update")
.subcommand(App::new("update")
.about("Update Rust toolchains")
.after_help(UPDATE_HELP)
.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")
.long("no-self-update")
.setting(ArgSettings::Hidden)))
.subcommand(SubCommand::with_name("default")
.subcommand(App::new("default")
.about("Set the default toolchain")
.after_help(DEFAULT_HELP)
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("toolchain")
.subcommand(App::new("toolchain")
.about("Modify or query the installed toolchains")
.after_help(TOOLCHAIN_HELP)
.setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list").about("List installed toolchains"))
.subcommand(SubCommand::with_name("install")
.subcommand(App::new("list").about("List installed toolchains"))
.subcommand(App::new("install")
.about("Install or update a given toolchain")
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("uninstall")
.subcommand(App::new("uninstall")
.about("Uninstall a toolchain")
.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")
.arg(Arg::with_name("toolchain").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'
.arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("add")
.subcommand(App::new("add")
.setting(AppSettings::Hidden) // synonym for 'install'
.arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("remove")
.subcommand(App::new("remove")
.setting(AppSettings::Hidden) // synonym for 'uninstall'
.arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required))))
.subcommand(SubCommand::with_name("target")
.subcommand(App::new("target")
.about("Modify a toolchain's supported targets")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list")
.subcommand(App::new("list")
.about("List installed and available targets")
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("add")
.subcommand(App::new("add")
.about("Add a target to a Rust toolchain")
.arg(Arg::with_name("target").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("remove")
.subcommand(App::new("remove")
.about("Remove a target from a Rust toolchain")
.arg(Arg::with_name("target").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("install")
.subcommand(App::new("install")
.setting(AppSettings::Hidden) // synonym for 'add'
.arg(Arg::with_name("target")
.setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("uninstall")
.subcommand(App::new("uninstall")
.setting(AppSettings::Hidden) // synonym for 'remove'
.arg(Arg::with_name("target")
.setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue))))
.subcommand(SubCommand::with_name("component")
.subcommand(App::new("component")
.about("Modify a toolchain's installed components")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list")
.subcommand(App::new("list")
.about("List installed and available components")
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("add")
.subcommand(App::new("add")
.about("Add a component to a Rust toolchain")
.arg(Arg::with_name("component").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
@ -138,7 +138,7 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("target")
.long("target")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("remove")
.subcommand(App::new("remove")
.about("Remove a component from a Rust toolchain")
.arg(Arg::with_name("component").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
@ -147,17 +147,17 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("target")
.long("target")
.setting(ArgSettings::TakesValue))))
.subcommand(SubCommand::with_name("override")
.subcommand(App::new("override")
.about("Modify directory toolchain overrides")
.after_help(OVERRIDE_HELP)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
// .setting(AppSettings::SubcommandRequiredElseHelp)
.subcommand(SubCommand::with_name("list").about("List directory toolchain overrides"))
.subcommand(SubCommand::with_name("set")
.subcommand(App::new("list").about("List directory toolchain overrides"))
.subcommand(App::new("set")
.about("Set the override toolchain for a directory")
.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")
.after_help(OVERRIDE_UNSET_HELP)
.arg(Arg::with_name("path")
@ -167,11 +167,11 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("nonexistent")
.long("nonexistent")
.help("Remove override toolchain for all nonexistent directories")))
.subcommand(SubCommand::with_name("add")
.subcommand(App::new("add")
.setting(AppSettings::Hidden) // synonym for 'set'
.arg(Arg::with_name("toolchain")
.setting(ArgSettings::Required)))
.subcommand(SubCommand::with_name("remove")
.subcommand(App::new("remove")
.setting(AppSettings::Hidden) // synonym for 'unset'
.about("Remove the override toolchain for a directory")
.arg(Arg::with_name("path")
@ -180,17 +180,17 @@ pub fn build_cli() -> App<'static, 'static> {
.arg(Arg::with_name("nonexistent")
.long("nonexistent")
.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")
.after_help(RUN_HELP)
.setting(AppSettings::TrailingVarArg)
.arg(Arg::with_name("toolchain").setting(ArgSettings::Required))
.arg(Arg::with_name("command")
.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")
.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")
.after_help(DOC_HELP)
.arg(Arg::with_name("book")
@ -200,37 +200,37 @@ pub fn build_cli() -> App<'static, 'static> {
.long("std")
.help("Standard library API documentation"))
.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")
.arg(Arg::with_name("command").setting(ArgSettings::Required))
.arg(Arg::with_name("toolchain")
.long("toolchain")
.setting(ArgSettings::TakesValue)))
.subcommand(SubCommand::with_name("self")
.subcommand(App::new("self")
.about("Modify the rustup installation")
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.subcommand(SubCommand::with_name("update")
.subcommand(App::new("update")
.about("Download and install updates to rustup"))
.subcommand(SubCommand::with_name("uninstall")
.subcommand(App::new("uninstall")
.about("Uninstall rustup.")
.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.")))
.subcommand(SubCommand::with_name("telemetry")
.subcommand(App::new("telemetry")
.about("rustup telemetry commands")
.setting(AppSettings::Hidden)
.setting(AppSettings::VersionlessSubcommands)
.setting(AppSettings::DeriveDisplayOrder)
.subcommand(SubCommand::with_name("enable").about("Enable rustup telemetry"))
.subcommand(SubCommand::with_name("disable").about("Disable rustup telemetry"))
.subcommand(SubCommand::with_name("analyze").about("Analyze stored telemetry")))
.subcommand(SubCommand::with_name("set")
.subcommand(App::new("enable").about("Enable rustup telemetry"))
.subcommand(App::new("disable").about("Disable rustup telemetry"))
.subcommand(App::new("analyze").about("Analyze stored telemetry")))
.subcommand(App::new("set")
.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")
.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")
.after_help(COMPLETIONS_HELP)
.setting(AppSettings::ArgRequiredElseHelp)

View file

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

View file

@ -33,7 +33,7 @@ where
pub fn compare_output(l: App, args: &str, right: &str, stderr: bool) -> bool {
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();
err.write_to(&mut buf).unwrap();
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 {
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();
err.write_to(&mut buf).unwrap();
let content = buf.into_inner();

View file

@ -1,6 +1,6 @@
extern crate clap;
use clap::{App, SubCommand};
use clap::{App, };
fn main() {
// 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")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.args_from_usage(
"-c, --config=[FILE] 'Sets a custom config file'
<output> 'Sets an optional output file'
-d... 'Turn debugging information on'",
)
.arg("-c, --config=[FILE] 'Sets a custom config file'")
.arg("<output> 'Sets an optional output file'")
.arg("-d... 'Turn debugging information on'")
.subcommand(
SubCommand::with_name("test")
App::new("test")
.about("does testing things")
.arg_from_usage("-l, --list 'lists test values'"),
.arg("-l, --list 'lists test values'"),
)
.get_matches();

View file

@ -1,6 +1,6 @@
extern crate clap;
use clap::{App, Arg, SubCommand};
use clap::{App, Arg, };
fn main() {
// 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"),
)
.subcommand(
SubCommand::with_name("test")
App::new("test")
.about("does testing things")
.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
// 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
// will be covered later.
//

View file

@ -5,14 +5,14 @@ use clap::{App, Arg};
fn main() {
// 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
// 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
// methods describing various settings for the individual arguments. Or by supplying a "usage"
// 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
// 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().
//
// 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
// 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()
//
//
// 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
.args_from_usage("[output] 'Supply an output file to use'
-i, --int=[IFACE] 'Set an interface to use'")
.arg("[output] 'Supply an output file to use'")
.arg("-i, --int=[IFACE] 'Set an interface to use'")
.get_matches();
// 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;
use clap::{App, Arg, SubCommand};
use clap::{App, Arg, };
fn main() {
// SubCommands 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
// s function exactly like sub-Apps, because that's exactly what they are. Each
// instance of a can have it's own version, author(s), Args, and even it's own
// subcommands.
//
// # Help and Version
@ -16,14 +16,14 @@ fn main() {
// 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
// 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")
// Normal App and Arg configuration goes here...
// In the following example assume we wanted an application which
// supported an "add" subcommand, this "add" subcommand also took
// 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"
// or "myapp help"
.version("0.1") // Subcommands can have independent version

View file

@ -28,9 +28,8 @@ fn main() {
let matches = App::new("myapp")
// Create two arguments, a required positional which accepts multiple values
// and an optional '-l value'
.args_from_usage(
"<seq>... 'A sequence of whole positive numbers, i.e. 20 25 30'
-l [len] 'A length to use, defaults to 10 when omitted'")
.arg("<seq>... 'A sequence of whole positive numbers, i.e. 20 25 30'")
.arg("-l [len] 'A length to use, defaults to 10 when omitted'")
.get_matches();
// 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.
.possible_values(&enum_vals))
// 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();
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
let matches = App::new("myapp")
// Add the version arguments
.args_from_usage("--set-ver [ver] 'set version manually'
--major 'auto inc major'
--minor 'auto inc minor'
--patch 'auto inc patch'")
.arg("--set-ver [ver] 'set version manually'")
.arg("--major 'auto inc major'")
.arg("--minor 'auto inc minor'")
.arg("--patch 'auto inc patch'")
// Create a group, make it required, and add the above arguments
.group(ArgGroup::with_name("vers")
.required(true)

View file

@ -1,6 +1,6 @@
extern crate clap;
use clap::{App, AppSettings, SubCommand};
use clap::{App, AppSettings, };
fn main() {
// You can use AppSettings to change the application level behavior of clap. .setting() function
@ -15,11 +15,11 @@ fn main() {
.setting(AppSettings::SubcommandsNegateReqs)
// 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
// will be only required if subcommand is not present.
.subcommand(SubCommand::with_name("test")
.subcommand(App::new("test")
.about("does some testing"))
// if program is invoked with subcommand, you do not
// need to specify the <input> argument anymore due to

View file

@ -13,6 +13,8 @@
// Using yaml requires calling a clap macro `load_yaml!()` so we must use the '#[macro_use]'
// directive
// Note: If you're using clap as a dependency and don't have a feature for your users called
// "yaml", you'll need to remove the #[cfg(feature = "yaml")] conditional compilation attribute
#[cfg(feature = "yaml")]
#[macro_use]
extern crate clap;

View file

@ -15,7 +15,7 @@ fn main() {
};
// 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 =>
(@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
// 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
// that subcommands are set up in a tree like heirachy.
//
@ -41,7 +41,7 @@
extern crate clap;
use clap::{App, AppSettings, Arg, SubCommand};
use clap::{App, AppSettings, Arg, };
fn main() {
let matches = App::new("git")
@ -49,28 +49,28 @@ fn main() {
.version("1.0")
.author("Me")
.subcommand(
SubCommand::with_name("clone").about("clones repos").arg(
App::new("clone").about("clones repos").arg(
Arg::with_name("repo")
.help("The repo to clone")
.required(true),
),
)
.subcommand(
SubCommand::with_name("push")
App::new("push")
.about("pushes things")
.setting(AppSettings::SubcommandRequiredElseHelp)
.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
.about("pushes remote things")
.arg(Arg::with_name("repo")
.required(true)
.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::with_name("add")
App::new("add")
.about("adds things")
.author("Someone Else") // Subcommands can list different authors
.version("v2.0 (I'm versioned differently") // or different version from their parents

View file

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

View file

@ -17,7 +17,6 @@ use yaml_rust::Yaml;
// Internal
use build::{Arg, ArgGroup, ArgSettings};
use completions::{ComplGen, Shell};
use mkeymap::MKeyMap;
use output::fmt::ColorWhen;
use output::{Help, Usage};
@ -107,20 +106,12 @@ where
#[doc(hidden)]
pub g_settings: AppFlags,
#[doc(hidden)]
pub args: MKeyMap<Arg<'a, 'b>>,
pub args: MKeyMap<'a, 'b>,
#[doc(hidden)]
pub subcommands: Vec<App<'a, 'b>>,
#[doc(hidden)]
pub groups: Vec<ArgGroup<'a>>,
#[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>>,
}
@ -181,7 +172,7 @@ impl<'a, 'b> App<'a, 'b> {
/// **Pro-tip:** When building things such as third party `cargo` subcommands, this setting
/// **should** be used!
///
/// **NOTE:** This command **should not** be used for [`SubCommand`]s.
/// **NOTE:** This command **should not** be used for [``]s.
///
/// # Examples
///
@ -191,7 +182,7 @@ impl<'a, 'b> App<'a, 'b> {
/// .bin_name("my_binary")
/// # ;
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
pub fn bin_name<S: Into<String>>(mut self, name: S) -> Self {
self.bin_name = Some(name.into());
self
@ -470,7 +461,7 @@ impl<'a, 'b> App<'a, 'b> {
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.
///
@ -483,14 +474,14 @@ impl<'a, 'b> App<'a, 'b> {
/// .setting(AppSettings::WaitOnError)
/// # ;
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
/// [`AppSettings`]: ./enum.AppSettings.html
pub fn setting(mut self, setting: AppSettings) -> Self {
self.settings.set(setting);
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.
///
@ -502,11 +493,12 @@ impl<'a, 'b> App<'a, 'b> {
/// .unset_setting(AppSettings::ColorAuto)
/// # ;
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
/// [`AppSettings`]: ./enum.AppSettings.html
/// [global]: ./struct.App.html#method.global_setting
pub fn unset_setting(mut self, setting: AppSettings) -> Self {
self.settings.unset(setting);
self.g_settings.unset(setting);
self
}
@ -684,7 +676,7 @@ impl<'a, 'b> App<'a, 'b> {
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
/// than creating multiple hidden subcommands as one only needs to check for the existence of
/// this command, and not all variants.
@ -692,14 +684,14 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test")
/// .subcommand(App::new("test")
/// .alias("do-stuff"))
/// .get_matches_from(vec!["myprog", "do-stuff"]);
/// 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 {
if let Some(ref mut als) = self.aliases {
als.push((name.into(), false));
@ -709,7 +701,7 @@ impl<'a, 'b> App<'a, 'b> {
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
/// than creating multiple hidden subcommands as one only needs to check for the existence of
/// this command, and not all variants.
@ -717,9 +709,9 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test")
/// .subcommand(App::new("test")
/// .aliases(&["do-stuff", "do-tests", "tests"]))
/// .arg(Arg::with_name("input")
/// .help("the file to add")
@ -728,7 +720,7 @@ impl<'a, 'b> App<'a, 'b> {
/// .get_matches_from(vec!["myprog", "do-tests"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
pub fn aliases(mut self, names: &[&'b str]) -> Self {
if let Some(ref mut als) = self.aliases {
for n in names {
@ -740,20 +732,20 @@ impl<'a, 'b> App<'a, 'b> {
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.
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test")
/// .subcommand(App::new("test")
/// .visible_alias("do-stuff"))
/// .get_matches_from(vec!["myprog", "do-stuff"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
/// [`App::alias`]: ./struct.App.html#method.alias
pub fn visible_alias<S: Into<&'b str>>(mut self, name: S) -> Self {
if let Some(ref mut als) = self.aliases {
@ -764,20 +756,20 @@ impl<'a, 'b> App<'a, 'b> {
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.
///
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// let m = App::new("myprog")
/// .subcommand(SubCommand::with_name("test")
/// .subcommand(App::new("test")
/// .visible_aliases(&["do-stuff", "tests"]))
/// .get_matches_from(vec!["myprog", "do-stuff"]);
/// assert_eq!(m.subcommand_name(), Some("test"));
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
/// [`App::aliases`]: ./struct.App.html#method.aliases
pub fn visible_aliases(mut self, names: &[&'b str]) -> Self {
if let Some(ref mut als) = self.aliases {
@ -813,11 +805,10 @@ impl<'a, 'b> App<'a, 'b> {
/// ```no_run
/// # use clap::{App, ArgGroup};
/// App::new("app")
/// .args_from_usage(
/// "--set-ver [ver] 'set the version manually'
/// --major 'auto increase major'
/// --minor 'auto increase minor'
/// --patch 'auto increase patch'")
/// .arg("--set-ver [ver] 'set the version manually'")
/// .arg("--major 'auto increase major'")
/// .arg("--minor 'auto increase minor'")
/// .arg("--patch 'auto increase patch'")
/// .group(ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"])
/// .required(true))
@ -836,13 +827,12 @@ impl<'a, 'b> App<'a, 'b> {
/// ```no_run
/// # use clap::{App, ArgGroup};
/// App::new("app")
/// .args_from_usage(
/// "--set-ver [ver] 'set the version manually'
/// --major 'auto increase major'
/// --minor 'auto increase minor'
/// --patch 'auto increase patch'
/// -c [FILE] 'a config file'
/// -i [IFACE] 'an interface'")
/// .arg("--set-ver [ver] 'set the version manually'")
/// .arg("--major 'auto increase major'")
/// .arg("--minor 'auto increase minor'")
/// .arg("--patch 'auto increase patch'")
/// .arg("-c [FILE] 'a config file'")
/// .arg("-i [IFACE] 'an interface'")
/// .groups(&[
/// ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"])
@ -861,7 +851,7 @@ impl<'a, 'b> App<'a, 'b> {
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,
/// etc. They also function just like [`App`]s, in that they get their own auto generated help,
/// version, and usage.
@ -869,14 +859,14 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples
///
/// ```no_run
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// App::new("myprog")
/// .subcommand(SubCommand::with_name("config")
/// .subcommand(App::new("config")
/// .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
pub fn subcommand(mut self, subcmd: App<'a, 'b>) -> Self {
self.subcommands.push(subcmd);
@ -884,20 +874,20 @@ impl<'a, 'b> App<'a, 'b> {
}
/// Adds multiple subcommands to the list of valid possibilities by iterating over an
/// [`IntoIterator`] of [`SubCommand`]s
/// [`IntoIterator`] of [``]s
///
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// # App::new("myprog")
/// .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)),
/// 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
pub fn subcommands<I>(mut self, subcmds: I) -> Self
where
@ -909,7 +899,7 @@ impl<'a, 'b> App<'a, 'b> {
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
/// emphasise frequently used subcommands, or prioritize those towards the top of the list.
/// Duplicate values **are** allowed. Subcommands with duplicate display orders will be
@ -920,15 +910,15 @@ impl<'a, 'b> App<'a, 'b> {
/// # Examples
///
/// ```rust
/// # use clap::{App, SubCommand};
/// # use clap::{App, };
/// 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
/// // without a display_order have a value of
/// // 999 and are displayed alphabetically with
/// // all other 999 subcommands
/// .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*
/// // all we have to do is give it a value lower than 999.
/// // Any other subcommands with a value of 1 will be displayed
@ -955,7 +945,7 @@ impl<'a, 'b> App<'a, 'b> {
/// beta I should be first!
/// alpha Some help and text
/// ```
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
pub fn display_order(mut self, ord: usize) -> Self {
self.disp_ord = ord;
self
@ -991,7 +981,7 @@ impl<'a, 'b> App<'a, 'b> {
let a = self
.args
.remove_by_name(arg)
.expect(&*format!("Arg '{}' not found.", arg));
.unwrap_or_else(|| Arg::with_name(arg));
self.args.push(f(a));
self
@ -1306,7 +1296,7 @@ impl<'a, 'b> App<'a, 'b> {
/// .unwrap_or_else( |e| { panic!("An error occurs: {}", e) });
/// ```
/// [`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::VersionDisplayed`]: ./enum.ErrorKind.html#variant.VersionDisplayed
/// [`Error::exit`]: ./struct.Error.html#method.exit
@ -1402,7 +1392,8 @@ impl<'a, 'b> App<'a, 'b> {
let global_arg_vec: Vec<&str> = (&self)
.args
.values()
.args
.iter()
.filter(|a| a.is_set(ArgSettings::Global))
.map(|ga| ga.name)
.collect();
@ -1431,14 +1422,14 @@ impl<'a, 'b> App<'a, 'b> {
}
// Perform expensive debug assertions
debug_assert!({
for a in self.args.values() {
for a in self.args.args.iter() {
self._arg_debug_asserts(a);
}
true
});
let mut pos_counter = 1;
for a in self.args.values_mut() {
for a in self.args.args.iter_mut() {
// Fill in the groups
if let Some(ref grps) = a.groups {
for g in grps {
@ -1465,7 +1456,7 @@ impl<'a, 'b> App<'a, 'b> {
a._build();
if a.short.is_none() && a.long.is_none() && a.index.is_none() {
a.index = Some(pos_counter);
pos_counter+=1;
pos_counter += 1;
}
}
@ -1476,9 +1467,9 @@ impl<'a, 'b> App<'a, 'b> {
// Perform some expensive assertions on the Parser itself
fn _app_debug_asserts(&mut self) -> bool {
debugln!("App::app_debug_asserts;");
for name in arg_names!(self) {
if self.args.values().filter(|x| x.name == name).count() > 1 {
debugln!("App::_app_debug_asserts;");
for name in self.args.args.iter().map(|x| x.name) {
if self.args.args.iter().filter(|x| x.name == name).count() > 1 {
panic!(format!(
"Arg names must be unique, found {} more than once",
name
@ -1525,7 +1516,12 @@ impl<'a, 'b> App<'a, 'b> {
sc.max_w = self.max_w;
}
{
for a in self.args.values().filter(|a| a.is_set(ArgSettings::Global)) {
for a in self
.args
.args
.iter()
.filter(|a| a.is_set(ArgSettings::Global))
{
sc.args.push(a.clone());
}
}
@ -1538,50 +1534,29 @@ impl<'a, 'b> App<'a, 'b> {
pub(crate) fn _create_help_and_version(&mut self) {
debugln!("App::_create_help_and_version;");
// name is "hclap_help" because flags are sorted by name
if !self
.args
.values()
.filter_map(|x| x.long)
.any(|x| x == "help")
{
if !(self.args.args.iter().any(|x| x.long == Some("help") || x.name == "help")) {
debugln!("App::_create_help_and_version: Building --help");
if self.help_short.is_none()
&& !self.args.values().filter_map(|x| x.short).any(|x| x == 'h')
{
self.help_short = Some('h');
}
let mut arg = Arg::with_name("hclap_help")
let mut help = Arg::with_name("help")
.long("help")
.help(self.help_message.unwrap_or("Prints help information"));
.help("Prints help information");
if !self.args.args.iter().any(|x| x.short == Some('h')) {
help = help.short('h');
}
// we have to set short manually because we're dealing with char's
arg.short = self.help_short;
self.args.push(arg);
} else {
self.settings.unset(AppSettings::NeedsLongHelp);
self.args.push(help);
}
if !self.is_set(AppSettings::DisableVersion) && !self
.args
.values()
.filter_map(|x| x.long)
.any(|x| x == "version")
if !(self.args.args.iter().any(|x| x.long == Some("version") || x.name == "version")
|| self.is_set(AppSettings::DisableVersion))
{
debugln!("App::_create_help_and_version: Building --version");
if self.version_short.is_none()
&& !self.args.values().filter_map(|x| x.short).any(|x| x == 'V')
{
self.version_short = Some('V');
}
// name is "vclap_version" because flags are sorted by name
let mut arg = Arg::with_name("vclap_version")
let mut version = Arg::with_name("version")
.long("version")
.help(self.version_message.unwrap_or("Prints version information"));
// we have to set short manually because we're dealing with char's
arg.short = self.version_short;
self.args.push(arg);
} else {
self.settings.unset(AppSettings::NeedsLongVersion);
.help("Prints version information");
if !self.args.args.iter().any(|x| x.short == Some('V')) {
version = version.short('V');
}
self.args.push(version);
}
if self.has_subcommands()
&& !self.is_set(AppSettings::DisableHelpSubcommand)
@ -1592,15 +1567,16 @@ impl<'a, 'b> App<'a, 'b> {
App::new("help")
.about("Prints this message or the help of the given subcommand(s)"),
);
} else {
self.settings.unset(AppSettings::NeedsSubcommandHelp);
}
}
pub(crate) fn _derive_display_order(&mut self) {
debugln!("App::_derive_display_order:{}", self.name);
if self.settings.is_set(AppSettings::DeriveDisplayOrder) {
for (i, a) in args_mut!(self)
for (i, a) in self
.args
.args
.iter_mut()
.filter(|a| a.has_switch())
.filter(|a| a.disp_ord == 999)
.enumerate()
@ -1626,11 +1602,11 @@ impl<'a, 'b> App<'a, 'b> {
// Long conflicts
if let Some(l) = a.long {
assert!(
args!(self).fold(0, |acc, arg| if arg.long == Some(l) {
acc + 1
} else {
acc
},) < 2,
self.args
.args
.iter()
.filter(|x| x.long == Some(l))
.count() < 2,
"Argument long must be unique\n\n\t--{} is already in use",
l
);
@ -1639,11 +1615,11 @@ impl<'a, 'b> App<'a, 'b> {
// Short conflicts
if let Some(s) = a.short {
assert!(
args!(self).fold(0, |acc, arg| if arg.short == Some(s) {
acc + 1
} else {
acc
},) < 2,
self.args
.args
.iter()
.filter(|x| x.short == Some(s))
.count() < 2,
"Argument short must be unique\n\n\t-{} is already in use",
s
);
@ -1750,7 +1726,8 @@ impl<'a, 'b> App<'a, 'b> {
} else {
x.to_string()
}
}).collect::<Vec<_>>()
})
.collect::<Vec<_>>()
.join("|");
format!("<{}>", &*g_string)
}
@ -1760,7 +1737,7 @@ impl<'a, 'b> App<'a, 'b> {
#[doc(hidden)]
impl<'a, 'b> App<'a, 'b> {
pub(crate) fn find(&self, name: &str) -> Option<&Arg<'a, 'b>> {
self.args.values().find(|a| a.name == name)
self.args.args.iter().find(|a| a.name == name)
}
// Should we color the output? None=determined by output location, true=yes, false=no
@ -1784,14 +1761,14 @@ impl<'a, 'b> App<'a, 'b> {
if !self.is_set(AppSettings::Propagated) {
panic!("If App::_build hasn't been called, manually search through Arg longs");
}
longs!(self).any(|al| al == &OsString::from(l))
self.args.contains_long(l)
}
pub(crate) fn contains_short(&self, s: char) -> bool {
if !self.is_set(AppSettings::Propagated) {
panic!("If App::_build hasn't been called, manually search through Arg shorts");
}
shorts!(self).any(|&arg_s| arg_s == s)
self.args.contains_short(s)
}
pub fn is_set(&self, s: AppSettings) -> bool {
@ -1899,271 +1876,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 = "Build and Arg with a long of '--help' to override the default help arg 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 = "Build and Arg with a long of '--version' to override the default version arg 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")]
impl<'a> From<&'a Yaml> for App<'a, 'a> {
fn from(mut yaml: &'a Yaml) -> Self {
use parse::SubCommand;
// We WANT this to panic on error...so expect() is good.
let mut is_sc = None;
let mut a = if let Some(name) = yaml["name"].as_str() {
@ -2198,8 +1913,6 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
yaml_str!(a, yaml, template);
yaml_str!(a, yaml, usage);
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, version_message);
yaml_str!(a, yaml, alias);
@ -2293,7 +2006,7 @@ impl<'a> From<&'a Yaml> for App<'a, 'a> {
}
if let Some(v) = yaml["subcommands"].as_vec() {
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() {

View file

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

View file

@ -163,7 +163,7 @@ impl<'a, 'b> Arg<'a, 'b> {
for (k, v) in arg_settings.iter() {
a = match k.as_str().unwrap() {
"short" => yaml_to_str!(a, v, short),
"short" => yaml_to_char!(a, v, short),
"long" => yaml_to_str!(a, v, long),
"aliases" => yaml_vec_or_str!(v, a, alias),
"help" => yaml_to_str!(a, v, help),
@ -569,7 +569,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config"))
/// .arg(Arg::with_name("dbg")
/// .long("debug"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--debug"
/// ]);
///
@ -587,7 +587,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config"))
/// .arg(Arg::with_name("dbg")
/// .long("debug"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog"
/// ]);
///
@ -638,7 +638,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile")
/// .short('i')
/// .takes_value(true))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--debug", "-i", "file"
/// ]);
///
@ -660,7 +660,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile")
/// .short('i')
/// .takes_value(true))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog"
/// ]);
///
@ -712,7 +712,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile")
/// .short('i')
/// .takes_value(true))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--debug"
/// ]);
///
@ -734,7 +734,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("infile")
/// .short('i')
/// .takes_value(true))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog"
/// ]);
///
@ -785,7 +785,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config"))
/// .arg(Arg::with_name("debug")
/// .long("debug"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--debug", "--config", "file.conf"
/// ]);
///
@ -834,7 +834,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("debug"))
/// .arg(Arg::with_name("input")
/// .index(1))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--config", "file.conf", "file.txt"
/// ]);
///
@ -1028,7 +1028,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config"))
/// .arg(Arg::with_name("input")
/// .index(1))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog"
/// ]);
///
@ -1046,7 +1046,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("config"))
/// .arg(Arg::with_name("input")
/// .index(1))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--config", "file.conf"
/// ]);
///
@ -1098,7 +1098,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .requires_if("my.cfg", "other")
/// .long("config"))
/// .arg(Arg::with_name("other"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--config", "some.cfg"
/// ]);
///
@ -1116,7 +1116,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .requires_if("my.cfg", "input")
/// .long("config"))
/// .arg(Arg::with_name("input"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--config", "my.cfg"
/// ]);
///
@ -1176,7 +1176,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("option")
/// .takes_value(true))
/// .arg(Arg::with_name("other"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--config", "special.conf"
/// ]);
///
@ -1234,7 +1234,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("other")
/// .long("other")
/// .takes_value(true))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--other", "not-special"
/// ]);
///
@ -1254,7 +1254,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("other")
/// .long("other")
/// .takes_value(true))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--other", "special"
/// ]);
///
@ -1317,7 +1317,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("option")
/// .takes_value(true)
/// .long("option"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--option", "other"
/// ]);
///
@ -1343,7 +1343,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("option")
/// .takes_value(true)
/// .long("option"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--option", "spec"
/// ]);
///
@ -1398,7 +1398,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .index(1))
/// .arg(Arg::with_name("output")
/// .index(2))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog"
/// ]);
///
@ -1419,7 +1419,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .index(1))
/// .arg(Arg::with_name("output")
/// .index(2))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--config", "file.conf", "in.txt"
/// ]);
///
@ -1587,7 +1587,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .long("mode")
/// .takes_value(true)
/// .possible_values(&["fast", "slow", "medium"]))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--mode", "wrong"
/// ]);
/// assert!(res.is_err());
@ -1652,7 +1652,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .possible_value("fast")
/// .possible_value("slow")
/// .possible_value("medium"))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "--mode", "wrong"
/// ]);
/// assert!(res.is_err());
@ -1779,7 +1779,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true)
/// .number_of_values(2)
/// .short('F'))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "-F", "file1"
/// ]);
///
@ -1819,7 +1819,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("file")
/// .index(1)
/// .validator(has_at))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "some@file"
/// ]);
/// assert!(res.is_ok());
@ -1858,7 +1858,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .arg(Arg::with_name("file")
/// .index(1)
/// .validator_os(has_ampersand))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "Fish & chips"
/// ]);
/// assert!(res.is_ok());
@ -1907,7 +1907,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true)
/// .max_values(3)
/// .short('F'))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "-F", "file1", "file2"
/// ]);
///
@ -1926,7 +1926,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true)
/// .max_values(2)
/// .short('F'))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "-F", "file1", "file2", "file3"
/// ]);
///
@ -1971,7 +1971,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true)
/// .min_values(2)
/// .short('F'))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "prog", "-F", "file1", "file2", "file3"
/// ]);
///
@ -1990,7 +1990,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// .takes_value(true)
/// .min_values(2)
/// .short('F'))
/// .get_matches_from_safe(vec![
/// .try_get_matches_from(vec![
/// "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
/// 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.
///
/// ```rust
/// # use clap::{App, Arg, SubCommand, ArgSettings};
/// # use clap::{App, Arg, ArgSettings};
/// let m = App::new("prog")
/// .arg(Arg::with_name("verb")
/// .long("verbose")
/// .short('v')
/// .setting(ArgSettings::Global))
/// .subcommand(SubCommand::with_name("test"))
/// .subcommand(SubCommand::with_name("do-stuff"))
/// .subcommand(App::new("test"))
/// .subcommand(App::new("do-stuff"))
/// .get_matches_from(vec![
/// "prog", "do-stuff", "--verbose"
/// ]);
@ -3012,7 +3012,7 @@ impl<'a, 'b> Arg<'a, 'b> {
/// let sub_m = m.subcommand_matches("do-stuff").unwrap();
/// assert!(sub_m.is_present("verb"));
/// ```
/// [`SubCommand`]: ./struct.App.html#method.subcommand
/// [``]: ./struct.App.html#method.subcommand
/// [required]: ./enum.ArgSettings.html#variant.Required
/// [`ArgMatches`]: ./struct.ArgMatches.html
/// [`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 {
if hide {
self.set(ArgSettings::HiddenShortHelp)
self.setting(ArgSettings::HiddenShortHelp)
} 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 {
if hide {
self.set(ArgSettings::HiddenLongHelp)
self.setting(ArgSettings::HiddenLongHelp)
} 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> {
fn from(a: &'z Arg<'a, 'b>) -> Self { a.clone() }
}
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> {

View file

@ -39,15 +39,14 @@ use yaml_rust;
/// ```rust
/// # use clap::{App, ArgGroup, ErrorKind};
/// let result = App::new("app")
/// .args_from_usage(
/// "--set-ver [ver] 'set the version manually'
/// --major 'auto increase major'
/// --minor 'auto increase minor'
/// --patch 'auto increase patch'")
/// .arg("--set-ver [ver] 'set the version manually'")
/// .arg("--major 'auto increase major'")
/// .arg("--minor 'auto increase minor'")
/// .arg("--patch 'auto increase patch'")
/// .group(ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"])
/// .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
/// assert!(result.is_err());
/// let err = result.unwrap_err();
@ -58,15 +57,14 @@ use yaml_rust;
/// ```rust
/// # use clap::{App, ArgGroup};
/// let result = App::new("app")
/// .args_from_usage(
/// "--set-ver [ver] 'set the version manually'
/// --major 'auto increase major'
/// --minor 'auto increase minor'
/// --patch 'auto increase patch'")
/// .arg("--set-ver [ver] 'set the version manually'")
/// .arg("--major 'auto increase major'")
/// .arg("--minor 'auto increase minor'")
/// .arg("--patch 'auto increase patch'")
/// .group(ArgGroup::with_name("vers")
/// .args(&["set-ver", "major", "minor","patch"])
/// .required(true))
/// .get_matches_from_safe(vec!["app", "--major"]);
/// .try_get_matches_from(vec!["app", "--major"]);
/// assert!(result.is_ok());
/// let matches = result.unwrap();
/// // 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'))
/// .group(ArgGroup::with_name("req_flags")
/// .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
/// assert!(result.is_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
/// 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.
///
/// **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")
/// .args(&["flag", "color"])
/// .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
/// assert!(result.is_err());
/// let err = result.unwrap_err();
/// assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
/// ```
/// [`App`]: ./struct.App.html
/// [`SubCommand`]: ./struct.SubCommand.html
/// [``]: ./struct..html
/// [`ArgGroup::multiple`]: ./struct.ArgGroup.html#method.multiple
pub fn required(mut self, r: bool) -> Self {
self.required = r;
@ -297,7 +295,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"])
/// .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
/// // error
/// assert!(result.is_err());
@ -338,7 +336,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"])
/// .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,
/// // yet we only used "-d" it's an error
/// assert!(result.is_err());
@ -374,7 +372,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"])
/// .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
/// assert!(result.is_err());
/// let err = result.unwrap_err();
@ -412,7 +410,7 @@ impl<'a> ArgGroup<'a> {
/// .group(ArgGroup::with_name("req_flags")
/// .args(&["flag", "color"])
/// .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"
/// // it's an error
/// assert!(result.is_err());

View file

@ -76,6 +76,17 @@ macro_rules! yaml_opt_str {
}};
}
#[cfg(feature = "yaml")]
macro_rules! yaml_char {
($v:expr) => {{
$v.as_str()
.unwrap_or_else(|| panic!("failed to convert YAML {:?} value to a string", $v))
.chars()
.next()
.unwrap_or_else(|| panic!("Expected char"))
}};
}
#[cfg(feature = "yaml")]
macro_rules! yaml_str {
($v:expr) => {{
@ -84,6 +95,13 @@ macro_rules! yaml_str {
}};
}
#[cfg(feature = "yaml")]
macro_rules! yaml_to_char {
($a:ident, $v:ident, $c:ident) => {{
$a.$c(yaml_char!($v))
}};
}
#[cfg(feature = "yaml")]
macro_rules! yaml_to_str {
($a:ident, $v:ident, $c:ident) => {{

View file

@ -1,193 +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
// error-handling?
longs = longs!(p).fold(String::new(), |acc, l| {
format!("{} --{}", acc, l.to_str().unwrap())
}),
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
//! // to generate arguments dynamically.
//! extern crate clap;
//! use clap::{Arg, App, SubCommand};
//! use clap::{Arg, App, };
//!
//! fn main() {
//! let matches = App::new("My Super Program")
@ -67,7 +67,7 @@
//! .short('v')
//! .multiple(true)
//! .help("Sets the level of verbosity"))
//! .subcommand(SubCommand::with_name("test")
//! .subcommand(App::new("test")
//! .about("controls testing features")
//! .version("1.3")
//! .author("Someone E. <someone_else@other.com>")
@ -117,22 +117,21 @@
//! // This example demonstrates clap's "usage strings" method of creating arguments
//! // which is less verbose
//! extern crate clap;
//! use clap::{Arg, App, SubCommand};
//! use clap::{Arg, App, };
//!
//! fn main() {
//! let matches = App::new("myapp")
//! .version("1.0")
//! .author("Kevin K. <kbknapp@gmail.com>")
//! .about("Does awesome things")
//! .args_from_usage(
//! "-c, --config=[FILE] 'Sets a custom config file'
//! <INPUT> 'Sets the input file to use'
//! -v... 'Sets the level of verbosity'")
//! .subcommand(SubCommand::with_name("test")
//! .arg("-c, --config=[FILE] 'Sets a custom config file'")
//! .arg("<INPUT> 'Sets the input file to use'")
//! .arg("-v... 'Sets the level of verbosity'")
//! .subcommand(App::new("test")
//! .about("controls testing features")
//! .version("1.3")
//! .author("Someone E. <someone_else@other.com>")
//! .arg_from_usage("-d, --debug 'Print debug information'"))
//! .arg("-d, --debug 'Print debug information'"))
//! .get_matches();
//!
//! // Same as previous example...
@ -517,7 +516,7 @@
//! [license]: https://raw.githubusercontent.com/kbknapp/clap-rs/master/LICENSE-MIT
#![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(
missing_docs,
missing_debug_implementations,
@ -566,10 +565,9 @@ extern crate vec_map;
extern crate yaml_rust;
pub use build::{App, AppSettings, Arg, ArgGroup, ArgSettings, Propagation};
pub use completions::Shell;
pub use output::fmt::Format;
pub use parse::errors::{Error, ErrorKind, Result};
pub use parse::{ArgMatches, OsValues, SubCommand, Values};
pub use parse::{ArgMatches, OsValues, Values};
#[cfg(feature = "yaml")]
pub use yaml_rust::YamlLoader;
@ -583,10 +581,7 @@ use std::result::Result as StdResult;
mod macros;
mod build;
mod mkeymap;
mod completions;
mod output;
mod parse;
mod util;

View file

@ -46,7 +46,7 @@ macro_rules! load_yaml {
/// # use clap::App;
/// # fn main() {
/// 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();
///
/// 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;
/// # fn main() {
/// 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();
///
/// let len = value_t_or_exit!(matches.value_of("length"), u32);
@ -136,7 +136,7 @@ macro_rules! value_t_or_exit {
/// # use clap::App;
/// # fn main() {
/// 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();
///
/// 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;
/// # fn main() {
/// 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();
///
/// 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.
///
/// `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)*) => {
clap_app!{ @app
($builder.subcommand(
clap_app!{ @app ($crate::SubCommand::with_name(stringify!($name))) $($tail)* }
clap_app!{ @app ($crate::App::new(stringify!($name))) $($tail)* }
))
$($tt)*
}
@ -789,7 +789,7 @@ macro_rules! clap_app {
// Build a subcommand outside of an app.
(@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
(($name:expr) => $($tail:tt)*) => {{
@ -883,34 +883,13 @@ macro_rules! write_nspaces {
}};
}
macro_rules! args {
($app:expr, $how:ident) => {
$app.args.$how()
};
($app:expr) => {
args!($app, values)
};
}
macro_rules! args_mut {
($app:expr) => {
args!($app, values_mut)
};
}
macro_rules! flags {
($app:expr, $how:ident) => {{
use mkeymap::KeyType::*;
$app.args
.args
.$how()
.filter(|(_, a)| !a.settings.is_set(::build::ArgSettings::TakesValue))
.filter(|(k, _)| match k {
Long(_) => true,
Short(_) => true,
Position(_) => false,
})
.filter(|(_, a)| !a.help_heading.is_some())
.map(|(_, v)| v)
.filter(|a| !a.settings.is_set(::build::ArgSettings::TakesValue) && a.index.is_none())
.filter(|a| !a.help_heading.is_some())
}};
($app:expr) => {
flags!($app, iter)
@ -926,17 +905,11 @@ macro_rules! flags_mut {
macro_rules! opts {
($app:expr, $how:ident) => {{
use mkeymap::KeyType::*;
$app.args
.args
.$how()
.filter(|(_, a)| a.settings.is_set(::build::ArgSettings::TakesValue))
.filter(|(k, _)| match k {
Long(_) => true,
Short(_) => true,
Position(_) => false,
})
.filter(|(_, a)| !a.help_heading.is_some())
.map(|(_, v)| v)
.filter(|a| a.settings.is_set(::build::ArgSettings::TakesValue) && a.index.is_none())
.filter(|a| !a.help_heading.is_some())
}};
($app:expr) => {
opts!($app, iter)
@ -953,7 +926,8 @@ macro_rules! opts_mut {
macro_rules! positionals {
($app:expr) => {
$app.args
.values()
.args
.iter()
.filter(|a| !(a.short.is_some() || a.long.is_some()))
};
}
@ -967,16 +941,6 @@ macro_rules! positionals_mut {
};
}
#[allow(unused_macros)]
macro_rules! custom_headings {
($app:expr, $how:ident) => {
$app.args.$how().filter(|a| (a.help_heading.is_some()))
};
($app:expr) => {
custom_headings!($app, values)
};
}
#[allow(unused_macros)]
macro_rules! custom_headings_mut {
($app:expr) => {
@ -1018,17 +982,6 @@ macro_rules! groups_for_arg {
}};
}
// Finds an arg by name
// ! look up usages and find ways to improve performance
macro_rules! find {
($app:expr, $name:expr, $what:ident) => {
$what!($app).find(|a| &a.name == $name)
};
($app:expr, $name:expr) => {
$app.args.values().find(|a| &a.name == $name)
};
}
macro_rules! find_subcmd_cloned {
($_self:expr, $sc:expr) => {{
subcommands_cloned!($_self)
@ -1043,16 +996,10 @@ macro_rules! find_subcmd {
}};
}
// macro_rules! shorts {
// ($app:expr) => {{
// _shorts_longs!($app, short)
// }};
// }
macro_rules! longs {
($app:expr) => {{
use mkeymap::KeyType;
$app.args.keys().filter_map(|a| {
$app.args.keys.iter().map(|x| &x.key).filter_map(|a| {
if let KeyType::Long(v) = a {
Some(v)
} else {
@ -1062,22 +1009,9 @@ macro_rules! longs {
}};
}
macro_rules! shorts {
($app:expr) => {{
use mkeymap::KeyType;
$app.args.keys().filter_map(|a| {
if let KeyType::Short(v) = a {
Some(v)
} else {
None
}
})
}};
}
macro_rules! _names {
(@args $app:expr) => {{
$app.args.values().map(|a| &*a.name)
$app.args.args.iter().map(|a| &*a.name)
}};
(@sc $app:expr) => {{
$app.subcommands.iter().map(|s| &*s.name).chain(

View file

@ -1,14 +1,16 @@
use build::Arg;
use std::collections::hash_map;
use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::hash::Hash;
use std::slice;
#[derive(PartialEq, Debug, Clone)]
pub struct Key {
pub key: KeyType,
pub index: usize
}
#[derive(Default, PartialEq, Debug, Clone)]
pub struct MKeyMap<T> {
keys: HashMap<KeyType, usize>,
value_index: Vec<T>,
pub struct MKeyMap<'a, 'b> {
pub keys: Vec<Key>,
pub args: Vec<Arg<'a, 'b>>,
built: bool, // mutation isn't possible after being built
}
@ -40,96 +42,190 @@ impl KeyType {
}
}
impl<T> MKeyMap<T>
where
T: Sized + Hash + PartialEq + Default + Eq,
{
impl PartialEq<&str> for KeyType {
fn eq(&self, rhs: &&str) -> bool {
match self {
KeyType::Long(ref l) => l == OsStr::new(rhs),
_ => false
}
}
}
impl PartialEq<char> for KeyType {
fn eq(&self, rhs: &char) -> bool {
match self {
KeyType::Short(c) => c == rhs,
_ => false
}
}
}
impl<'a, 'b> MKeyMap<'a, 'b> {
pub fn new() -> Self { MKeyMap::default() }
//TODO ::from(x), ::with_capacity(n) etc
//? set theory ops?
pub fn insert(&mut self, key: KeyType, value: T) -> usize {
pub fn contains_long(&self, l: &str) -> bool {
self.keys.iter().any(|x| x.key == l)
}
pub fn contains_short(&self, c: char) -> bool {
self.keys.iter().any(|x| x.key == c)
}
pub fn insert(&mut self, key: KeyType, value: Arg<'a, 'b>) -> usize {
let index = self.push(value);
self.keys.insert(key, index);
self.keys.push(Key { key, index });
index
}
pub fn push(&mut self, value: T) -> usize {
pub fn push(&mut self, value: Arg<'a, 'b>) -> usize {
if self.built {
panic!("Cannot add Args to the map after the map is built");
}
let index = self.value_index.len();
self.value_index.push(value);
let index = self.args.len();
self.args.push(value);
index
}
//TODO ::push_many([x, y])
pub fn insert_key(&mut self, key: KeyType, index: usize) {
if index >= self.value_index.len() {
if index >= self.args.len() {
panic!("Index out of bounds");
}
self.keys.insert(key, index);
self.keys.push(Key {key, index});
}
//TODO ::insert_keyset([Long, Key2])
// ! Arg mutation functionality
pub fn get(&self, key: KeyType) -> Option<&T> {
self.keys
.get(&key)
.and_then(|&idx| self.value_index.get(idx))
pub fn get(&self, key: KeyType) -> Option<&Arg<'a, 'b>> {
for k in &self.keys {
if k.key == key {
return Some(&self.args[k.index]);
}
}
None
}
//TODO ::get_first([KeyA, KeyB])
pub fn get_mut(&mut self, key: KeyType) -> Option<&mut T> {
if let Some(&idx) = self.keys.get(&key) {
self.value_index.get_mut(idx)
} else {
None
pub fn get_mut(&mut self, key: KeyType) -> Option<&mut Arg<'a, 'b>> {
for k in &self.keys {
if k.key == key {
return self.args.get_mut(k.index);
}
}
None
}
pub fn is_empty(&self) -> bool { self.keys.is_empty() && self.value_index.is_empty() }
pub fn is_empty(&self) -> bool { self.keys.is_empty() && self.args.is_empty() }
pub fn remove_key(&mut self, key: KeyType) { self.keys.remove(&key); }
pub fn remove_key(&mut self, key: KeyType) {
let mut idx = None;
for (i, k) in self.keys.iter().enumerate() {
if k.key == key {
idx = Some(i);
break;
}
}
if let Some(idx) = idx {
self.keys.swap_remove(idx);
}
}
//TODO ::remove_keys([KeyA, KeyB])
pub fn keys(&self) -> Keys<usize> {
Keys {
iter: self.keys.keys(),
pub fn insert_key_by_name(&mut self, key: KeyType, name: &str) {
let index = self.find_by_name(name);
self.keys.push(Key {key, index});
}
pub fn _build(&mut self) {
self.built = true;
for (i, arg) in self.args.iter_mut().enumerate() {
for k in _get_keys(arg) {
self.keys.push(Key {key: k, index: i});
}
}
}
pub fn longs(&self) -> impl Iterator<Item = &OsString> {
self.keys.keys().filter_map(|x| match x {
KeyType::Long(ref l) => Some(l),
_ => None,
})
}
pub fn make_entries_by_index(&mut self, index: usize) {
let short;
let positional;
let mut longs;
pub fn shorts(&self) -> impl Iterator<Item = &KeyType> {
self.keys.keys().filter(|x| x.is_short())
}
{
let arg = &self.args[index];
short = arg.short.map(|c| KeyType::Short(c));
positional = arg.index.map(|n| KeyType::Position(n));
pub fn values(&self) -> Values<T> {
Values {
iter: self.value_index.iter(),
longs = arg
.aliases
.clone()
.map(|v| {
v.iter()
.map(|(n, _)| KeyType::Long(OsString::from(n)))
.collect()
}).unwrap_or(Vec::new());
longs.extend(arg.long.map(|l| KeyType::Long(OsString::from(l))));
}
short.map(|s| self.insert_key(s, index));
positional.map(|p| self.insert_key(p, index));
longs.into_iter().map(|l| self.insert_key(l, index)).count();
}
pub fn values_mut(&mut self) -> ValuesMut<T> {
ValuesMut {
iter: self.value_index.iter_mut(),
pub fn find_by_name(&mut self, name: &str) -> usize {
self.args
.iter()
.position(|x| x.name == name)
.expect("No such name found")
}
pub fn remove(&mut self, key: KeyType) -> Option<Arg<'a, 'b>> {
if self.built {
panic!("Cannot remove args after being built");
}
let mut idx = None;
for k in self.keys.iter() {
if k.key == key {
idx = Some(k.index);
break;
}
}
if let Some(idx) = idx {
let arg = self.args.swap_remove(idx);
for key in _get_keys(&arg) {
let _ = self.remove_key(key);
}
return Some(arg);
}
None
}
pub fn iter(&self) -> Iter<T> {
Iter {
map: self,
keys: self.keys(),
//TODO ::remove_many([KeyA, KeyB])
//? probably shouldn't add a possibility for removal?
//? or remove by replacement by some dummy object, so the order is preserved
pub fn remove_by_name(&mut self, _name: &str) -> Option<Arg<'a, 'b>> {
if self.built {
panic!("Cannot remove args after being built");
}
let mut index = None;
for (i, arg) in self.args.iter().enumerate() {
if arg.name == _name {
index = Some(i);
break;
}
}
if let Some(i) = index {
Some(self.args.swap_remove(i))
} else {
None
}
}
}
@ -158,151 +254,6 @@ fn _get_keys(arg: &Arg) -> Vec<KeyType> {
keys
}
impl<'a, 'b> MKeyMap<Arg<'a, 'b>> {
pub fn insert_key_by_name(&mut self, key: KeyType, name: &str) {
let index = self.find_by_name(name);
self.keys.insert(key, index);
}
pub fn _build(&mut self) {
self.built = true;
for (i, arg) in self.value_index.iter_mut().enumerate() {
for k in _get_keys(arg) {
self.keys.insert(k, i);
}
}
}
pub fn make_entries_by_index(&mut self, index: usize) {
let short;
let positional;
let mut longs;
{
let arg = &self.value_index[index];
short = arg.short.map(|c| KeyType::Short(c));
positional = arg.index.map(|n| KeyType::Position(n));
longs = arg
.aliases
.clone()
.map(|v| {
v.iter()
.map(|(n, _)| KeyType::Long(OsString::from(n)))
.collect()
}).unwrap_or(Vec::new());
longs.extend(arg.long.map(|l| KeyType::Long(OsString::from(l))));
}
short.map(|s| self.insert_key(s, index));
positional.map(|p| self.insert_key(p, index));
longs.into_iter().map(|l| self.insert_key(l, index)).count();
}
pub fn find_by_name(&mut self, name: &str) -> usize {
self.value_index
.iter()
.position(|x| x.name == name)
.expect("No such name found")
}
pub fn remove(&mut self, key: KeyType) -> Option<Arg<'a, 'b>> {
if self.built {
panic!("Cannot remove args after being built");
}
let index = if let Some(index) = self.keys.get(&key) {
index.clone()
} else {
return None;
};
let arg = self.value_index.swap_remove(index);
for key in _get_keys(&arg) {
let _ = self.keys.remove(&key);
}
Some(arg)
}
//TODO ::remove_many([KeyA, KeyB])
//? probably shouldn't add a possibility for removal?
//? or remove by replacement by some dummy object, so the order is preserved
pub fn remove_by_name(&mut self, _name: &str) -> Option<Arg<'a, 'b>> {
if self.built {
panic!("Cannot remove args after being built");
}
let mut index = None;
for (i, arg) in self.value_index.iter().enumerate() {
if arg.name == _name {
index = Some(i);
break;
}
}
if let Some(i) = index {
Some(self.value_index.swap_remove(i))
} else {
None
}
}
}
#[derive(Debug)]
pub struct Keys<'a, V: 'a> {
iter: hash_map::Keys<'a, KeyType, V>,
}
impl<'a, V> Iterator for Keys<'a, V> {
type Item = &'a KeyType;
fn next(&mut self) -> Option<Self::Item> { self.iter.next() }
}
#[derive(Debug)]
pub struct Values<'a, V: 'a> {
iter: slice::Iter<'a, V>,
}
impl<'a, V> Iterator for Values<'a, V> {
type Item = &'a V;
fn next(&mut self) -> Option<Self::Item> { self.iter.next() }
}
#[derive(Debug)]
pub struct ValuesMut<'a, V: 'a> {
iter: slice::IterMut<'a, V>,
}
impl<'a, V> Iterator for ValuesMut<'a, V> {
type Item = &'a mut V;
fn next(&mut self) -> Option<Self::Item> { self.iter.next() }
}
#[derive(Debug)]
pub struct Iter<'c, T>
where
T: 'c,
{
map: &'c MKeyMap<T>,
keys: Keys<'c, usize>,
}
impl<'c, T> Iterator for Iter<'c, T>
where
T: 'c + Sized + Hash + PartialEq + Default + Eq,
{
type Item = (&'c KeyType, &'c T);
fn next(&mut self) -> Option<Self::Item> {
if let Some(key) = self.keys.next() {
Some((key, self.map.get(key.clone()).unwrap()))
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use self::KeyType::*;
@ -310,7 +261,7 @@ mod tests {
#[test]
fn get_some_value() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
@ -322,7 +273,7 @@ mod tests {
#[test]
fn get_none_value() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
map.get(Long(OsString::from("Two")));
@ -340,7 +291,7 @@ mod tests {
#[test]
fn insert_duplicate_key() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
@ -353,15 +304,15 @@ mod tests {
#[test]
#[should_panic]
fn insert_duplicate_value() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
let orig_len = map.value_index.len();
let orig_len = map.args.len();
map.insert(Long(OsString::from("Two")), Arg::with_name("Value1"));
assert_eq!(map.value_index.len(), orig_len);
assert_eq!(map.args.len(), orig_len);
assert_eq!(
map.get(Long(OsString::from("One"))),
map.get(Long(OsString::from("Two")))
@ -379,7 +330,7 @@ mod tests {
#[test]
fn insert_multiple_keys() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
map.insert_key(Long(OsString::from("Two")), index);
@ -388,7 +339,7 @@ mod tests {
map.get(Long(OsString::from("One"))),
map.get(Long(OsString::from("Two")))
);
assert_eq!(map.value_index.len(), 1);
assert_eq!(map.args.len(), 1);
}
// #[test]
@ -407,7 +358,7 @@ mod tests {
#[test]
fn get_mutable() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
@ -419,13 +370,13 @@ mod tests {
#[test]
fn remove_key() {
let mut map: MKeyMap<Arg> = MKeyMap::new();
let mut map: MKeyMap = MKeyMap::new();
let index = map.insert(Long(OsString::from("One")), Arg::with_name("Value1"));
map.insert_key(Long(OsString::from("Two")), index);
map.remove_key(Long(OsString::from("One")));
assert_eq!(map.keys.len(), 1);
assert_eq!(map.value_index.len(), 1);
assert_eq!(map.args.len(), 1);
}
}

View file

@ -686,7 +686,7 @@ impl<'w> Help<'w> {
let opts = parser.has_opts();
let subcmds = parser.has_visible_subcommands();
let custom_headings = custom_headings!(parser.app).fold(0, |acc, arg| {
let custom_headings = parser.app.args.args.iter().fold(0, |acc, arg| {
if arg.help_heading.is_some() {
acc + 1
} else {
@ -708,7 +708,7 @@ impl<'w> Help<'w> {
let unified_help = parser.is_set(AppSettings::UnifiedHelpMessage);
if unified_help && (flags || opts) {
let opts_flags = args!(parser.app).filter(|a| a.has_switch());
let opts_flags = parser.app.args.args.iter().filter(|a| a.has_switch());
if !first {
self.writer.write_all(b"\n\n")?;
}
@ -745,7 +745,7 @@ impl<'w> Help<'w> {
}
color!(self, format!("{}:\n", heading), warning)?;
self.write_args(
custom_headings!(parser.app).filter(|a| a.help_heading.unwrap() == heading),
parser.app.args.args.iter().filter(|a| a.help_heading.is_some() && a.help_heading.unwrap() == heading),
)?;
first = false
}
@ -1070,7 +1070,7 @@ impl<'w> Help<'w> {
self.write_all_args(parser)?;
}
b"unified" => {
let opts_flags = parser.app.args.values().filter(|a| a.has_switch());
let opts_flags = parser.app.args.args.iter().filter(|a| a.has_switch());
self.write_args(opts_flags)?;
}
b"flags" => {

View file

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

View file

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

View file

@ -15,10 +15,10 @@ use ArgMatches;
/// # Examples
///
/// ```rust
/// # use clap::{App, Arg, SubCommand};
/// # use clap::{App, Arg, };
/// App::new("myprog")
/// .subcommand(
/// SubCommand::with_name("config")
/// App::new("config")
/// .about("Used for configuration")
/// .arg(Arg::with_name("config_file")
/// .help("The configuration file to use")
@ -35,24 +35,3 @@ pub struct SubCommand<'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::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::validator::Validator;

View file

@ -67,7 +67,8 @@ where
let mut reqs = ChildGraph::with_capacity(5);
for a in app
.args
.values()
.args
.iter()
.filter(|a| a.settings.is_set(ArgSettings::Required))
.map(|a| a.name)
{
@ -102,7 +103,9 @@ where
let highest_idx = *self
.app
.args
.keys()
.keys
.iter()
.map(|x| &x.key)
.filter_map(|x| {
if let KeyType::Position(n) = x {
Some(n)
@ -117,7 +120,9 @@ where
let num_p = self
.app
.args
.keys()
.keys
.iter()
.map(|x| &x.key)
.filter(|x| {
if let KeyType::Position(_) = x {
true
@ -290,7 +295,7 @@ where
//I wonder whether this part is even needed if we insert all Args using make_entries
let mut key: Vec<(KeyType, usize)> = Vec::new();
let mut counter = 0;
for (i, a) in self.app.args.values_mut().enumerate() {
for (i, a) in self.app.args.args.iter_mut().enumerate() {
if a.index == None && a.short == None && a.long == None {
counter += 1;
a.index = Some(counter);
@ -324,7 +329,9 @@ where
&& (a.index.unwrap_or(0) as usize != self
.app
.args
.keys()
.keys
.iter()
.map(|x| &x.key)
.filter(|x| {
if let KeyType::Position(_) = x {
true
@ -420,7 +427,7 @@ where
);
if is_match {
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)?;
}
subcmd_name = Some(sc_name.to_owned());
@ -504,7 +511,9 @@ where
let positional_count = self
.app
.args
.keys()
.keys
.iter()
.map(|x| &x.key)
.filter(|x| {
if let KeyType::Position(_) = x {
true
@ -562,7 +571,9 @@ where
pos_counter = self
.app
.args
.keys()
.keys
.iter()
.map(|x| &x.key)
.filter(|x| {
if let KeyType::Position(_) = x {
true
@ -584,7 +595,9 @@ where
&& (self.is_set(AS::TrailingVarArg) && pos_counter == self
.app
.args
.keys()
.keys
.iter()
.map(|x| &x.key)
.filter(|x| {
if let KeyType::Position(_) = x {
true
@ -637,7 +650,7 @@ where
sc_m.add_val_to("", &a);
}
matcher.subcommand(SubCommand {
matcher.subcommand( SubCommand{
name: sc_name,
matches: sc_m.into(),
});
@ -909,7 +922,7 @@ where
let name = sc.name.clone();
let mut p = Parser::new(sc);
p.get_matches_with(&mut sc_matcher, it)?;
matcher.subcommand(SubCommand {
matcher.subcommand( SubCommand{
name: name,
matches: sc_matcher.into(),
});
@ -928,11 +941,11 @@ where
// 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
if arg == "help" && self.app.settings.is_set(AS::NeedsLongHelp) {
if arg == "help" && !self.app.settings.is_set(AS::NoAutoHelp) {
sdebugln!("Help");
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");
return Err(self.version_err(true));
}
@ -949,16 +962,20 @@ where
);
// 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
if let Some(h) = self.app.help_short {
if arg == h && self.app.settings.is_set(AS::NeedsLongHelp) {
sdebugln!("Help");
return Err(self.help_err(false));
if let Some(help) = self.app.find("help") {
if let Some(h) = help.short {
if arg == h && !self.app.settings.is_set(AS::NoAutoHelp) {
sdebugln!("Help");
return Err(self.help_err(false));
}
}
}
if let Some(v) = self.app.version_short {
if arg == v && self.app.settings.is_set(AS::NeedsLongVersion) {
sdebugln!("Version");
return Err(self.version_err(false));
if let Some(version) = self.app.find("version") {
if let Some(v) = version.short {
if arg == v && !self.app.settings.is_set(AS::NoAutoVersion) {
sdebugln!("Version");
return Err(self.version_err(false));
}
}
}
sdebugln!("Neither");
@ -978,7 +995,7 @@ where
};
self.app.long_about.is_some()
|| args!(self.app).any(|f| should_long(&f))
|| self.app.args.args.iter().any(|f| should_long(&f))
|| subcommands!(self.app).any(|s| s.long_about.is_some())
}
@ -1426,7 +1443,7 @@ where
}
pub(crate) fn add_env(&mut self, matcher: &mut ArgMatcher<'a>) -> ClapResult<()> {
for a in self.app.args.values() {
for a in self.app.args.args.iter() {
if let Some(ref val) = a.env {
if matcher
.get(a.name)
@ -1460,8 +1477,13 @@ where
let longs = self
.app
.args
.longs()
.map(|x| x.to_string_lossy().into_owned())
.keys
.iter()
.map(|x| &x.key)
.filter_map(|x| match x {
KeyType::Long(l) => Some(l.to_string_lossy().into_owned()),
_ => None
})
.collect::<Vec<_>>();
debugln!("Parser::did_you_mean_error: longs={:?}", longs);
@ -1559,7 +1581,7 @@ where
pub(crate) fn has_flags(&self) -> bool { self.app.has_flags() }
pub(crate) fn has_positionals(&self) -> bool {
self.app.args.keys().filter(|x| x.is_position()).count() > 0
self.app.args.keys.iter().any(|x| x.key.is_position())
}
pub(crate) fn has_subcommands(&self) -> bool { self.app.has_subcommands() }

25
src/parse/relation.rs Normal file
View file

@ -0,0 +1,25 @@
struct RelationArg<'a, T> {
args: Vec<T>,
value: Option<&'a [u8]>,
modifier: RelationModifier
}
enum RelationModifier {
All,
Any,
None
}
enum RelationKind<'a, T> {
Present(RelationArg<'a, T>),
NotPresent(RelationArg<'a, T>),
None
}
struct Relations<'a, T>(Vec<RelationKind<'a, T>>);
impl<'a, T> Relation<'a, T> where T: Eq {
fn is_sat(&self, &[T]) -> bool {
for r in self.
}
}

View file

@ -517,7 +517,8 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
.p
.app
.args
.values()
.args
.iter()
.filter(|a| a.r_ifs.is_some())
.map(|a| (a, a.r_ifs.as_ref().unwrap()))
{
@ -562,7 +563,8 @@ impl<'a, 'b, 'c, 'z> Validator<'a, 'b, 'c, 'z> {
.p
.app
.args
.values()
.args
.iter()
.filter(|a| a.r_unless.is_some())
.filter(|a| !matcher.contains(a.name))
{

3120
tests.test

File diff suppressed because it is too large Load diff

View file

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

View file

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

View file

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

View file

@ -1,903 +0,0 @@
// TODO Completions tests now fail due to non-deterministic order of arguments
/*
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")
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other"))
.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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -35,7 +35,7 @@ fn flag_conflict_2() {
let result = App::new("flag_conflict")
.arg(Arg::from("-f, --flag 'some flag'").conflicts_with("other"))
.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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -53,7 +53,7 @@ fn group_conflict() {
)
.arg(Arg::from("--some 'some 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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -71,7 +71,7 @@ fn group_conflict_2() {
)
.arg(Arg::from("--some 'some 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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -102,7 +102,7 @@ fn conflict_with_unused_default_value() {
let result = App::new("conflict")
.arg(Arg::from("-o, --opt=[opt] 'some opt'").default_value("default"))
.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());
let m = result.unwrap();
assert_eq!(m.value_of("opt"), Some("default"));

View file

@ -9,7 +9,7 @@ use clap::{App, Arg, ErrorKind};
fn opts() {
let r = App::new("df")
.arg(Arg::from("-o [opt] 'some opt'").default_value("default"))
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("o"));
@ -20,7 +20,7 @@ fn opts() {
fn opt_user_override() {
let r = App::new("df")
.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());
let m = r.unwrap();
assert!(m.is_present("opt"));
@ -31,7 +31,7 @@ fn opt_user_override() {
fn positionals() {
let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").default_value("default"))
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -42,7 +42,7 @@ fn positionals() {
fn positional_user_override() {
let r = App::new("df")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -58,7 +58,7 @@ fn osstr_opts() {
let r = App::new("df")
.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());
let m = r.unwrap();
assert!(m.is_present("o"));
@ -72,7 +72,7 @@ fn osstr_opt_user_override() {
let r = App::new("df")
.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());
let m = r.unwrap();
assert!(m.is_present("opt"));
@ -86,7 +86,7 @@ fn osstr_positionals() {
let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").default_value_os(expected))
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -100,7 +100,7 @@ fn osstr_positional_user_override() {
let r = App::new("df")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -114,7 +114,7 @@ fn default_if_arg_present_no_default() {
let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'"))
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -126,7 +126,7 @@ fn default_if_arg_present_no_default_user_override() {
let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'"))
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -142,7 +142,7 @@ fn default_if_arg_present_no_arg_with_default() {
.default_value("first")
.default_value_if("opt", None, "default"),
)
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -158,7 +158,7 @@ fn default_if_arg_present_with_default() {
.default_value("first")
.default_value_if("opt", None, "default"),
)
.get_matches_from_safe(vec!["", "--opt", "some"]);
.try_get_matches_from(vec!["", "--opt", "some"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -174,7 +174,7 @@ fn default_if_arg_present_with_default_user_override() {
.default_value("first")
.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());
let m = r.unwrap();
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_if("opt", None, "default"),
)
.get_matches_from_safe(vec!["", "other"]);
.try_get_matches_from(vec!["", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -204,7 +204,7 @@ fn default_if_arg_present_with_value_no_default() {
let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'"))
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -216,7 +216,7 @@ fn default_if_arg_present_with_value_no_default_fail() {
let r = App::new("df")
.arg(Arg::from("--opt [FILE] 'some arg'"))
.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());
let m = r.unwrap();
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")
.arg(Arg::from("--opt [FILE] 'some arg'"))
.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());
let m = r.unwrap();
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_if("opt", Some("some"), "default"),
)
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
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_if("opt", Some("some"), "default"),
)
.get_matches_from_safe(vec!["", "--opt", "other"]);
.try_get_matches_from(vec!["", "--opt", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -275,7 +275,7 @@ fn default_if_arg_present_with_value_with_default() {
.default_value("first")
.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());
let m = r.unwrap();
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_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());
let m = r.unwrap();
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_if("opt", Some("some"), "default"),
)
.get_matches_from_safe(vec!["", "other"]);
.try_get_matches_from(vec!["", "other"]);
assert!(r.is_ok());
let m = r.unwrap();
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_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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -342,7 +342,7 @@ fn default_ifs_arg_present() {
.default_value("first")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -359,7 +359,7 @@ fn default_ifs_arg_present_user_override() {
.default_value("first")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -376,7 +376,7 @@ fn default_ifs_arg_present_order() {
.default_value("first")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -408,7 +408,7 @@ fn conditional_reqs_fail() {
.required_if("target", "file")
.long("output"),
)
.get_matches_from_safe(vec!["test", "--input", "some"]);
.try_get_matches_from(vec!["test", "--input", "some"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -439,7 +439,7 @@ fn conditional_reqs_pass() {
.required_if("target", "file")
.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());
let m = m.unwrap();
@ -458,7 +458,7 @@ fn issue_1050_num_vals_and_defaults() {
.number_of_values(1)
.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());
let m = res.unwrap();
assert_eq!(m.value_of("exit-code"), Some("1"));

View file

@ -3,7 +3,7 @@ extern crate regex;
use std::str;
use clap::{App, AppSettings, Arg, SubCommand};
use clap::{App, AppSettings, Arg, };
include!("../clap-test.rs");
@ -225,7 +225,7 @@ fn derive_order_subcommand_propagate() {
.global_setting(AppSettings::DeriveDisplayOrder)
.version("1.2")
.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("option_b")
.long("option_b")
@ -252,7 +252,7 @@ fn unified_help_subcommand_propagate() {
let app = App::new("test")
.global_setting(AppSettings::UnifiedHelpMessage)
.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("option_b")
.long("option_b")
@ -280,7 +280,7 @@ fn unified_help_and_derive_order_subcommand_propagate() {
.global_setting(AppSettings::DeriveDisplayOrder)
.global_setting(AppSettings::UnifiedHelpMessage)
.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("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::UnifiedHelpMessage)
.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("option_b")
.long("option_b")

View file

@ -11,7 +11,7 @@ fn env() {
let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV"))
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -26,7 +26,7 @@ fn env_os() {
let r = App::new("df")
.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());
let m = r.unwrap();
@ -43,7 +43,7 @@ fn no_env() {
let r = App::new("df")
.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());
let m = r.unwrap();
@ -54,14 +54,15 @@ fn no_env() {
#[test]
fn with_default() {
env::set_var("CLP_TEST_ENV_WITH_DEFAULT", "env");
env::set_var("CLP_TEST_ENV_WD", "env");
let r = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_WITH_DEFAULT")
.env("CLP_TEST_ENV_WD")
.default_value("default"),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -72,11 +73,11 @@ fn with_default() {
#[test]
fn opt_user_override() {
env::set_var("CLP_TEST_ENV_OVERRIDE", "env");
env::set_var("CLP_TEST_ENV_OR", "env");
let r = App::new("df")
.arg(Arg::from("--arg [FILE] 'some arg'").env("CLP_TEST_ENV_OVERRIDE"))
.get_matches_from_safe(vec!["", "--arg", "opt"]);
.arg(Arg::from("--arg [FILE] 'some arg'").env("CLP_TEST_ENV_OR"))
.try_get_matches_from(vec!["", "--arg", "opt"]);
assert!(r.is_ok());
let m = r.unwrap();
@ -87,11 +88,11 @@ fn opt_user_override() {
#[test]
fn positionals() {
env::set_var("CLP_TEST_ENV_POS", "env");
env::set_var("CLP_TEST_ENV_P", "env");
let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_POS"))
.get_matches_from_safe(vec![""]);
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_P"))
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -102,11 +103,11 @@ fn positionals() {
#[test]
fn positionals_user_override() {
env::set_var("CLP_TEST_ENV_USER_OR", "env");
env::set_var("CLP_TEST_ENV_POR", "env");
let r = App::new("df")
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_USER_OR"))
.get_matches_from_safe(vec!["", "opt"]);
.arg(Arg::from("[arg] 'some opt'").env("CLP_TEST_ENV_POR"))
.try_get_matches_from(vec!["", "opt"]);
assert!(r.is_ok());
let m = r.unwrap();
@ -117,15 +118,16 @@ fn positionals_user_override() {
#[test]
fn multiple_one() {
env::set_var("CLP_TEST_ENV_MULT1", "env");
env::set_var("CLP_TEST_ENV_MO", "env");
let r = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_MULT1")
.env("CLP_TEST_ENV_MO")
.use_delimiter(true)
.multiple(true),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -144,7 +146,8 @@ fn multiple_three() {
.env("CLP_TEST_ENV_MULTI1")
.use_delimiter(true)
.multiple(true),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -165,7 +168,8 @@ fn multiple_no_delimiter() {
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_MULTI2")
.multiple(true),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -179,14 +183,15 @@ fn multiple_no_delimiter() {
#[test]
fn possible_value() {
env::set_var("CLP_TEST_ENV_POS_VAL", "env");
env::set_var("CLP_TEST_ENV_PV", "env");
let r = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_POS_VAL")
.env("CLP_TEST_ENV_PV")
.possible_value("env"),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -197,26 +202,27 @@ fn possible_value() {
#[test]
fn not_possible_value() {
env::set_var("CLP_TEST_ENV_NOT_POS_VAL", "env");
env::set_var("CLP_TEST_ENV_NPV", "env");
let r = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_NOT_POS_VAL")
.env("CLP_TEST_ENV_NPV")
.possible_value("never"),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_err());
}
#[test]
fn validator() {
env::set_var("CLP_TEST_ENV_VAL", "env");
env::set_var("CLP_TEST_ENV_VDOR", "env");
let r = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_VAL")
.env("CLP_TEST_ENV_VDOR")
.validator(|s| {
if s == "env" {
Ok(())
@ -224,7 +230,8 @@ fn validator() {
Err("not equal".to_string())
}
}),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_ok());
let m = r.unwrap();
@ -235,12 +242,12 @@ fn validator() {
#[test]
fn validator_output() {
env::set_var("CLP_TEST_ENV_VAL_OUT", "42");
env::set_var("CLP_TEST_ENV_VO", "42");
let m = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_VAL_OUT")
.env("CLP_TEST_ENV_VO")
.validator(|s| s.parse::<i32>()),
).get_matches_from(vec![""]);
@ -249,12 +256,12 @@ fn validator_output() {
#[test]
fn validator_invalid() {
env::set_var("CLP_TEST_ENV_INVAL", "env");
env::set_var("CLP_TEST_ENV_IV", "env");
let r = App::new("df")
.arg(
Arg::from("[arg] 'some opt'")
.env("CLP_TEST_ENV_INVAL")
.env("CLP_TEST_ENV_IV")
.validator(|s| {
if s != "env" {
Ok(())
@ -262,7 +269,8 @@ fn validator_invalid() {
Err("is equal".to_string())
}
}),
).get_matches_from_safe(vec![""]);
)
.try_get_matches_from(vec![""]);
assert!(r.is_err());
}

View file

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

View file

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

View file

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

View file

@ -4,7 +4,7 @@ extern crate regex;
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
Kevin K.
@ -440,8 +440,8 @@ ARGS:
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
-v, --verbose Prints out more stuff.
-V, --version Prints version information
OPTIONS:
-f, --frequency <HERTZ> The sampling frequency.
@ -541,7 +541,7 @@ fn setup() -> App<'static, 'static> {
#[test]
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_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -549,7 +549,7 @@ fn help_short() {
#[test]
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_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -557,7 +557,7 @@ fn help_long() {
#[test]
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_eq!(m.unwrap_err().kind, ErrorKind::UnknownArgument);
@ -567,10 +567,11 @@ fn help_no_subcommand() {
fn help_subcommand() {
let m = setup()
.subcommand(
SubCommand::with_name("test")
App::new("test")
.about("tests things")
.arg_from_usage("-v --verbose 'with verbosity'"),
).get_matches_from_safe(vec!["myprog", "help"]);
.arg("-v --verbose 'with verbosity'"),
)
.try_get_matches_from(vec!["myprog", "help"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -637,7 +638,7 @@ fn args_with_last_usage() {
#[test]
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_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -645,7 +646,7 @@ fn subcommand_short_help() {
#[test]
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_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -653,7 +654,7 @@ fn subcommand_long_help() {
#[test]
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_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
@ -687,15 +688,13 @@ fn after_and_before_help_output() {
#[test]
fn multi_level_sc_help() {
let app = App::new("ctest").subcommand(
SubCommand::with_name("subcmd").subcommand(
SubCommand::with_name("multi")
App::new("subcmd").subcommand(
App::new("multi")
.about("tests subcommands")
.author("Kevin K. <kbknapp@gmail.com>")
.version("0.1")
.args_from_usage(
"-f, --flag 'tests flags'
-o, --option [scoption]... 'tests options'",
),
.arg("-f, --flag 'tests flags'")
.arg("-o, --option [scoption]... 'tests options'")
),
);
assert!(test::compare_output(
@ -708,7 +707,7 @@ fn multi_level_sc_help() {
#[test]
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(
app,
"ctest --help",
@ -828,7 +827,7 @@ fn issue_626_variable_panic() {
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.")
.takes_value(true))
.get_matches_from_safe(vec!["ctest", "--help"]);
.try_get_matches_from(vec!["ctest", "--help"]);
}
}
@ -981,7 +980,7 @@ fn issue_760() {
#[test]
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] [-e PATTERN | -f FILE ]... [<path> ...]
rg [OPTIONS] --files [<path> ...]
@ -995,13 +994,14 @@ fn ripgrep_usage() {
fn ripgrep_usage_using_templates() {
let app = App::new("ripgrep")
.version("0.5")
.usage(
.override_usage(
"
rg [OPTIONS] <pattern> [<path> ...]
rg [OPTIONS] [-e PATTERN | -f FILE ]... [<path> ...]
rg [OPTIONS] --files [<path> ...]
rg [OPTIONS] --type-list",
).template(
)
.help_template(
"\
{bin} {version}
@ -1019,9 +1019,9 @@ fn sc_negates_reqs() {
let app = App::new("prog")
.version("1.0")
.setting(AppSettings::SubcommandsNegateReqs)
.arg_from_usage("-o, --opt <FILE> 'tests options'")
.arg("-o, --opt <FILE> 'tests options'")
.arg(Arg::with_name("PATH").help("help"))
.subcommand(SubCommand::with_name("test"));
.subcommand(App::new("test"));
assert!(test::compare_output(
app,
"prog --help",
@ -1034,10 +1034,9 @@ fn sc_negates_reqs() {
fn hidden_args() {
let app = App::new("prog")
.version("1.0")
.args_from_usage(
"-f, --flag 'testing flags'
-o, --opt [FILE] 'tests options'",
).arg(Arg::with_name("pos").hidden(true));
.arg("-f, --flag 'testing flags'")
.arg("-o, --opt [FILE] 'tests options'")
.arg(Arg::with_name("pos").hidden(true));
assert!(test::compare_output(app, "prog --help", HIDDEN_ARGS, false));
}
@ -1046,11 +1045,10 @@ fn args_negate_sc() {
let app = App::new("prog")
.version("1.0")
.setting(AppSettings::ArgsNegateSubcommands)
.args_from_usage(
"-f, --flag 'testing flags'
-o, --opt [FILE] 'tests options'",
).arg(Arg::with_name("PATH").help("help"))
.subcommand(SubCommand::with_name("test"));
.arg("-f, --flag 'testing flags'")
.arg("-o, --opt [FILE] 'tests options'")
.arg(Arg::with_name("PATH").help("help"))
.subcommand(App::new("test"));
assert!(test::compare_output(
app,
"prog --help",
@ -1063,11 +1061,10 @@ fn args_negate_sc() {
fn issue_1046_hidden_scs() {
let app = App::new("prog")
.version("1.0")
.args_from_usage(
"-f, --flag 'testing flags'
-o, --opt [FILE] 'tests options'",
).arg(Arg::with_name("PATH").help("some"))
.subcommand(SubCommand::with_name("test").setting(AppSettings::Hidden));
.arg("-f, --flag 'testing flags'")
.arg("-o, --opt [FILE] 'tests options'")
.arg(Arg::with_name("PATH").help("some"))
.subcommand(App::new("test").setting(AppSettings::Hidden));
assert!(test::compare_output(
app,
"prog --help",
@ -1092,10 +1089,8 @@ fn customize_version_and_help() {
.version("0.1")
.author("Nobody <odysseus@example.com>")
.about("You can customize the version and help text")
.help_short("H")
.help_message("Print help information")
.version_short("v")
.version_message("Print version information");
.mut_arg("help", |h| h.short('H').long("help").help("Print help information"))
.mut_arg("version", |v| v.short('v').long("version").help("Print version information"));
assert!(test::compare_output(
app,
"customize --help",
@ -1153,7 +1148,8 @@ fn last_arg_mult_usage_req_with_sc() {
.last(true)
.required(true)
.help("some"),
).subcommand(SubCommand::with_name("test").about("some"));
)
.subcommand(App::new("test").about("some"));
assert!(test::compare_output(
app,
"last --help",
@ -1174,7 +1170,8 @@ fn last_arg_mult_usage_with_sc() {
.multiple(true)
.last(true)
.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));
}
@ -1213,24 +1210,14 @@ fn issue_1112_setup() -> App<'static, 'static> {
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.arg(
Arg::with_name("help")
.long("help")
.short('h')
.help("some help"),
).subcommand(
SubCommand::with_name("foo").arg(
Arg::with_name("help")
.short('h')
.long("help")
.help("some help"),
),
)
.global_setting(AppSettings::NoAutoHelp)
.arg(Arg::from("-h, --help 'some help'"))
.subcommand(App::new("foo").arg(Arg::from("-h, --help 'some help'")))
}
#[test]
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.unwrap().is_present("help"));
@ -1238,7 +1225,7 @@ fn issue_1112_override_help_long() {
#[test]
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.unwrap().is_present("help"));
@ -1246,7 +1233,7 @@ fn issue_1112_override_help_short() {
#[test]
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!(
@ -1259,7 +1246,7 @@ fn issue_1112_override_help_subcmd_long() {
#[test]
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!(
@ -1457,7 +1444,7 @@ FLAGS:
#[test]
fn show_long_about_issue_897() {
let app = App::new("ctest").version("0.1").subcommand(
SubCommand::with_name("foo")
App::new("foo")
.version("0.1")
.about("About foo")
.long_about("Long about foo"),
@ -1483,7 +1470,7 @@ FLAGS:
#[test]
fn show_short_about_issue_897() {
let app = App::new("ctest").version("0.1").subcommand(
SubCommand::with_name("foo")
App::new("foo")
.version("0.1")
.about("About foo")
.long_about("Long about foo"),

View file

@ -110,7 +110,7 @@ fn quoted_arg_long_name() {
);
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.");
assert!(matches.is_present("option2"));
}
@ -148,7 +148,7 @@ fn quoted_arg_name() {
);
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.");
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());
let matches = result.expect("Expected to successfully match the given args.");
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());
let matches = result.expect("Expected to successfully match the given args.");
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());
let err = result.unwrap_err();
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());
let err = result.unwrap_err();
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());
let matches = result.expect("Expected to successfully match the given args.");
assert!(!matches.is_present("difficulty"));

View file

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

View file

@ -318,7 +318,7 @@ fn leading_hyphen_fail() {
fn leading_hyphen_with_flag_after() {
let r = App::new("mvae")
.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"]);
assert!(r.is_ok());
let m = r.unwrap();
@ -331,7 +331,7 @@ fn leading_hyphen_with_flag_after() {
fn leading_hyphen_with_flag_before() {
let r = App::new("mvae")
.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"]);
assert!(r.is_ok());
let m = r.unwrap();
@ -348,7 +348,7 @@ fn leading_hyphen_with_only_pos_follows() {
.number_of_values(1)
.setting(ArgSettings::AllowHyphenValues),
)
.arg_from_usage("[arg] 'some arg'")
.arg("[arg] 'some arg'")
.try_get_matches_from(vec!["", "-o", "-2", "--", "val"]);
assert!(r.is_ok(), "{:?}", r);
let m = r.unwrap();
@ -371,7 +371,7 @@ fn did_you_mean() {
#[test]
fn issue_665() {
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(
"--subject-prefix [Subject-Prefix] 'Use [Subject-Prefix] instead of the standard [PATCH] prefix'") )
.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("[arg] 'some arg'"),
])
.get_matches_from_safe(vec!["", "--", "-f"]);
.try_get_matches_from(vec!["", "--", "-f"]);
assert!(r.is_ok());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -21,14 +21,14 @@ fn only_pos_follow() {
fn issue_946() {
let r = App::new("compiletest")
.setting(clap::AppSettings::AllowLeadingHyphen)
.args_from_usage("--exact 'filters match exactly'")
.arg("--exact 'filters match exactly'")
.arg(
clap::Arg::with_name("filter")
.index(1)
.takes_value(true)
.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);
let matches = r.unwrap();
@ -43,7 +43,7 @@ fn positional() {
Arg::from("-f, --flag 'some flag'"),
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);
let m = r.unwrap();
assert!(m.is_present("positional"));
@ -65,7 +65,7 @@ fn positional() {
fn lots_o_vals() {
let r = App::new("opts")
.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",
@ -108,7 +108,7 @@ fn positional_multiple() {
Arg::from("-f, --flag 'some flag'"),
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);
let m = r.unwrap();
assert!(m.is_present("positional"));
@ -126,7 +126,7 @@ fn positional_multiple_3() {
Arg::from("-f, --flag 'some flag'"),
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);
let m = r.unwrap();
assert!(m.is_present("positional"));
@ -144,7 +144,7 @@ fn positional_multiple_2() {
Arg::from("-f, --flag 'some flag'"),
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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::UnknownArgument);
@ -159,7 +159,7 @@ fn positional_possible_values() {
.index(1)
.possible_value("test123"),
])
.get_matches_from_safe(vec!["", "-f", "test123"]);
.try_get_matches_from(vec!["", "-f", "test123"]);
assert!(r.is_ok(), "{:#?}", r);
let m = r.unwrap();
assert!(m.is_present("positional"));
@ -186,35 +186,35 @@ fn positional_hyphen_does_not_panic() {
#[test]
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]");
}
#[test]
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]...");
}
#[test]
fn multiple_positional_usage_string() {
let mut app = App::new("test")
.arg_from_usage("[FILE] 'some file'")
.arg_from_usage("[FILES]... 'some file'");
.arg("[FILE] 'some file'")
.arg("[FILES]... 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test [ARGS]");
}
#[test]
fn multiple_positional_one_required_usage_string() {
let mut app = App::new("test")
.arg_from_usage("<FILE> 'some file'")
.arg_from_usage("[FILES]... 'some file'");
.arg("<FILE> 'some file'")
.arg("[FILES]... 'some file'");
assert_eq!(app.generate_usage(), "USAGE:\n test <FILE> [FILES]...");
}
#[test]
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>");
}
@ -222,9 +222,9 @@ fn single_positional_required_usage_string() {
#[should_panic]
fn missing_required() {
let r = App::new("test")
.arg_from_usage("[FILE1] 'some file'")
.arg_from_usage("<FILE2> 'some file'")
.get_matches_from_safe(vec!["test", "file"]);
.arg("[FILE1] 'some file'")
.arg("<FILE2> 'some file'")
.try_get_matches_from(vec!["test", "file"]);
assert!(r.is_err());
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}
@ -232,9 +232,9 @@ fn missing_required() {
#[test]
fn missing_required_2() {
let r = App::new("test")
.arg_from_usage("<FILE1> 'some file'")
.arg_from_usage("<FILE2> 'some file'")
.get_matches_from_safe(vec!["test", "file"]);
.arg("<FILE1> 'some file'")
.arg("<FILE2> 'some file'")
.try_get_matches_from(vec!["test", "file"]);
assert!(r.is_err());
assert_eq!(r.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
}
@ -242,10 +242,10 @@ fn missing_required_2() {
#[test]
fn last_positional() {
let r = App::new("test")
.arg_from_usage("<TARGET> 'some target'")
.arg_from_usage("[CORPUS] 'some corpus'")
.arg("<TARGET> 'some target'")
.arg("[CORPUS] 'some corpus'")
.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());
let m = r.unwrap();
assert_eq!(m.values_of("ARGS").unwrap().collect::<Vec<_>>(), &["arg"]);
@ -254,10 +254,10 @@ fn last_positional() {
#[test]
fn last_positional_no_double_dash() {
let r = App::new("test")
.arg_from_usage("<TARGET> 'some target'")
.arg_from_usage("[CORPUS] 'some corpus'")
.arg("<TARGET> 'some target'")
.arg("[CORPUS] 'some corpus'")
.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_eq!(r.unwrap_err().kind, ErrorKind::UnknownArgument);
}
@ -265,9 +265,9 @@ fn last_positional_no_double_dash() {
#[test]
fn last_positional_second_to_last_mult() {
let r = App::new("test")
.arg_from_usage("<TARGET> 'some target'")
.arg_from_usage("[CORPUS]... 'some corpus'")
.arg("<TARGET> 'some target'")
.arg("[CORPUS]... 'some corpus'")
.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);
}

View file

@ -5,7 +5,7 @@ use clap::{App, Arg, ErrorKind};
fn flag_overrides_itself() {
let res = App::new("posix")
.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());
let m = res.unwrap();
assert!(m.is_present("flag"));
@ -16,7 +16,7 @@ fn flag_overrides_itself() {
fn mult_flag_overrides_itself() {
let res = App::new("posix")
.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());
let m = res.unwrap();
assert!(m.is_present("flag"));
@ -27,7 +27,7 @@ fn mult_flag_overrides_itself() {
fn option_overrides_itself() {
let res = App::new("posix")
.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());
let m = res.unwrap();
assert!(m.is_present("opt"));
@ -44,7 +44,7 @@ fn mult_option_require_delim_overrides_itself() {
.number_of_values(1)
.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());
let m = res.unwrap();
assert!(m.is_present("opt"));
@ -59,7 +59,7 @@ fn mult_option_require_delim_overrides_itself() {
fn mult_option_overrides_itself() {
let res = App::new("posix")
.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",
]);
assert!(res.is_ok());
@ -90,7 +90,7 @@ fn pos_mult_overrides_itself() {
// opts with multiple
let res = App::new("posix")
.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());
let m = res.unwrap();
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("-d, --debug 'other 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());
let m = result.unwrap();
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("-d, --debug 'other 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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::ArgumentConflict);
@ -261,7 +261,7 @@ fn pos_required_overridden_by_flag() {
let result = App::new("require_overriden")
.arg(Arg::with_name("pos").index(1).required(true))
.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());
}
@ -293,7 +293,7 @@ fn require_overriden_4() {
.arg(Arg::from("-f, --flag 'some flag'").requires("debug"))
.arg(Arg::from("-d, --debug 'other 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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);

View file

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

View file

@ -4,7 +4,7 @@ extern crate regex;
#[cfg(test)]
mod tests {
include!("../clap-test.rs");
use clap::{App, Arg, ArgMatches, ArgSettings, SubCommand};
use clap::{App, Arg, ArgMatches, ArgSettings, };
fn get_app() -> App<'static, 'static> {
App::new("myprog")
@ -23,7 +23,7 @@ mod tests {
.setting(ArgSettings::MultipleOccurrences)
.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> {

View file

@ -36,7 +36,7 @@ fn flag_required() {
let result = App::new("flag_required")
.arg(Arg::from("-f, --flag 'some flag'").requires("color"))
.arg(Arg::from("-c, --color 'third flag'"))
.get_matches_from_safe(vec!["", "-f"]);
.try_get_matches_from(vec!["", "-f"]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -57,7 +57,7 @@ fn option_required() {
let result = App::new("option_required")
.arg(Arg::from("-f [flag] 'some flag'").requires("c"))
.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());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -79,7 +79,7 @@ fn option_required_2() {
fn positional_required() {
let result = App::new("positional_required")
.arg(Arg::with_name("flag").index(1).required(true))
.get_matches_from_safe(vec![""]);
.try_get_matches_from(vec![""]);
assert!(result.is_err());
let err = result.err().unwrap();
assert_eq!(err.kind, ErrorKind::MissingRequiredArgument);
@ -106,7 +106,7 @@ fn group_required() {
)
.arg(Arg::from("--some 'some arg'"))
.arg(Arg::from("--other 'other arg'"))
.get_matches_from_safe(vec!["", "-f"]);
.try_get_matches_from(vec!["", "-f"]);
assert!(result.is_err());
let err = result.err().unwrap();
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("-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());
}
@ -224,7 +224,7 @@ fn required_unless() {
.long("config"),
)
.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());
let m = res.unwrap();
@ -242,7 +242,7 @@ fn required_unless_err() {
.long("config"),
)
.arg(Arg::with_name("dbg").long("debug"))
.get_matches_from_safe(vec!["unlesstest"]);
.try_get_matches_from(vec!["unlesstest"]);
assert!(res.is_err());
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("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());
let m = res.unwrap();
@ -281,7 +281,7 @@ fn required_unless_all_err() {
)
.arg(Arg::with_name("dbg").long("debug"))
.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_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("infile").short('i').takes_value(true))
.get_matches_from_safe(vec!["unlessone", "--debug"]);
.try_get_matches_from(vec!["unlessone", "--debug"]);
assert!(res.is_ok());
let m = res.unwrap();
@ -321,7 +321,7 @@ fn required_unless_one_2() {
)
.arg(Arg::with_name("dbg").long("debug"))
.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());
let m = res.unwrap();
@ -340,7 +340,7 @@ fn required_unless_one_works_with_short() {
.short('x')
.required_unless_one(&["a", "b"]),
)
.get_matches_from_safe(vec!["unlessone", "-a"]);
.try_get_matches_from(vec!["unlessone", "-a"]);
assert!(res.is_ok());
}
@ -355,7 +355,7 @@ fn required_unless_one_works_with_short_err() {
.short('x')
.required_unless_one(&["a", "b"]),
)
.get_matches_from_safe(vec!["unlessone"]);
.try_get_matches_from(vec!["unlessone"]);
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("b").short('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());
}
@ -381,7 +381,7 @@ fn required_unless_one_works_with_long() {
.long("x_is_the_option")
.required_unless_one(&["a", "b"]),
)
.get_matches_from_safe(vec!["unlessone", "-a"]);
.try_get_matches_from(vec!["unlessone", "-a"]);
assert!(res.is_ok());
}
@ -397,7 +397,7 @@ fn required_unless_one_1() {
)
.arg(Arg::with_name("dbg").long("debug"))
.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());
let m = res.unwrap();
@ -417,7 +417,7 @@ fn required_unless_one_err() {
)
.arg(Arg::with_name("dbg").long("debug"))
.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_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -445,7 +445,7 @@ fn requires_if_present_val() {
.long("config"),
)
.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_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("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_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("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());
}
@ -494,7 +494,7 @@ fn requires_if_present_val_no_present_pass() {
.long("config"),
)
.arg(Arg::with_name("extra").long("extra"))
.get_matches_from_safe(vec!["unlessone"]);
.try_get_matches_from(vec!["unlessone"]);
assert!(res.is_ok());
}
@ -511,7 +511,7 @@ fn required_if_val_present_pass() {
.long("config"),
)
.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());
}
@ -526,7 +526,7 @@ fn required_if_val_present_fail() {
.long("config"),
)
.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_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -576,7 +576,7 @@ fn required_if_wrong_val() {
.long("config"),
)
.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());
}
@ -592,7 +592,7 @@ fn required_ifs_val_present_pass() {
)
.arg(Arg::with_name("option").takes_value(true).long("option"))
.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());
}
@ -608,7 +608,7 @@ fn required_ifs_val_present_fail() {
)
.arg(Arg::with_name("extra").takes_value(true).long("extra"))
.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_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("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());
}
@ -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("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_eq!(res.unwrap_err().kind, ErrorKind::MissingRequiredArgument);
@ -674,19 +674,19 @@ For more information try --help";
fn issue_1158_app() -> App<'static, 'static> {
App::new("example")
.arg(
Arg::from_usage("-c, --config [FILE] 'Custom config file.'")
Arg::from("-c, --config [FILE] 'Custom config file.'")
.required_unless("ID")
.conflicts_with("ID"),
)
.arg(
Arg::from_usage("[ID] 'ID'")
Arg::from("[ID] 'ID'")
.required_unless("config")
.conflicts_with("config")
.requires_all(&["x", "y", "z"]),
)
.arg(Arg::from_usage("-x [X] 'X'"))
.arg(Arg::from_usage("-y [Y] 'Y'"))
.arg(Arg::from_usage("-z [Z] 'Z'"))
.arg(Arg::from("-x [X] 'X'"))
.arg(Arg::from("-y [Y] 'Y'"))
.arg(Arg::from("-z [Z] 'Z'"))
}
#[test]

View file

@ -3,7 +3,7 @@ extern crate regex;
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
@ -56,7 +56,7 @@ For more information try --help";
fn subcommand() {
let m = App::new("test")
.subcommand(
SubCommand::with_name("some").arg(
App::new("some").arg(
Arg::with_name("test")
.short('t')
.long("test")
@ -76,7 +76,7 @@ fn subcommand() {
fn subcommand_none_given() {
let m = App::new("test")
.subcommand(
SubCommand::with_name("some").arg(
App::new("some").arg(
Arg::with_name("test")
.short('t')
.long("test")
@ -93,15 +93,16 @@ fn subcommand_none_given() {
fn subcommand_multiple() {
let m = App::new("test")
.subcommands(vec![
SubCommand::with_name("some").arg(
App::new("some").arg(
Arg::with_name("test")
.short('t')
.long("test")
.takes_value(true)
.help("testing testing"),
),
SubCommand::with_name("add").arg(Arg::with_name("roster").short('r')),
]).arg(Arg::with_name("other").long("other"))
App::new("add").arg(Arg::with_name("roster").short('r')),
])
.arg(Arg::with_name("other").long("other"))
.get_matches_from(vec!["myprog", "some", "--test", "testing"]);
assert!(m.subcommand_matches("some").is_some());
@ -115,7 +116,7 @@ fn subcommand_multiple() {
#[test]
fn single_alias() {
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"]);
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -123,7 +124,7 @@ fn single_alias() {
#[test]
fn multiple_aliases() {
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"]);
assert_eq!(m.subcommand_name(), Some("test"));
}
@ -131,7 +132,7 @@ fn multiple_aliases() {
#[test]
#[cfg(feature = "suggestions")]
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));
}
@ -139,7 +140,7 @@ fn subcmd_did_you_mean_output() {
#[cfg(feature = "suggestions")]
fn subcmd_did_you_mean_output_arg() {
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));
}
@ -147,8 +148,8 @@ fn subcmd_did_you_mean_output_arg() {
#[test]
fn alias_help() {
let m = App::new("myprog")
.subcommand(SubCommand::with_name("test").alias("do-stuff"))
.get_matches_from_safe(vec!["myprog", "help", "do-stuff"]);
.subcommand(App::new("test").alias("do-stuff"))
.try_get_matches_from(vec!["myprog", "help", "do-stuff"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::HelpDisplayed);
}
@ -156,7 +157,7 @@ fn alias_help() {
#[test]
fn visible_aliases_help_output() {
let app = App::new("clap-test").version("2.6").subcommand(
SubCommand::with_name("test")
App::new("test")
.about("Some help")
.alias("invisible")
.visible_alias("dongle")
@ -173,7 +174,7 @@ fn visible_aliases_help_output() {
#[test]
fn invisible_aliases_help_output() {
let app = App::new("clap-test").version("2.6").subcommand(
SubCommand::with_name("test")
App::new("test")
.about("Some help")
.alias("invisible"),
);
@ -189,8 +190,8 @@ fn invisible_aliases_help_output() {
fn issue_1031_args_with_same_name() {
let res = App::new("prog")
.arg(Arg::from("--ui-path=<PATH>"))
.subcommand(SubCommand::with_name("signer"))
.get_matches_from_safe(vec!["prog", "--ui-path", "signer"]);
.subcommand(App::new("signer"))
.try_get_matches_from(vec!["prog", "--ui-path", "signer"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap();
@ -201,8 +202,8 @@ fn issue_1031_args_with_same_name() {
fn issue_1031_args_with_same_name_no_more_vals() {
let res = App::new("prog")
.arg(Arg::from("--ui-path=<PATH>"))
.subcommand(SubCommand::with_name("signer"))
.get_matches_from_safe(vec!["prog", "--ui-path", "value", "signer"]);
.subcommand(App::new("signer"))
.try_get_matches_from(vec!["prog", "--ui-path", "value", "signer"]);
assert!(res.is_ok(), "{:?}", res.unwrap_err().kind);
let m = res.unwrap();
@ -217,7 +218,7 @@ fn issue_1161_multiple_hyphen_hyphen() {
.arg(Arg::with_name("eff").short('f'))
.arg(Arg::with_name("pea").short('p').takes_value(true))
.arg(Arg::with_name("slop").multiple(true).last(true))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
"-f",
"-p=bob",
"--",

View file

@ -1,7 +1,7 @@
extern crate clap;
extern crate regex;
use clap::{App, SubCommand};
use clap::{App, };
include!("../clap-test.rs");
@ -51,7 +51,7 @@ SUBCOMMANDS:
#[test]
fn with_template() {
let app = app_example1().template(EXAMPLE1_TMPL_S);
let app = app_example1().help_template(EXAMPLE1_TMPL_S);
assert!(test::compare_output(
app,
"MyApp --help",
@ -62,7 +62,7 @@ fn with_template() {
#[test]
fn custom_template() {
let app = app_example1().template(EXAMPLE1_TMPS_F);
let app = app_example1().help_template(EXAMPLE1_TMPS_F);
assert!(test::compare_output(
app,
"MyApp --help",
@ -77,7 +77,7 @@ fn template_empty() {
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.template("");
.help_template("");
assert!(test::compare_output(app, "MyApp --help", "", false));
}
@ -87,7 +87,7 @@ fn template_notag() {
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.template("test no tag test");
.help_template("test no tag test");
assert!(test::compare_output(
app,
"MyApp --help",
@ -102,7 +102,7 @@ fn template_unknowntag() {
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.template("test {unknown_tag} test");
.help_template("test {unknown_tag} test");
assert!(test::compare_output(
app,
"MyApp --help",
@ -117,7 +117,7 @@ fn template_author_version() {
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.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(
app,
"MyApp --help",
@ -133,14 +133,12 @@ fn app_example1<'b, 'c>() -> App<'b, 'c> {
.version("1.0")
.author("Kevin K. <kbknapp@gmail.com>")
.about("Does awesome things")
.args_from_usage(
"-c, --config=[FILE] 'Sets a custom config file'
<output> 'Sets an optional output file'
-d... 'Turn debugging information on'",
)
.arg("-c, --config=[FILE] 'Sets a custom config file'")
.arg("<output> 'Sets an optional output file'")
.arg("-d... 'Turn debugging information on'")
.subcommand(
SubCommand::with_name("test")
App::new("test")
.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('b'),
])
.get_matches_safe();
.try_get_matches();
}
#[test]
@ -21,7 +21,7 @@ fn unique_arg_shorts() {
Arg::with_name("arg1").short('a'),
Arg::with_name("arg2").short('a'),
])
.get_matches_safe();
.try_get_matches();
}
#[test]
@ -32,5 +32,5 @@ fn unique_arg_longs() {
Arg::with_name("arg1").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")
.arg(Arg::from("<arg> 'some arg'"))
.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_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
}
@ -21,7 +21,7 @@ fn invalid_utf8_strict_option_short_space() {
let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from("-a"),
OsString::from_vec(vec![0xe9]),
@ -35,7 +35,7 @@ fn invalid_utf8_strict_option_short_equals() {
let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
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")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]);
@ -61,7 +61,7 @@ fn invalid_utf8_strict_option_long_space() {
let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from("--arg"),
OsString::from_vec(vec![0xe9]),
@ -75,7 +75,7 @@ fn invalid_utf8_strict_option_long_equals() {
let m = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.setting(AppSettings::StrictUtf8)
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
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() {
let r = App::new("bad_utf8")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -98,7 +98,7 @@ fn invalid_utf8_lossy_positional() {
fn invalid_utf8_lossy_option_short_space() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from("-a"),
OsString::from_vec(vec![0xe9]),
@ -113,7 +113,7 @@ fn invalid_utf8_lossy_option_short_space() {
fn invalid_utf8_lossy_option_short_equals() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
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() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
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() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from("--arg"),
OsString::from_vec(vec![0xe9]),
@ -156,7 +156,7 @@ fn invalid_utf8_lossy_option_long_space() {
fn invalid_utf8_lossy_option_long_equals() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
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() {
let r = App::new("bad_utf8")
.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());
let m = r.unwrap();
assert!(m.is_present("arg"));
@ -184,7 +184,7 @@ fn invalid_utf8_positional() {
fn invalid_utf8_option_short_space() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from("-a"),
OsString::from_vec(vec![0xe9]),
@ -202,7 +202,7 @@ fn invalid_utf8_option_short_space() {
fn invalid_utf8_option_short_equals() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
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() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]);
@ -236,7 +236,7 @@ fn invalid_utf8_option_short_no_space() {
fn invalid_utf8_option_long_space() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from("--arg"),
OsString::from_vec(vec![0xe9]),
@ -254,7 +254,7 @@ fn invalid_utf8_option_long_space() {
fn invalid_utf8_option_long_equals() {
let r = App::new("bad_utf8")
.arg(Arg::from("-a, --arg <arg> 'some arg'"))
.get_matches_from_safe(vec![
.try_get_matches_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
]);

View file

@ -3,7 +3,7 @@ extern crate regex;
use std::str;
use clap::{App, Arg, ErrorKind};
use clap::{App, Arg, ErrorKind, AppSettings};
include!("../clap-test.rs");
@ -15,7 +15,7 @@ fn version_short() {
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.get_matches_from_safe(vec!["myprog", "-V"]);
.try_get_matches_from(vec!["myprog", "-V"]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind, ErrorKind::VersionDisplayed);
@ -36,7 +36,7 @@ fn version_long() {
#[test]
fn complex_version_output() {
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()
let mut ver = vec![];
@ -47,12 +47,13 @@ fn complex_version_output() {
#[test]
fn override_ver() {
let m = App::new("test")
.setting(AppSettings::NoAutoVersion)
.author("Kevin K.")
.about("tests stuff")
.version("1.3")
.arg(Arg::from("-v, --version 'some version'"))
.get_matches_from_safe(vec!["test", "--version"]);
.mut_arg("version", |a| a.short('v').long("version").help("some 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"));
}

View file

@ -23,7 +23,7 @@ fn help_message() {
let yml = load_yaml!("app.yml");
let mut app = App::from_yaml(yml);
// 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();
app.write_help(&mut help_buffer).unwrap();
@ -38,7 +38,7 @@ fn author() {
let yml = load_yaml!("app.yml");
let mut app = App::from_yaml(yml);
// 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();
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`
* `App::args` and `App::arg` are more generic
* 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