mirror of
https://github.com/clap-rs/clap
synced 2025-03-04 07:17:26 +00:00
Changed tabs to spaces
This commit is contained in:
parent
1814d87192
commit
9f4cdc9fa3
9 changed files with 1248 additions and 1248 deletions
|
@ -4,81 +4,81 @@ use clap::{App, Arg, SubCommand};
|
|||
|
||||
fn main() {
|
||||
|
||||
// Create an application with 5 possible arguments (2 auto generated) and 2 subcommands (1 auto generated)
|
||||
// - A config file
|
||||
// + Uses "-c filename" or "--config filename"
|
||||
// - An output file
|
||||
// + A positional argument (i.e. "$ myapp output_filename")
|
||||
// - A debug flag
|
||||
// + Uses "-d" or "--debug"
|
||||
// + Allows multiple occurrences of such as "-dd" (for vary levels of debugging, as an example)
|
||||
// - A help flag (automatically generated by clap)
|
||||
// + Uses "-h" or "--help" (Only autogenerated if you do NOT specify your own "-h" or "--help")
|
||||
// - A version flag (automatically generated by clap)
|
||||
// + Uses "-v" or "--version" (Only autogenerated if you do NOT specify your own "-v" or "--version")
|
||||
// - A subcommand "test" (subcommands behave like their own apps, with their own arguments
|
||||
// + Used by "$ myapp test" with the following arguments
|
||||
// > A list flag
|
||||
// = Uses "-l" (usage is "$ myapp test -l"
|
||||
// > A help flag (automatically generated by clap
|
||||
// = Uses "-h" or "--help" (full usage "$ myapp test -h" or "$ myapp test --help")
|
||||
// > A version flag (automatically generated by clap
|
||||
// = Uses "-v" or "--version" (full usage "$ myapp test -v" or "$ myapp test --version")
|
||||
// - A subcommand "help" (automatically generated by clap because we specified a subcommand of our own)
|
||||
// + Used by "$ myapp help" (same functionality as "-h" or "--help")
|
||||
let matches = App::new("MyApp")
|
||||
.version("1.0")
|
||||
.author("Kevin K. <kbknapp@gmail.com>")
|
||||
.about("Does awesome things")
|
||||
.arg(Arg::new("config")
|
||||
.short("c")
|
||||
.long("config")
|
||||
.help("Sets a custom config file")
|
||||
.takes_value(true))
|
||||
.arg(Arg::new("output")
|
||||
.help("Sets an optional output file")
|
||||
.index(1))
|
||||
.arg(Arg::new("debug")
|
||||
.short("d")
|
||||
.multiple(true)
|
||||
.help("Turn debugging information on"))
|
||||
.subcommand(SubCommand::new("test")
|
||||
.about("does testing things")
|
||||
.arg(Arg::new("list")
|
||||
.short("l")
|
||||
.help("lists test values")))
|
||||
.get_matches();
|
||||
// Create an application with 5 possible arguments (2 auto generated) and 2 subcommands (1 auto generated)
|
||||
// - A config file
|
||||
// + Uses "-c filename" or "--config filename"
|
||||
// - An output file
|
||||
// + A positional argument (i.e. "$ myapp output_filename")
|
||||
// - A debug flag
|
||||
// + Uses "-d" or "--debug"
|
||||
// + Allows multiple occurrences of such as "-dd" (for vary levels of debugging, as an example)
|
||||
// - A help flag (automatically generated by clap)
|
||||
// + Uses "-h" or "--help" (Only autogenerated if you do NOT specify your own "-h" or "--help")
|
||||
// - A version flag (automatically generated by clap)
|
||||
// + Uses "-v" or "--version" (Only autogenerated if you do NOT specify your own "-v" or "--version")
|
||||
// - A subcommand "test" (subcommands behave like their own apps, with their own arguments
|
||||
// + Used by "$ myapp test" with the following arguments
|
||||
// > A list flag
|
||||
// = Uses "-l" (usage is "$ myapp test -l"
|
||||
// > A help flag (automatically generated by clap
|
||||
// = Uses "-h" or "--help" (full usage "$ myapp test -h" or "$ myapp test --help")
|
||||
// > A version flag (automatically generated by clap
|
||||
// = Uses "-v" or "--version" (full usage "$ myapp test -v" or "$ myapp test --version")
|
||||
// - A subcommand "help" (automatically generated by clap because we specified a subcommand of our own)
|
||||
// + Used by "$ myapp help" (same functionality as "-h" or "--help")
|
||||
let matches = App::new("MyApp")
|
||||
.version("1.0")
|
||||
.author("Kevin K. <kbknapp@gmail.com>")
|
||||
.about("Does awesome things")
|
||||
.arg(Arg::new("config")
|
||||
.short("c")
|
||||
.long("config")
|
||||
.help("Sets a custom config file")
|
||||
.takes_value(true))
|
||||
.arg(Arg::new("output")
|
||||
.help("Sets an optional output file")
|
||||
.index(1))
|
||||
.arg(Arg::new("debug")
|
||||
.short("d")
|
||||
.multiple(true)
|
||||
.help("Turn debugging information on"))
|
||||
.subcommand(SubCommand::new("test")
|
||||
.about("does testing things")
|
||||
.arg(Arg::new("list")
|
||||
.short("l")
|
||||
.help("lists test values")))
|
||||
.get_matches();
|
||||
|
||||
// You can check the value provided by positional arguments, or option arguments
|
||||
if let Some(o) = matches.value_of("output") {
|
||||
println!("Value for output: {}", o);
|
||||
}
|
||||
// You can check the value provided by positional arguments, or option arguments
|
||||
if let Some(o) = matches.value_of("output") {
|
||||
println!("Value for output: {}", o);
|
||||
}
|
||||
|
||||
if let Some(c) = matches.value_of("config") {
|
||||
println!("Value for config: {}", c);
|
||||
}
|
||||
if let Some(c) = matches.value_of("config") {
|
||||
println!("Value for config: {}", c);
|
||||
}
|
||||
|
||||
// You can see how many times a particular flag or argument occurred
|
||||
// Note, only flags can have multiple occurrences
|
||||
match matches.occurrences_of("debug") {
|
||||
0 => println!("Debug mode is off"),
|
||||
1 => println!("Debug mode is kind of on"),
|
||||
2 => println!("Debug mode is on"),
|
||||
3 | _ => println!("Don't be crazy"),
|
||||
}
|
||||
// You can see how many times a particular flag or argument occurred
|
||||
// Note, only flags can have multiple occurrences
|
||||
match matches.occurrences_of("debug") {
|
||||
0 => println!("Debug mode is off"),
|
||||
1 => println!("Debug mode is kind of on"),
|
||||
2 => println!("Debug mode is on"),
|
||||
3 | _ => println!("Don't be crazy"),
|
||||
}
|
||||
|
||||
// You can check for the existance of subcommands, and if found use their
|
||||
// matches just as you would the top level app
|
||||
if let Some(ref matches) = matches.subcommand_matches("test") {
|
||||
// "$ myapp test" was run
|
||||
if matches.is_present("list") {
|
||||
// "$ myapp test -l" was run
|
||||
println!("Printing testing lists...");
|
||||
} else {
|
||||
println!("Not printing testing lists...");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Continued program logic goes here...
|
||||
// You can check for the existance of subcommands, and if found use their
|
||||
// matches just as you would the top level app
|
||||
if let Some(ref matches) = matches.subcommand_matches("test") {
|
||||
// "$ myapp test" was run
|
||||
if matches.is_present("list") {
|
||||
// "$ myapp test -l" was run
|
||||
println!("Printing testing lists...");
|
||||
} else {
|
||||
println!("Not printing testing lists...");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Continued program logic goes here...
|
||||
}
|
||||
|
|
1558
src/app.rs
1558
src/app.rs
File diff suppressed because it is too large
Load diff
546
src/arg.rs
546
src/arg.rs
|
@ -19,7 +19,7 @@
|
|||
/// .help("Provides a config file to myprog")
|
||||
/// # ).get_matches();
|
||||
pub struct Arg {
|
||||
/// The unique name of the argument, required
|
||||
/// The unique name of the argument, required
|
||||
pub name: &'static str,
|
||||
/// The short version (i.e. single character) of the argument, no preceding `-`
|
||||
/// **NOTE:** `short` is mutually exclusive with `index`
|
||||
|
@ -46,293 +46,293 @@ pub struct Arg {
|
|||
/// I.e. `-v -v -v` or `-vvv`
|
||||
pub multiple: bool,
|
||||
/// A list of names for other arguments that *may not* be used with this flag
|
||||
pub blacklist: Option<Vec<&'static str>>,
|
||||
pub blacklist: Option<Vec<&'static str>>,
|
||||
/// A list of names of other arguments that are *required* to be used when
|
||||
/// this flag is used
|
||||
pub requires: Option<Vec<&'static str>>
|
||||
}
|
||||
|
||||
impl Arg {
|
||||
/// Creates a new instace of `Arg` using a unique string name.
|
||||
/// The name will be used by the library consumer to get information about
|
||||
/// whether or not the argument was used at runtime.
|
||||
///
|
||||
/// **NOTE:** in the case of arguments that take values (i.e. `takes_value(true)`)
|
||||
/// and positional arguments (i.e. those without a `-` or `--`) the name will also
|
||||
/// be displayed when the user prints the usage/help information of the program.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// Arg::new("conifg")
|
||||
/// # .short("c")
|
||||
/// # ).get_matches();
|
||||
pub fn new(n: &'static str) -> Arg {
|
||||
Arg {
|
||||
name: n,
|
||||
short: None,
|
||||
long: None,
|
||||
help: None,
|
||||
required: false,
|
||||
takes_value: false,
|
||||
multiple: false,
|
||||
index: None,
|
||||
blacklist: Some(vec![]),
|
||||
requires: Some(vec![]),
|
||||
}
|
||||
}
|
||||
/// Creates a new instace of `Arg` using a unique string name.
|
||||
/// The name will be used by the library consumer to get information about
|
||||
/// whether or not the argument was used at runtime.
|
||||
///
|
||||
/// **NOTE:** in the case of arguments that take values (i.e. `takes_value(true)`)
|
||||
/// and positional arguments (i.e. those without a `-` or `--`) the name will also
|
||||
/// be displayed when the user prints the usage/help information of the program.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// Arg::new("conifg")
|
||||
/// # .short("c")
|
||||
/// # ).get_matches();
|
||||
pub fn new(n: &'static str) -> Arg {
|
||||
Arg {
|
||||
name: n,
|
||||
short: None,
|
||||
long: None,
|
||||
help: None,
|
||||
required: false,
|
||||
takes_value: false,
|
||||
multiple: false,
|
||||
index: None,
|
||||
blacklist: Some(vec![]),
|
||||
requires: Some(vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the short version of the argument without the preceding `-`.
|
||||
///
|
||||
///
|
||||
/// By default `clap` automatically assigns `v` and `h` to display version and help information
|
||||
/// respectivly. You may use `v` or `h` for your own purposes, in which case `clap` simply
|
||||
/// will not asign those to the displaying of version or help.
|
||||
///
|
||||
/// **NOTE:** Any leading `-` characters will be stripped, and only the first
|
||||
/// non `-` chacter will be used as the `short` version, i.e. for when the user
|
||||
/// mistakenly sets the short to `-o` or the like.
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .short("c")
|
||||
/// # ).get_matches();
|
||||
pub fn short(mut self, s: &'static str) -> Arg {
|
||||
self.short = Some(s.trim_left_matches(|c| c == '-')
|
||||
.char_at(0));
|
||||
self
|
||||
}
|
||||
/// Sets the short version of the argument without the preceding `-`.
|
||||
///
|
||||
///
|
||||
/// By default `clap` automatically assigns `v` and `h` to display version and help information
|
||||
/// respectivly. You may use `v` or `h` for your own purposes, in which case `clap` simply
|
||||
/// will not asign those to the displaying of version or help.
|
||||
///
|
||||
/// **NOTE:** Any leading `-` characters will be stripped, and only the first
|
||||
/// non `-` chacter will be used as the `short` version, i.e. for when the user
|
||||
/// mistakenly sets the short to `-o` or the like.
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .short("c")
|
||||
/// # ).get_matches();
|
||||
pub fn short(mut self, s: &'static str) -> Arg {
|
||||
self.short = Some(s.trim_left_matches(|c| c == '-')
|
||||
.char_at(0));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the long version of the argument without the preceding `--`.
|
||||
///
|
||||
/// By default `clap` automatically assigns `version` and `help` to display version and help information
|
||||
/// respectivly. You may use `version` or `help` for your own purposes, in which case `clap` simply
|
||||
/// will not asign those to the displaying of version or help automatically, and you will have to do
|
||||
/// so manually.
|
||||
///
|
||||
/// **NOTE:** Any leading `-` characters will be stripped i.e. for
|
||||
/// when the user mistakenly sets the short to `--out` or the like.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .long("config")
|
||||
/// # ).get_matches();
|
||||
pub fn long(mut self, l: &'static str) -> Arg {
|
||||
self.long = Some(l.trim_left_matches(|c| c == '-'));
|
||||
self
|
||||
}
|
||||
/// Sets the long version of the argument without the preceding `--`.
|
||||
///
|
||||
/// By default `clap` automatically assigns `version` and `help` to display version and help information
|
||||
/// respectivly. You may use `version` or `help` for your own purposes, in which case `clap` simply
|
||||
/// will not asign those to the displaying of version or help automatically, and you will have to do
|
||||
/// so manually.
|
||||
///
|
||||
/// **NOTE:** Any leading `-` characters will be stripped i.e. for
|
||||
/// when the user mistakenly sets the short to `--out` or the like.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .long("config")
|
||||
/// # ).get_matches();
|
||||
pub fn long(mut self, l: &'static str) -> Arg {
|
||||
self.long = Some(l.trim_left_matches(|c| c == '-'));
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the help text of the argument that will be displayed to the user
|
||||
/// when they print the usage/help information.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .help("The config file used by the myprog")
|
||||
/// # ).get_matches();
|
||||
pub fn help(mut self, h: &'static str) -> Arg {
|
||||
self.help = Some(h);
|
||||
self
|
||||
}
|
||||
/// Sets the help text of the argument that will be displayed to the user
|
||||
/// when they print the usage/help information.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .help("The config file used by the myprog")
|
||||
/// # ).get_matches();
|
||||
pub fn help(mut self, h: &'static str) -> Arg {
|
||||
self.help = Some(h);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets whether or not the argument is required by default. Required by
|
||||
/// default means it is required, when no other mutually exlusive rules have
|
||||
/// been evaluated. Mutually exclusive rules take precedence over being required
|
||||
/// by default.
|
||||
///
|
||||
/// **NOTE:** Flags (i.e. not positional, or arguments that take values)
|
||||
/// cannot be required by default.
|
||||
/// when they print the usage/help information.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .required(true)
|
||||
/// # ).get_matches();
|
||||
pub fn required(mut self, r: bool) -> Arg {
|
||||
self.required = r;
|
||||
self
|
||||
}
|
||||
/// Sets whether or not the argument is required by default. Required by
|
||||
/// default means it is required, when no other mutually exlusive rules have
|
||||
/// been evaluated. Mutually exclusive rules take precedence over being required
|
||||
/// by default.
|
||||
///
|
||||
/// **NOTE:** Flags (i.e. not positional, or arguments that take values)
|
||||
/// cannot be required by default.
|
||||
/// when they print the usage/help information.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .required(true)
|
||||
/// # ).get_matches();
|
||||
pub fn required(mut self, r: bool) -> Arg {
|
||||
self.required = r;
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets a mutually exclusive argument by name. I.e. when using this argument,
|
||||
/// the following argument can't be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
/// by default. Mutually exclusive rules only need to be set for one of the two
|
||||
/// arguments, they do not need to be set for each.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .mutually_excludes("debug")
|
||||
/// # ).get_matches();
|
||||
pub fn mutually_excludes(mut self, name: &'static str) -> Arg {
|
||||
if let Some(ref mut vec) = self.blacklist {
|
||||
vec.push(name);
|
||||
} else {
|
||||
self.blacklist = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
/// Sets a mutually exclusive argument by name. I.e. when using this argument,
|
||||
/// the following argument can't be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
/// by default. Mutually exclusive rules only need to be set for one of the two
|
||||
/// arguments, they do not need to be set for each.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .mutually_excludes("debug")
|
||||
/// # ).get_matches();
|
||||
pub fn mutually_excludes(mut self, name: &'static str) -> Arg {
|
||||
if let Some(ref mut vec) = self.blacklist {
|
||||
vec.push(name);
|
||||
} else {
|
||||
self.blacklist = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets a mutually exclusive arguments by names. I.e. when using this argument,
|
||||
/// the following argument can't be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
/// by default. Mutually exclusive rules only need to be set for one of the two
|
||||
/// arguments, they do not need to be set for each.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .mutually_excludes_all(
|
||||
/// vec!["debug", "input"])
|
||||
/// # ).get_matches();
|
||||
pub fn mutually_excludes_all(mut self, names: Vec<&'static str>) -> Arg {
|
||||
if let Some(ref mut vec) = self.blacklist {
|
||||
for n in names {
|
||||
vec.push(n);
|
||||
}
|
||||
} else {
|
||||
self.blacklist = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
/// Sets a mutually exclusive arguments by names. I.e. when using this argument,
|
||||
/// the following argument can't be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
/// by default. Mutually exclusive rules only need to be set for one of the two
|
||||
/// arguments, they do not need to be set for each.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .mutually_excludes_all(
|
||||
/// vec!["debug", "input"])
|
||||
/// # ).get_matches();
|
||||
pub fn mutually_excludes_all(mut self, names: Vec<&'static str>) -> Arg {
|
||||
if let Some(ref mut vec) = self.blacklist {
|
||||
for n in names {
|
||||
vec.push(n);
|
||||
}
|
||||
} else {
|
||||
self.blacklist = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets an argument by name that is required when this one is presnet I.e. when
|
||||
/// using this argument, the following argument *must* be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .requires("debug")
|
||||
/// # ).get_matches();
|
||||
pub fn requires(mut self, name: &'static str) -> Arg {
|
||||
if let Some(ref mut vec) = self.requires {
|
||||
vec.push(name);
|
||||
} else {
|
||||
self.requires = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
/// Sets an argument by name that is required when this one is presnet I.e. when
|
||||
/// using this argument, the following argument *must* be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .requires("debug")
|
||||
/// # ).get_matches();
|
||||
pub fn requires(mut self, name: &'static str) -> Arg {
|
||||
if let Some(ref mut vec) = self.requires {
|
||||
vec.push(name);
|
||||
} else {
|
||||
self.requires = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets arguments by names that are required when this one is presnet I.e. when
|
||||
/// using this argument, the following arguments *must* be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
/// by default.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .requires_all(
|
||||
/// vec!["debug", "input"])
|
||||
/// # ).get_matches();
|
||||
pub fn requires_all(mut self, names: Vec<&'static str>) -> Arg {
|
||||
if let Some(ref mut vec) = self.requires {
|
||||
for n in names {
|
||||
vec.push(n);
|
||||
}
|
||||
} else {
|
||||
self.requires = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
/// Sets arguments by names that are required when this one is presnet I.e. when
|
||||
/// using this argument, the following arguments *must* be present.
|
||||
///
|
||||
/// **NOTE:** Mutually exclusive rules take precedence over being required
|
||||
/// by default.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let myprog = App::new("myprog").arg(Arg::new("conifg")
|
||||
/// .requires_all(
|
||||
/// vec!["debug", "input"])
|
||||
/// # ).get_matches();
|
||||
pub fn requires_all(mut self, names: Vec<&'static str>) -> Arg {
|
||||
if let Some(ref mut vec) = self.requires {
|
||||
for n in names {
|
||||
vec.push(n);
|
||||
}
|
||||
} else {
|
||||
self.requires = Some(vec![]);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Specifies that the argument takes an additional value at run time.
|
||||
///
|
||||
/// **NOTE:** When setting this to `true` the `name` of the argument
|
||||
/// will be used when printing the help/usage information to the user.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .takes_value(true)
|
||||
/// # ).get_matches();
|
||||
pub fn takes_value(mut self, tv: bool) -> Arg {
|
||||
assert!(self.index == None);
|
||||
self.takes_value = tv;
|
||||
self
|
||||
}
|
||||
/// Specifies that the argument takes an additional value at run time.
|
||||
///
|
||||
/// **NOTE:** When setting this to `true` the `name` of the argument
|
||||
/// will be used when printing the help/usage information to the user.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .takes_value(true)
|
||||
/// # ).get_matches();
|
||||
pub fn takes_value(mut self, tv: bool) -> Arg {
|
||||
assert!(self.index == None);
|
||||
self.takes_value = tv;
|
||||
self
|
||||
}
|
||||
|
||||
/// Specifies the index of a positional argument starting at 1.
|
||||
///
|
||||
/// **NOTE:** When setting this, any `short` or `long` values you set
|
||||
/// are ignored as positional arguments cannot have a `short` or `long`.
|
||||
/// Also, the name will be used when printing the help/usage information
|
||||
/// to the user.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .index(1)
|
||||
/// # ).get_matches();
|
||||
pub fn index(mut self, idx: u8) -> Arg {
|
||||
assert!(self.takes_value == false);
|
||||
if idx < 1 { panic!("Argument index must start at 1"); }
|
||||
self.index = Some(idx);
|
||||
self
|
||||
}
|
||||
/// Specifies the index of a positional argument starting at 1.
|
||||
///
|
||||
/// **NOTE:** When setting this, any `short` or `long` values you set
|
||||
/// are ignored as positional arguments cannot have a `short` or `long`.
|
||||
/// Also, the name will be used when printing the help/usage information
|
||||
/// to the user.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("conifg")
|
||||
/// .index(1)
|
||||
/// # ).get_matches();
|
||||
pub fn index(mut self, idx: u8) -> Arg {
|
||||
assert!(self.takes_value == false);
|
||||
if idx < 1 { panic!("Argument index must start at 1"); }
|
||||
self.index = Some(idx);
|
||||
self
|
||||
}
|
||||
|
||||
/// Specifies if the flag may appear more than once such as for multiple debugging
|
||||
/// levels (as an example). `-ddd` for three levels of debugging, or `-d -d -d`.
|
||||
/// When this is set to `true` you recieve the number of occurances the user supplied
|
||||
/// of a particular flag at runtime.
|
||||
///
|
||||
/// **NOTE:** When setting this, any `takes_value` or `index` values you set
|
||||
/// are ignored as flags cannot have a values or an `index`.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("debug")
|
||||
/// .multiple(true)
|
||||
/// # ).get_matches();
|
||||
pub fn multiple(mut self, multi: bool) -> Arg {
|
||||
assert!(self.takes_value == false);
|
||||
assert!(self.index == None);
|
||||
self.multiple = multi;
|
||||
self
|
||||
}
|
||||
/// Specifies if the flag may appear more than once such as for multiple debugging
|
||||
/// levels (as an example). `-ddd` for three levels of debugging, or `-d -d -d`.
|
||||
/// When this is set to `true` you recieve the number of occurances the user supplied
|
||||
/// of a particular flag at runtime.
|
||||
///
|
||||
/// **NOTE:** When setting this, any `takes_value` or `index` values you set
|
||||
/// are ignored as flags cannot have a values or an `index`.
|
||||
///
|
||||
/// Example:
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg};
|
||||
/// # let matches = App::new("myprog")
|
||||
/// # .arg(
|
||||
/// # Arg::new("debug")
|
||||
/// .multiple(true)
|
||||
/// # ).get_matches();
|
||||
pub fn multiple(mut self, multi: bool) -> Arg {
|
||||
assert!(self.takes_value == false);
|
||||
assert!(self.index == None);
|
||||
self.multiple = multi;
|
||||
self
|
||||
}
|
||||
}
|
|
@ -75,15 +75,15 @@ impl ArgMatches {
|
|||
/// # use clap::{App, Arg};
|
||||
/// let matches = App::new("myprog").get_matches();
|
||||
/// ```
|
||||
pub fn new(name: &'static str) -> ArgMatches {
|
||||
ArgMatches {
|
||||
matches_of: name,
|
||||
pub fn new(name: &'static str) -> ArgMatches {
|
||||
ArgMatches {
|
||||
matches_of: name,
|
||||
flags: HashMap::new(),
|
||||
opts: HashMap::new(),
|
||||
positionals: HashMap::new(),
|
||||
subcommand: None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the value of a specific option or positional argument (i.e. an argument that takes
|
||||
/// an additional value at runtime). If the option wasn't present at runtime
|
||||
|
@ -98,19 +98,19 @@ impl ArgMatches {
|
|||
/// println!("Value for output: {}", o);
|
||||
/// }
|
||||
/// ```
|
||||
pub fn value_of(&self, name: &'static str) -> Option<&String> {
|
||||
pub fn value_of(&self, name: &'static str) -> Option<&String> {
|
||||
if let Some(ref opt) = self.opts.get(name) {
|
||||
if let Some(ref v) = opt.value {
|
||||
return Some(v);
|
||||
}
|
||||
if let Some(ref v) = opt.value {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
if let Some(ref pos) = self.positionals.get(name) {
|
||||
if let Some(ref v) = pos.value {
|
||||
return Some(v);
|
||||
}
|
||||
if let Some(ref v) = pos.value {
|
||||
return Some(v);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if a flag was argument was supplied at runtime. **DOES NOT** work for
|
||||
/// option or positional arguments (use `.value_of()` instead)
|
||||
|
@ -125,7 +125,7 @@ impl ArgMatches {
|
|||
/// println!("The output argument was used!");
|
||||
/// }
|
||||
/// ```
|
||||
pub fn is_present(&self, name: &'static str) -> bool {
|
||||
pub fn is_present(&self, name: &'static str) -> bool {
|
||||
if let Some((sc_name, _ )) = self.subcommand {
|
||||
if sc_name == name { return true; }
|
||||
}
|
||||
|
@ -135,7 +135,7 @@ impl ArgMatches {
|
|||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks the number of occurrences of a flag at runtime.
|
||||
///
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
/// $ myprog -a --some
|
||||
/// ```
|
||||
pub struct FlagArg {
|
||||
/// The unique name of the argument, required
|
||||
/// The unique name of the argument, required
|
||||
pub name: &'static str,
|
||||
/// The short version (i.e. single character)
|
||||
/// of the argument, no preceding `-`
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
///
|
||||
/// **NOTE:** The long version may also use the `--argument=value` version too
|
||||
pub struct OptArg {
|
||||
/// The unique name of the argument, required
|
||||
/// The unique name of the argument, required
|
||||
pub name: &'static str,
|
||||
/// The short version (i.e. single character) of the argument, no preceding `-`
|
||||
pub short: Option<char>,
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
///
|
||||
/// **NOTE:** The index starts at `1` **NOT** `0`
|
||||
pub struct PosArg {
|
||||
/// The unique name of the argument, required
|
||||
/// The unique name of the argument, required
|
||||
pub name: &'static str,
|
||||
/// The string of text that will displayed to the user when the application's
|
||||
/// `help` text is displayed
|
||||
|
|
170
src/lib.rs
170
src/lib.rs
|
@ -22,50 +22,50 @@
|
|||
//! // ...
|
||||
//!
|
||||
//! let matches = App::new("MyApp")
|
||||
//! .version("1.0")
|
||||
//! .author("Kevin K. <kbknapp@gmail.com>")
|
||||
//! .about("Does awesome things")
|
||||
//! .arg(Arg::new("config")
|
||||
//! .short("c")
|
||||
//! .long("config")
|
||||
//! .help("Sets a custom config file")
|
||||
//! .takes_value(true))
|
||||
//! .arg(Arg::new("output")
|
||||
//! .help("Sets an optional output file")
|
||||
//! .index(1))
|
||||
//! .arg(Arg::new("debug")
|
||||
//! .short("d")
|
||||
//! .multiple(true)
|
||||
//! .help("Turn debugging information on"))
|
||||
//! .subcommand(SubCommand::new("test")
|
||||
//! .about("Has test sub functionality")
|
||||
//! .arg(Arg::new("verbose")
|
||||
//! .short("v")
|
||||
//! .help("Display verbose information")))
|
||||
//! .get_matches();
|
||||
//! .version("1.0")
|
||||
//! .author("Kevin K. <kbknapp@gmail.com>")
|
||||
//! .about("Does awesome things")
|
||||
//! .arg(Arg::new("config")
|
||||
//! .short("c")
|
||||
//! .long("config")
|
||||
//! .help("Sets a custom config file")
|
||||
//! .takes_value(true))
|
||||
//! .arg(Arg::new("output")
|
||||
//! .help("Sets an optional output file")
|
||||
//! .index(1))
|
||||
//! .arg(Arg::new("debug")
|
||||
//! .short("d")
|
||||
//! .multiple(true)
|
||||
//! .help("Turn debugging information on"))
|
||||
//! .subcommand(SubCommand::new("test")
|
||||
//! .about("Has test sub functionality")
|
||||
//! .arg(Arg::new("verbose")
|
||||
//! .short("v")
|
||||
//! .help("Display verbose information")))
|
||||
//! .get_matches();
|
||||
//!
|
||||
//! if let Some(o) = matches.value_of("output") {
|
||||
//! println!("Value for output: {}", o);
|
||||
//! }
|
||||
//! if let Some(o) = matches.value_of("output") {
|
||||
//! println!("Value for output: {}", o);
|
||||
//! }
|
||||
//!
|
||||
//! if let Some(c) = matches.value_of("config") {
|
||||
//! println!("Value for config: {}", c);
|
||||
//! }
|
||||
//! if let Some(c) = matches.value_of("config") {
|
||||
//! println!("Value for config: {}", c);
|
||||
//! }
|
||||
//!
|
||||
//! match matches.occurrences_of("debug") {
|
||||
//! 0 => println!("Debug mode is off"),
|
||||
//! 1 => println!("Debug mode is kind of on"),
|
||||
//! 2 => println!("Debug mode is on"),
|
||||
//! 3 | _ => println!("Don't be crazy"),
|
||||
//! 0 => println!("Debug mode is off"),
|
||||
//! 1 => println!("Debug mode is kind of on"),
|
||||
//! 2 => println!("Debug mode is on"),
|
||||
//! 3 | _ => println!("Don't be crazy"),
|
||||
//! }
|
||||
//!
|
||||
//! if let Some(ref matches) = matches.subcommand_matches("test") {
|
||||
//! if matches.is_present("verbose") {
|
||||
//! println!("Printing verbose test info...");
|
||||
//! } else {
|
||||
//! println!("Not printing regular test info...");
|
||||
//! }
|
||||
//! }
|
||||
//! if matches.is_present("verbose") {
|
||||
//! println!("Printing verbose test info...");
|
||||
//! } else {
|
||||
//! println!("Not printing regular test info...");
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! // more porgram logic goes here...
|
||||
//! ```
|
||||
|
@ -79,22 +79,22 @@
|
|||
//! Does awesome things
|
||||
//!
|
||||
//! USAGE:
|
||||
//! MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
|
||||
//! MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
|
||||
//!
|
||||
//! FLAGS:
|
||||
//! -d Turn debugging information on
|
||||
//! -h,--help Prints this message
|
||||
//! -v,--version Prints version information
|
||||
//! -d Turn debugging information on
|
||||
//! -h,--help Prints this message
|
||||
//! -v,--version Prints version information
|
||||
//!
|
||||
//! OPTIONS:
|
||||
//! -c,--config <config> Sets a custom config file
|
||||
//! -c,--config <config> Sets a custom config file
|
||||
//!
|
||||
//! POSITIONAL ARGUMENTS:
|
||||
//! output Sets an optional output file
|
||||
//! output Sets an optional output file
|
||||
//!
|
||||
//! SUBCOMMANDS:
|
||||
//! help Prints this message
|
||||
//! test Has test sub-functionality
|
||||
//! help Prints this message
|
||||
//! test Has test sub-functionality
|
||||
//! ```
|
||||
|
||||
pub use argmatches::ArgMatches;
|
||||
|
@ -112,44 +112,44 @@ mod subcommand;
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unique_arg_names(){
|
||||
App::new("some").args(vec![
|
||||
Arg::new("arg").short("a"),
|
||||
Arg::new("arg").short("b")
|
||||
]);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unique_arg_shorts(){
|
||||
App::new("some").args(vec![
|
||||
Arg::new("arg1").short("a"),
|
||||
Arg::new("arg2").short("a")
|
||||
]);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unique_arg_longs(){
|
||||
App::new("some").args(vec![
|
||||
Arg::new("arg1").long("long"),
|
||||
Arg::new("arg2").long("long")
|
||||
]);
|
||||
}
|
||||
#[test]
|
||||
fn create_app(){
|
||||
App::new("some").about("about").author("author").version("1.0");
|
||||
}
|
||||
#[test]
|
||||
fn create_arg_flag(){
|
||||
Arg::new("some").short("a").long("long").help("help with some arg").multiple(true);
|
||||
}
|
||||
#[test]
|
||||
fn create_arg_pos(){
|
||||
Arg::new("some").index(1).help("help with some arg").required(true);
|
||||
}
|
||||
#[test]
|
||||
fn create_arg_opt(){
|
||||
Arg::new("some").short("s").long("some").takes_value(true).help("help with some arg").required(true);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unique_arg_names(){
|
||||
App::new("some").args(vec![
|
||||
Arg::new("arg").short("a"),
|
||||
Arg::new("arg").short("b")
|
||||
]);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unique_arg_shorts(){
|
||||
App::new("some").args(vec![
|
||||
Arg::new("arg1").short("a"),
|
||||
Arg::new("arg2").short("a")
|
||||
]);
|
||||
}
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn unique_arg_longs(){
|
||||
App::new("some").args(vec![
|
||||
Arg::new("arg1").long("long"),
|
||||
Arg::new("arg2").long("long")
|
||||
]);
|
||||
}
|
||||
#[test]
|
||||
fn create_app(){
|
||||
App::new("some").about("about").author("author").version("1.0");
|
||||
}
|
||||
#[test]
|
||||
fn create_arg_flag(){
|
||||
Arg::new("some").short("a").long("long").help("help with some arg").multiple(true);
|
||||
}
|
||||
#[test]
|
||||
fn create_arg_pos(){
|
||||
Arg::new("some").index(1).help("help with some arg").required(true);
|
||||
}
|
||||
#[test]
|
||||
fn create_arg_opt(){
|
||||
Arg::new("some").short("s").long("some").takes_value(true).help("help with some arg").required(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,29 +15,29 @@ use argmatches::ArgMatches;
|
|||
/// # let matches = App::new("myprog")
|
||||
/// # .subcommand(
|
||||
/// SubCommand::new("conifg")
|
||||
/// .about("Used for configuration")
|
||||
/// .arg(Arg::new("config_file")
|
||||
/// .help("The configuration file to use")
|
||||
/// .index(1))
|
||||
/// .about("Used for configuration")
|
||||
/// .arg(Arg::new("config_file")
|
||||
/// .help("The configuration file to use")
|
||||
/// .index(1))
|
||||
/// # ).get_matches();
|
||||
pub struct SubCommand {
|
||||
pub name: &'static str,
|
||||
pub matches: ArgMatches
|
||||
pub name: &'static str,
|
||||
pub matches: ArgMatches
|
||||
}
|
||||
|
||||
impl SubCommand {
|
||||
/// Creates a new instance of a subcommand requiring a name. Will be displayed
|
||||
/// to the user when they print version or help and usage information.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg, SubCommand};
|
||||
/// # let prog = App::new("myprog").subcommand(
|
||||
/// SubCommand::new("config")
|
||||
/// # ).get_matches();
|
||||
/// ```
|
||||
pub fn new(name: &'static str) -> App {
|
||||
App::new(name)
|
||||
}
|
||||
/// Creates a new instance of a subcommand requiring a name. Will be displayed
|
||||
/// to the user when they print version or help and usage information.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```no_run
|
||||
/// # use clap::{App, Arg, SubCommand};
|
||||
/// # let prog = App::new("myprog").subcommand(
|
||||
/// SubCommand::new("config")
|
||||
/// # ).get_matches();
|
||||
/// ```
|
||||
pub fn new(name: &'static str) -> App {
|
||||
App::new(name)
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue