Struct clap::Arg
[-] [+]
- [src]
+ [src]
pub struct Arg<'n, 'l, 'h, 'b, 'p, 'r> { - pub name: &'n str, - pub short: Option<char>, - pub long: Option<&'l str>, - pub help: Option<&'h str>, - pub required: bool, - pub takes_value: bool, - pub index: Option<u8>, - pub multiple: bool, - pub blacklist: Option<Vec<&'b str>>, - pub possible_vals: Option<Vec<&'p str>>, - pub requires: Option<Vec<&'r str>>, -}
The abstract representation of a command line argument used by the consumer of the library.
+ // some fields omitted +}The abstract representation of a command line argument used by the consumer of the library. +Used to set all the options and relationships that define a valid argument for the program.
This struct is used by the library consumer and describes the command line arguments for -their program. -and then evaluates the settings the consumer provided and determines the concret -argument struct to use when parsing.
+their program. Then evaluates the settings the consumer provided and determines the concret +argument type to use when parsing.Example
@@ -71,30 +61,7 @@ argument struct to use when parsing. .takes_value(true) .help("Provides a config file to myprog")-
Fields
-Methods
impl<'n, 'l, 'h, 'b, 'p, 'r> Arg<'n, 'l, 'h, 'b, 'p, 'r>
Methods
impl<'n, 'l, 'h, 'b, 'p, 'r> Arg<'n, 'l, 'h, 'b, 'p, 'r>
fn new(n: &'n str) -> Arg<'n, 'l, 'h, 'b, 'p, 'r>
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.
Struct clap::ArgMatches
[-] [+]
- [src]
+ [src]
pub struct ArgMatches<'a> { - pub flags: HashMap<&'a str, FlagArg>, - pub opts: HashMap<&'a str, OptArg>, - pub positionals: HashMap<&'a str, PosArg>, - pub subcommand: Option<Box<SubCommand<'a>>>, - pub usage: Option<String>, -}
Used to get information about the arguments that -where supplied to the program at runtime.
- -Fields of ArgMatches
aren't designed to be used directly, only
-the methods in order to query information.
Used to get information about the arguments that where supplied to the program at runtime by
+the user. To get a new instance of this struct you use .get_matches()
of the App
struct.
Example
@@ -66,19 +59,17 @@ the methods in order to query information. println!("Value for output: {}", o); } -// Although not advised, if you have a required argument -// you can call .unwrap() because the program will exit long before -// here at noticing the user didn't supply a required argument... -// use at your own risk ;) +// If you have a required argument you can call .unwrap() because the program will exit long +// before this point if the user didn't specify it at runtime. println!("Config file: {}", matches.value_of("config").unwrap()); -// You can check the present of an argument +// You can check the presence of an argument if matches.is_present("debug") { - // Checking if "debug" was present was necessary, - // as occurrences returns 0 if a flag isn't found - // but we can check how many times "debug" was found - // if we allow multiple (if multiple isn't allowed it always be 1 or 0) - if matches.occurrences_of("debug") > 1 { + // Another way to check if an argument was present, or if it occurred multiple times is to + // use occurrences_of() which returns 0 if an argument isn't found at runtime, or the + // number of times that it occurred, if it was. To allow an argument to appear more than + // once, you must use the .multiple(true) method, otherwise it will only return 1 or 0. + if matches.occurrences_of("debug") > 2 { println!("Debug mode is REALLY on"); } else { println!("Debug mode kind of on"); @@ -95,16 +86,7 @@ the methods in order to query information. } }-
Fields
-flags | |
opts | |
positionals | |
subcommand | |
usage |
Methods
impl<'a> ArgMatches<'a>
fn new() -> ArgMatches<'a>
-Creates a new instance of ArgMatches
. This ins't called directly, but
-through the .get_matches()
method of App
Example
-let matches = App::new("myprog").get_matches(); --
fn value_of<'n>(&self, name: &'n str) -> Option<&str>
+Methods
impl<'a> ArgMatches<'a>
fn value_of<'n>(&self, name: &'n str) -> Option<&str>
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
it returns None
.
values_of()
as value_of()
will only return the
fn values_of<'n>(&self, name: &'n str) -> Option<Vec<&str>>
Gets the values of a specific option or positional argument in a vector (i.e. an argument
-that takes an additional value at runtime). If the option wasn't present at runtime it
+that takes multiple values at runtime). If the option wasn't present at runtime it
returns None
None }
fn is_present<'n>(&self, name: &'n str) -> bool
-Checks if a flag was argument was supplied at runtime. DOES NOT work for
-option or positional arguments (use .value_of()
instead)
Returns if an argument was present at runtime.
Example
@@ -145,10 +126,9 @@ option or positional arguments (use .value_of()
instead)
}
fn occurrences_of<'n>(&self, name: &'n str) -> u8
-Checks the number of occurrences of an option, flag, or positional argument at runtime.
-If an option or flag isn't present it will return 0
, if the option or flag doesn't
-allow multiple occurrences, it will return 1
no matter how many times it occurred
-(unless it wasn't prsent) at all.
Returns the number of occurrences of an option, flag, or positional argument at runtime.
+If an argument isn't present it will return 0
. Can be used on arguments which don't
+allow multiple occurrences, but will obviously only return 0
or 1
.
Example
@@ -159,7 +139,8 @@ allow multiple occurrences, it will return 1
no matter how many tim
}
fn subcommand_matches<'n>(&self, name: &'n str) -> Option<&ArgMatches>
-If a subcommand was found, returns the ArgMatches struct associated with it's matches
+Returns the ArgMatches
for a particular subcommand or None if the subcommand wasn't
+present at runtime.
Example
@@ -168,7 +149,10 @@ allow multiple occurrences, it will return 1
no matter how many tim
}
fn subcommand_name(&self) -> Option<&str>
-If a subcommand was found, returns the name associated with it
+Returns the name of the subcommand used of the parent App
, or None
if one wasn't found
NOTE: Only a single subcommand may be present per App
at runtime, does NOT check for
+the name of sub-subcommand's names
Example
@@ -179,7 +163,8 @@ allow multiple occurrences, it will return 1
no matter how many tim
}
fn subcommand(&self) -> (&str, Option<&ArgMatches>)
-If a subcommand was found, returns the name and matches associated with it
+Returns the name and ArgMatches
of the subcommand used at runtime or ("", None) if one
+wasn't found.
Example
@@ -190,7 +175,7 @@ allow multiple occurrences, it will return 1
no matter how many tim
}
fn usage(&self) -> &str
-Returns a slice of the usage
+Returns a string slice of the usage statement for the App
(or SubCommand
)
Example
diff --git a/docs/search-index.js b/docs/search-index.js index 24e68c67..46855d9c 100644 --- a/docs/search-index.js +++ b/docs/search-index.js @@ -1,3 +1,3 @@ var searchIndex = {}; -searchIndex['clap'] = {"items":[[0,"","clap","# clap",null,null],[3,"Arg","","The abstract representation of a command line argument used by the consumer of the library.",null,null],[12,"name","","The unique name of the argument, required",0,null],[12,"short","","The short version (i.e. single character) of the argument, no preceding `-`\n**NOTE:** `short` is mutually exclusive with `index`",0,null],[12,"long","","The long version of the flag (i.e. word) without the preceding `--`\n**NOTE:** `long` is mutually exclusive with `index`",0,null],[12,"help","","The string of text that will displayed to the user when the application's\n`help` text is displayed",0,null],[12,"required","","If this is a required by default when using the command line program\ni.e. a configuration file that's required for the program to function\n**NOTE:** required by default means, it is required *until* mutually\nexclusive arguments are evaluated.",0,null],[12,"takes_value","","Determines if this argument is an option, vice a flag or positional and\nis mutually exclusive with `index` and `multiple`",0,null],[12,"index","","The index of the argument. `index` is mutually exclusive with `takes_value`\nand `multiple`",0,null],[12,"multiple","","Determines if multiple instances of the same flag are allowed. `multiple`\nis mutually exclusive with `index` and `takes_value`.\nI.e. `-v -v -v` or `-vvv`",0,null],[12,"blacklist","","A list of names for other arguments that *may not* be used with this flag",0,null],[12,"possible_vals","","A list of possible values for an option or positional argument",0,null],[12,"requires","","A list of names of other arguments that are *required* to be used when\nthis flag is used",0,null],[3,"SubCommand","","The abstract representation of a command line subcommand used by the consumer of the library.",null,null],[12,"name","","",1,null],[12,"matches","","",1,null],[3,"ArgMatches","","Used to get information about the arguments that\nwhere supplied to the program at runtime.",null,null],[12,"flags","","",2,null],[12,"opts","","",2,null],[12,"positionals","","",2,null],[12,"subcommand","","",2,null],[12,"usage","","",2,null],[3,"App","","Used to create a representation of the program and all possible command line arguments\nfor parsing at runtime.",null,null],[11,"new","","Creates a new instance of an application requiring a name (such as the binary). Will be displayed\nto the user when they print version or help and usage information.",3,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"author","","Sets a string of author(s)",3,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"about","","Sets a string briefly describing what the program does",3,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"version","","Sets a string of the version number",3,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"usage","","Sets a custom usage string to over-ride the one auto-generated by `clap`",3,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"arg","","Adds an argument to the list of valid possibilties",3,{"inputs":[{"name":"app"},{"name":"arg"}],"output":{"name":"app"}}],[11,"args","","Adds multiple arguments to the list of valid possibilties",3,{"inputs":[{"name":"app"},{"name":"vec"}],"output":{"name":"app"}}],[11,"subcommand","","Adds a subcommand to the list of valid possibilties. Subcommands\nare effectively sub apps, because they can contain their own arguments\nand subcommands. They also function just like apps, in that they get their\nown auto generated help and version switches.",3,{"inputs":[{"name":"app"},{"name":"app"}],"output":{"name":"app"}}],[11,"subcommands","","Adds multiple subcommands to the list of valid possibilties",3,{"inputs":[{"name":"app"},{"name":"vec"}],"output":{"name":"app"}}],[11,"get_matches","","",3,{"inputs":[{"name":"app"}],"output":{"name":"argmatches"}}],[11,"new","","Creates a new instace of `Arg` using a unique string name.\nThe name will be used by the library consumer to get information about\nwhether or not the argument was used at runtime. ",0,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"short","","Sets the short version of the argument without the preceding `-`.",0,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"long","","Sets the long version of the argument without the preceding `--`.",0,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"help","","Sets the help text of the argument that will be displayed to the user\nwhen they print the usage/help information. ",0,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"required","","Sets whether or not the argument is required by default. Required by\ndefault means it is required, when no other mutually exlusive rules have\nbeen evaluated. Mutually exclusive rules take precedence over being required\nby default.",0,{"inputs":[{"name":"arg"},{"name":"bool"}],"output":{"name":"arg"}}],[11,"mutually_excludes","","Sets a mutually exclusive argument by name. I.e. when using this argument,\nthe following argument can't be present.",0,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"mutually_excludes_all","","Sets a mutually exclusive arguments by names. I.e. when using this argument,\nthe following argument can't be present.",0,{"inputs":[{"name":"arg"},{"name":"vec"}],"output":{"name":"arg"}}],[11,"requires","","Sets an argument by name that is required when this one is presnet I.e. when\nusing this argument, the following argument *must* be present.",0,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"requires_all","","Sets arguments by names that are required when this one is presnet I.e. when\nusing this argument, the following arguments *must* be present.",0,{"inputs":[{"name":"arg"},{"name":"vec"}],"output":{"name":"arg"}}],[11,"takes_value","","Specifies that the argument takes an additional value at run time.",0,{"inputs":[{"name":"arg"},{"name":"bool"}],"output":{"name":"arg"}}],[11,"index","","Specifies the index of a positional argument starting at 1.",0,{"inputs":[{"name":"arg"},{"name":"u8"}],"output":{"name":"arg"}}],[11,"multiple","","Specifies if the flag may appear more than once such as for multiple debugging\nlevels (as an example). `-ddd` for three levels of debugging, or `-d -d -d`. \nWhen this is set to `true` you recieve the number of occurances the user supplied\nof a particular flag at runtime.",0,{"inputs":[{"name":"arg"},{"name":"bool"}],"output":{"name":"arg"}}],[11,"possible_values","","Specifies a list of possible values for this argument. At runtime, clap verifies that only\none of the specified values was used, or fails with a usage string.",0,{"inputs":[{"name":"arg"},{"name":"vec"}],"output":{"name":"arg"}}],[11,"new","","Creates a new instance of `ArgMatches`. This ins't called directly, but\nthrough the `.get_matches()` method of `App`",2,{"inputs":[{"name":"argmatches"}],"output":{"name":"argmatches"}}],[11,"value_of","","Gets the value of a specific option or positional argument (i.e. an argument that takes\nan additional value at runtime). If the option wasn't present at runtime\nit returns `None`. ",2,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"option"}}],[11,"values_of","","Gets the values of a specific option or positional argument in a vector (i.e. an argument\nthat takes an additional value at runtime). If the option wasn't present at runtime it\nreturns `None`",2,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"option"}}],[11,"is_present","","Checks if a flag was argument was supplied at runtime. **DOES NOT** work for\noption or positional arguments (use `.value_of()` instead)",2,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"bool"}}],[11,"occurrences_of","","Checks the number of occurrences of an option, flag, or positional argument at runtime.\nIf an option or flag isn't present it will return `0`, if the option or flag doesn't \nallow multiple occurrences, it will return `1` no matter how many times it occurred \n(unless it wasn't prsent) at all.",2,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"u8"}}],[11,"subcommand_matches","","If a subcommand was found, returns the ArgMatches struct associated with it's matches",2,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"option"}}],[11,"subcommand_name","","If a subcommand was found, returns the name associated with it",2,{"inputs":[{"name":"argmatches"}],"output":{"name":"option"}}],[11,"subcommand","","If a subcommand was found, returns the name and matches associated with it",2,null],[11,"usage","","Returns a slice of the usage",2,{"inputs":[{"name":"argmatches"}],"output":{"name":"str"}}],[11,"new","","Creates a new instance of a subcommand requiring a name. Will be displayed\nto the user when they print version or help and usage information.",1,{"inputs":[{"name":"subcommand"},{"name":"str"}],"output":{"name":"app"}}]],"paths":[[3,"Arg"],[3,"SubCommand"],[3,"ArgMatches"],[3,"App"]]}; +searchIndex['clap'] = {"items":[[0,"","clap","# clap",null,null],[3,"Arg","","The abstract representation of a command line argument used by the consumer of the library.\nUsed to set all the options and relationships that define a valid argument for the program.",null,null],[3,"SubCommand","","The abstract representation of a command line subcommand used by the consumer of the library.",null,null],[12,"name","","",0,null],[12,"matches","","",0,null],[3,"ArgMatches","","Used to get information about the arguments that where supplied to the program at runtime by\nthe user. To get a new instance of this struct you use `.get_matches()` of the `App` struct.",null,null],[3,"App","","Used to create a representation of the program and all possible command line arguments\nfor parsing at runtime.",null,null],[11,"new","","Creates a new instance of an application requiring a name (such as the binary). Will be displayed\nto the user when they print version or help and usage information.",1,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"author","","Sets a string of author(s) and will be showed to the user when displaying help inofrmation",1,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"about","","Sets a string briefly describing what the program does and will be displayed when\ndisplaying help information.",1,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"version","","Sets a string of the version number to be displayed when displaying version or help\ninformation.",1,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"usage","","Sets a custom usage string to over-ride the one auto-generated by `clap`. Will be\ndisplayed to the user when errors are found in argument parsing, or when you call\n`.usage()` of `ArgMatches`",1,{"inputs":[{"name":"app"},{"name":"str"}],"output":{"name":"app"}}],[11,"arg","","Adds an argument to the list of valid possibilties",1,{"inputs":[{"name":"app"},{"name":"arg"}],"output":{"name":"app"}}],[11,"args","","Adds multiple arguments to the list of valid possibilties",1,{"inputs":[{"name":"app"},{"name":"vec"}],"output":{"name":"app"}}],[11,"subcommand","","Adds a subcommand to the list of valid possibilties. Subcommands are effectively sub apps,\nbecause they can contain their own arguments, subcommands, version, usage, etc. They also\nfunction just like apps, in that they get their own auto generated help and version\nswitches.",1,{"inputs":[{"name":"app"},{"name":"app"}],"output":{"name":"app"}}],[11,"subcommands","","Adds multiple subcommands to the list of valid possibilties",1,{"inputs":[{"name":"app"},{"name":"vec"}],"output":{"name":"app"}}],[11,"get_matches","","",1,{"inputs":[{"name":"app"}],"output":{"name":"argmatches"}}],[11,"new","","Creates a new instace of `Arg` using a unique string name.\nThe name will be used by the library consumer to get information about\nwhether or not the argument was used at runtime. ",2,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"short","","Sets the short version of the argument without the preceding `-`.",2,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"long","","Sets the long version of the argument without the preceding `--`.",2,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"help","","Sets the help text of the argument that will be displayed to the user\nwhen they print the usage/help information. ",2,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"required","","Sets whether or not the argument is required by default. Required by\ndefault means it is required, when no other mutually exlusive rules have\nbeen evaluated. Mutually exclusive rules take precedence over being required\nby default.",2,{"inputs":[{"name":"arg"},{"name":"bool"}],"output":{"name":"arg"}}],[11,"mutually_excludes","","Sets a mutually exclusive argument by name. I.e. when using this argument,\nthe following argument can't be present.",2,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"mutually_excludes_all","","Sets a mutually exclusive arguments by names. I.e. when using this argument,\nthe following argument can't be present.",2,{"inputs":[{"name":"arg"},{"name":"vec"}],"output":{"name":"arg"}}],[11,"requires","","Sets an argument by name that is required when this one is presnet I.e. when\nusing this argument, the following argument *must* be present.",2,{"inputs":[{"name":"arg"},{"name":"str"}],"output":{"name":"arg"}}],[11,"requires_all","","Sets arguments by names that are required when this one is presnet I.e. when\nusing this argument, the following arguments *must* be present.",2,{"inputs":[{"name":"arg"},{"name":"vec"}],"output":{"name":"arg"}}],[11,"takes_value","","Specifies that the argument takes an additional value at run time.",2,{"inputs":[{"name":"arg"},{"name":"bool"}],"output":{"name":"arg"}}],[11,"index","","Specifies the index of a positional argument starting at 1.",2,{"inputs":[{"name":"arg"},{"name":"u8"}],"output":{"name":"arg"}}],[11,"multiple","","Specifies if the flag may appear more than once such as for multiple debugging\nlevels (as an example). `-ddd` for three levels of debugging, or `-d -d -d`. \nWhen this is set to `true` you recieve the number of occurances the user supplied\nof a particular flag at runtime.",2,{"inputs":[{"name":"arg"},{"name":"bool"}],"output":{"name":"arg"}}],[11,"possible_values","","Specifies a list of possible values for this argument. At runtime, clap verifies that only\none of the specified values was used, or fails with a usage string.",2,{"inputs":[{"name":"arg"},{"name":"vec"}],"output":{"name":"arg"}}],[11,"value_of","","Gets the value of a specific option or positional argument (i.e. an argument that takes\nan additional value at runtime). If the option wasn't present at runtime\nit returns `None`. ",3,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"option"}}],[11,"values_of","","Gets the values of a specific option or positional argument in a vector (i.e. an argument\nthat takes multiple values at runtime). If the option wasn't present at runtime it\nreturns `None`",3,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"option"}}],[11,"is_present","","Returns if an argument was present at runtime.",3,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"bool"}}],[11,"occurrences_of","","Returns the number of occurrences of an option, flag, or positional argument at runtime.\nIf an argument isn't present it will return `0`. Can be used on arguments which *don't*\nallow multiple occurrences, but will obviously only return `0` or `1`.",3,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"u8"}}],[11,"subcommand_matches","","Returns the `ArgMatches` for a particular subcommand or None if the subcommand wasn't\npresent at runtime.",3,{"inputs":[{"name":"argmatches"},{"name":"str"}],"output":{"name":"option"}}],[11,"subcommand_name","","Returns the name of the subcommand used of the parent `App`, or `None` if one wasn't found",3,{"inputs":[{"name":"argmatches"}],"output":{"name":"option"}}],[11,"subcommand","","Returns the name and `ArgMatches` of the subcommand used at runtime or (\"\", None) if one\nwasn't found.",3,null],[11,"usage","","Returns a string slice of the usage statement for the `App` (or `SubCommand`)",3,{"inputs":[{"name":"argmatches"}],"output":{"name":"str"}}],[11,"new","","Creates a new instance of a subcommand requiring a name. Will be displayed\nto the user when they print version or help and usage information.",0,{"inputs":[{"name":"subcommand"},{"name":"str"}],"output":{"name":"app"}}]],"paths":[[3,"SubCommand"],[3,"App"],[3,"Arg"],[3,"ArgMatches"]]}; initSearch(searchIndex); diff --git a/docs/src/clap/app.rs.html b/docs/src/clap/app.rs.html index 31807059..a11f08e8 100644 --- a/docs/src/clap/app.rs.html +++ b/docs/src/clap/app.rs.html @@ -1285,6 +1285,22 @@ 1243 1244 1245 +1246 +1247 +1248 +1249 +1250 +1251 +1252 +1253 +1254 +1255 +1256 +1257 +1258 +1259 +1260 +1261
use std::collections::BTreeMap; use std::collections::BTreeSet; @@ -1390,7 +1406,7 @@ } } - /// Sets a string of author(s) + /// Sets a string of author(s) and will be showed to the user when displaying help inofrmation /// /// # Example /// @@ -1405,7 +1421,8 @@ self } - /// Sets a string briefly describing what the program does + /// Sets a string briefly describing what the program does and will be displayed when + /// displaying help information. /// /// # Example /// @@ -1420,7 +1437,8 @@ self } - /// Sets a string of the version number + /// Sets a string of the version number to be displayed when displaying version or help + /// information. /// /// # Example /// @@ -1435,13 +1453,15 @@ self } - /// Sets a custom usage string to over-ride the one auto-generated by `clap` + /// Sets a custom usage string to over-ride the one auto-generated by `clap`. Will be + /// displayed to the user when errors are found in argument parsing, or when you call + /// `.usage()` of `ArgMatches` /// - /// *NOTE:* You do not need to specify the "USAGE: " portion, as that will + /// *NOTE:* You do not need to specify the "USAGE: \n\t" portion, as that will /// still be applied by `clap`, you only need to specify the portion starting /// with the binary name. /// - /// *NOTE:* This will not replace the entire help message, only the portion + /// *NOTE:* This will not replace the entire help message, *only* the portion /// showing the usage. /// /// # Example @@ -1644,10 +1664,10 @@ self } - /// Adds a subcommand to the list of valid possibilties. Subcommands - /// are effectively sub apps, because they can contain their own arguments - /// and subcommands. They also function just like apps, in that they get their - /// own auto generated help and version switches. + /// Adds a subcommand to the list of valid possibilties. Subcommands are effectively sub apps, + /// because they can contain their own arguments, subcommands, version, usage, etc. They also + /// function just like apps, in that they get their own auto generated help and version + /// switches. /// /// # Example /// @@ -1688,6 +1708,9 @@ self } + // Creates a usage string if one was not provided by the user manually. This happens just + // after all arguments were parsed, but before any subcommands have been parsed (so as to + // give subcommands their own usage recursively) fn create_usage(&self) -> String { let tab = " "; let mut usage = String::with_capacity(75); @@ -1776,6 +1799,7 @@ usage } + // Prints the usage statement to the user fn print_usage(&self, more_info: bool) { print!("{}",self.create_usage()); if more_info { @@ -1783,6 +1807,7 @@ } } + // Prints the full help message to the user fn print_help(&self) { self.print_version(false); let flags = !self.flags.is_empty(); @@ -1907,6 +1932,7 @@ self.exit(0); } + // Used when spacing arguments and their help message when displaying help information #[inline(always)] fn get_spaces(&self, num: usize) -> &'static str { match num { @@ -1944,6 +1970,7 @@ } } + // Prints the version to the user and exits if quit=true fn print_version(&self, quit: bool) { // Print the binary name if existing, but replace all spaces with hyphens in case we're // dealing with subcommands i.e. git mv is translated to git-mv @@ -1951,16 +1978,21 @@ if quit { self.exit(0); } } + // Exits with a status code passed to the OS + // This is legacy from before std::process::exit() and may be removed evenutally fn exit(&self, status: i32) { process::exit(status); } + // Reports and error to the users screen along with an optional usage statement and quits fn report_error(&self, msg: String, usage: bool, quit: bool) { println!("{}", msg); if usage { self.print_usage(true); } if quit { self.exit(1); } } + // Starts the parsing process. Called on top level parent app **ONLY** then recursively calls + // the real parsing function for subcommands pub fn get_matches(mut self) -> ArgMatches<'ar> { self.verify_positionals(); for (_,sc) in self.subcommands.iter_mut() { @@ -1983,7 +2015,7 @@ matches } - + fn verify_positionals(&mut self) { // Because you must wait until all arguments have been supplied, this is the first chance // to make assertions on positional argument indexes diff --git a/docs/src/clap/args/arg.rs.html b/docs/src/clap/args/arg.rs.html index 54d841a2..723fcfd0 100644 --- a/docs/src/clap/args/arg.rs.html +++ b/docs/src/clap/args/arg.rs.html @@ -400,14 +400,25 @@ 358 359 360 +361 +362 +363 +364 +365 +366 +367 +368 +369 +370 +371
/// The abstract representation of a command line argument used by the consumer of the library. +/// Used to set all the options and relationships that define a valid argument for the program. /// /// /// This struct is used by the library consumer and describes the command line arguments for -/// their program. -/// and then evaluates the settings the consumer provided and determines the concret -/// argument struct to use when parsing. +/// their program. Then evaluates the settings the consumer provided and determines the concret +/// argument type to use when parsing. /// /// # Example /// @@ -423,37 +434,48 @@ /// # ).get_matches(); pub struct Arg<'n, 'l, 'h, 'b, 'p, 'r> { /// The unique name of the argument, required + #[doc(hidden)] pub name: &'n str, /// The short version (i.e. single character) of the argument, no preceding `-` /// **NOTE:** `short` is mutually exclusive with `index` + #[doc(hidden)] pub short: Option<char>, /// The long version of the flag (i.e. word) without the preceding `--` /// **NOTE:** `long` is mutually exclusive with `index` + #[doc(hidden)] pub long: Option<&'l str>, /// The string of text that will displayed to the user when the application's /// `help` text is displayed + #[doc(hidden)] pub help: Option<&'h str>, /// If this is a required by default when using the command line program /// i.e. a configuration file that's required for the program to function /// **NOTE:** required by default means, it is required *until* mutually /// exclusive arguments are evaluated. + #[doc(hidden)] pub required: bool, /// Determines if this argument is an option, vice a flag or positional and /// is mutually exclusive with `index` and `multiple` + #[doc(hidden)] pub takes_value: bool, /// The index of the argument. `index` is mutually exclusive with `takes_value` /// and `multiple` + #[doc(hidden)] pub index: Option<u8>, /// Determines if multiple instances of the same flag are allowed. `multiple` /// is mutually exclusive with `index` and `takes_value`. /// I.e. `-v -v -v` or `-vvv` + #[doc(hidden)] pub multiple: bool, /// A list of names for other arguments that *may not* be used with this flag + #[doc(hidden)] pub blacklist: Option<Vec<&'b str>>, /// A list of possible values for an option or positional argument + #[doc(hidden)] pub possible_vals: Option<Vec<&'p str>>, /// A list of names of other arguments that are *required* to be used when /// this flag is used + #[doc(hidden)] pub requires: Option<Vec<&'r str>> } diff --git a/docs/src/clap/args/argmatches.rs.html b/docs/src/clap/args/argmatches.rs.html index 91af7c8d..5e8e86bb 100644 --- a/docs/src/clap/args/argmatches.rs.html +++ b/docs/src/clap/args/argmatches.rs.html @@ -327,6 +327,9 @@ 285 286 287 +288 +289 +290
use std::collections::HashMap; @@ -335,13 +338,10 @@ use args::optarg::OptArg; use args::posarg::PosArg; -/// Used to get information about the arguments that -/// where supplied to the program at runtime. +/// Used to get information about the arguments that where supplied to the program at runtime by +/// the user. To get a new instance of this struct you use `.get_matches()` of the `App` struct. /// /// -/// Fields of `ArgMatches` aren't designed to be used directly, only -/// the methods in order to query information. -/// /// # Example /// /// ```no_run @@ -361,19 +361,17 @@ /// println!("Value for output: {}", o); /// } /// -/// // Although not advised, if you have a required argument -/// // you can call .unwrap() because the program will exit long before -/// // here at noticing the user didn't supply a required argument... -/// // use at your own risk ;) +/// // If you have a required argument you can call .unwrap() because the program will exit long +/// // before this point if the user didn't specify it at runtime. /// println!("Config file: {}", matches.value_of("config").unwrap()); /// -/// // You can check the present of an argument +/// // You can check the presence of an argument /// if matches.is_present("debug") { -/// // Checking if "debug" was present was necessary, -/// // as occurrences returns 0 if a flag isn't found -/// // but we can check how many times "debug" was found -/// // if we allow multiple (if multiple isn't allowed it always be 1 or 0) -/// if matches.occurrences_of("debug") > 1 { +/// // Another way to check if an argument was present, or if it occurred multiple times is to +/// // use occurrences_of() which returns 0 if an argument isn't found at runtime, or the +/// // number of times that it occurred, if it was. To allow an argument to appear more than +/// // once, you must use the .multiple(true) method, otherwise it will only return 1 or 0. +/// if matches.occurrences_of("debug") > 2 { /// println!("Debug mode is REALLY on"); /// } else { /// println!("Debug mode kind of on"); @@ -390,11 +388,15 @@ /// } /// } pub struct ArgMatches<'a> { - // pub matches_of: &'static str, + #[doc(hidden)] pub flags: HashMap<&'a str, FlagArg>, + #[doc(hidden)] pub opts: HashMap<&'a str, OptArg>, + #[doc(hidden)] pub positionals: HashMap<&'a str, PosArg>, + #[doc(hidden)] pub subcommand: Option<Box<SubCommand<'a>>>, + #[doc(hidden)] pub usage: Option<String> } @@ -408,6 +410,7 @@ /// # use clap::{App, Arg}; /// let matches = App::new("myprog").get_matches(); /// ``` + #[doc(hidden)] pub fn new() -> ArgMatches<'a> { ArgMatches { // matches_of: name, @@ -450,7 +453,7 @@ } /// Gets the values of a specific option or positional argument in a vector (i.e. an argument - /// that takes an additional value at runtime). If the option wasn't present at runtime it + /// that takes multiple values at runtime). If the option wasn't present at runtime it /// returns `None` /// /// # Example @@ -481,8 +484,7 @@ None } - /// Checks if a flag was argument was supplied at runtime. **DOES NOT** work for - /// option or positional arguments (use `.value_of()` instead) + /// Returns if an argument was present at runtime. /// /// /// # Example @@ -504,10 +506,9 @@ false } - /// Checks the number of occurrences of an option, flag, or positional argument at runtime. - /// If an option or flag isn't present it will return `0`, if the option or flag doesn't - /// allow multiple occurrences, it will return `1` no matter how many times it occurred - /// (unless it wasn't prsent) at all. + /// Returns the number of occurrences of an option, flag, or positional argument at runtime. + /// If an argument isn't present it will return `0`. Can be used on arguments which *don't* + /// allow multiple occurrences, but will obviously only return `0` or `1`. /// /// /// # Example @@ -534,7 +535,8 @@ 0 } - /// If a subcommand was found, returns the ArgMatches struct associated with it's matches + /// Returns the `ArgMatches` for a particular subcommand or None if the subcommand wasn't + /// present at runtime. /// /// /// # Example @@ -554,7 +556,10 @@ None } - /// If a subcommand was found, returns the name associated with it + /// Returns the name of the subcommand used of the parent `App`, or `None` if one wasn't found + /// + /// *NOTE*: Only a single subcommand may be present per `App` at runtime, does *NOT* check for + /// the name of sub-subcommand's names /// /// /// # Example @@ -575,7 +580,8 @@ None } - /// If a subcommand was found, returns the name and matches associated with it + /// Returns the name and `ArgMatches` of the subcommand used at runtime or ("", None) if one + /// wasn't found. /// /// /// # Example @@ -596,7 +602,7 @@ ("", None) } - /// Returns a slice of the usage + /// Returns a string slice of the usage statement for the `App` (or `SubCommand`) /// /// /// # Example diff --git a/src/app.rs b/src/app.rs index 4107677e..3650c472 100644 --- a/src/app.rs +++ b/src/app.rs @@ -102,7 +102,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ } } - /// Sets a string of author(s) + /// Sets a string of author(s) and will be showed to the user when displaying help inofrmation /// /// # Example /// @@ -117,7 +117,8 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ self } - /// Sets a string briefly describing what the program does + /// Sets a string briefly describing what the program does and will be displayed when + /// displaying help information. /// /// # Example /// @@ -132,7 +133,8 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ self } - /// Sets a string of the version number + /// Sets a string of the version number to be displayed when displaying version or help + /// information. /// /// # Example /// @@ -147,13 +149,15 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ self } - /// Sets a custom usage string to over-ride the one auto-generated by `clap` + /// Sets a custom usage string to over-ride the one auto-generated by `clap`. Will be + /// displayed to the user when errors are found in argument parsing, or when you call + /// `.usage()` of `ArgMatches` /// - /// *NOTE:* You do not need to specify the "USAGE: " portion, as that will + /// *NOTE:* You do not need to specify the "USAGE: \n\t" portion, as that will /// still be applied by `clap`, you only need to specify the portion starting /// with the binary name. /// - /// *NOTE:* This will not replace the entire help message, only the portion + /// *NOTE:* This will not replace the entire help message, *only* the portion /// showing the usage. /// /// # Example @@ -356,10 +360,10 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ self } - /// Adds a subcommand to the list of valid possibilties. Subcommands - /// are effectively sub apps, because they can contain their own arguments - /// and subcommands. They also function just like apps, in that they get their - /// own auto generated help and version switches. + /// Adds a subcommand to the list of valid possibilties. Subcommands are effectively sub apps, + /// because they can contain their own arguments, subcommands, version, usage, etc. They also + /// function just like apps, in that they get their own auto generated help and version + /// switches. /// /// # Example /// @@ -400,6 +404,9 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ self } + // Creates a usage string if one was not provided by the user manually. This happens just + // after all arguments were parsed, but before any subcommands have been parsed (so as to + // give subcommands their own usage recursively) fn create_usage(&self) -> String { let tab = " "; let mut usage = String::with_capacity(75); @@ -488,6 +495,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ usage } + // Prints the usage statement to the user fn print_usage(&self, more_info: bool) { print!("{}",self.create_usage()); if more_info { @@ -495,6 +503,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ } } + // Prints the full help message to the user fn print_help(&self) { self.print_version(false); let flags = !self.flags.is_empty(); @@ -619,6 +628,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ self.exit(0); } + // Used when spacing arguments and their help message when displaying help information #[inline(always)] fn get_spaces(&self, num: usize) -> &'static str { match num { @@ -656,6 +666,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ } } + // Prints the version to the user and exits if quit=true fn print_version(&self, quit: bool) { // Print the binary name if existing, but replace all spaces with hyphens in case we're // dealing with subcommands i.e. git mv is translated to git-mv @@ -663,16 +674,21 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ if quit { self.exit(0); } } + // Exits with a status code passed to the OS + // This is legacy from before std::process::exit() and may be removed evenutally fn exit(&self, status: i32) { process::exit(status); } + // Reports and error to the users screen along with an optional usage statement and quits fn report_error(&self, msg: String, usage: bool, quit: bool) { println!("{}", msg); if usage { self.print_usage(true); } if quit { self.exit(1); } } + // Starts the parsing process. Called on top level parent app **ONLY** then recursively calls + // the real parsing function for subcommands pub fn get_matches(mut self) -> ArgMatches<'ar> { self.verify_positionals(); for (_,sc) in self.subcommands.iter_mut() { @@ -695,7 +711,7 @@ impl<'a, 'v, 'ab, 'u, 'ar> App<'a, 'v, 'ab, 'u, 'ar>{ matches } - + fn verify_positionals(&mut self) { // Because you must wait until all arguments have been supplied, this is the first chance // to make assertions on positional argument indexes diff --git a/src/args/arg.rs b/src/args/arg.rs index 3c51054b..57547e0c 100644 --- a/src/args/arg.rs +++ b/src/args/arg.rs @@ -1,10 +1,10 @@ /// The abstract representation of a command line argument used by the consumer of the library. +/// Used to set all the options and relationships that define a valid argument for the program. /// /// /// This struct is used by the library consumer and describes the command line arguments for -/// their program. -/// and then evaluates the settings the consumer provided and determines the concret -/// argument struct to use when parsing. +/// their program. Then evaluates the settings the consumer provided and determines the concret +/// argument type to use when parsing. /// /// # Example /// @@ -20,37 +20,48 @@ /// # ).get_matches(); pub struct Arg<'n, 'l, 'h, 'b, 'p, 'r> { /// The unique name of the argument, required + #[doc(hidden)] pub name: &'n str, /// The short version (i.e. single character) of the argument, no preceding `-` /// **NOTE:** `short` is mutually exclusive with `index` + #[doc(hidden)] pub short: Option, /// The long version of the flag (i.e. word) without the preceding `--` /// **NOTE:** `long` is mutually exclusive with `index` + #[doc(hidden)] pub long: Option<&'l str>, /// The string of text that will displayed to the user when the application's /// `help` text is displayed + #[doc(hidden)] pub help: Option<&'h str>, /// If this is a required by default when using the command line program /// i.e. a configuration file that's required for the program to function /// **NOTE:** required by default means, it is required *until* mutually /// exclusive arguments are evaluated. + #[doc(hidden)] pub required: bool, /// Determines if this argument is an option, vice a flag or positional and /// is mutually exclusive with `index` and `multiple` + #[doc(hidden)] pub takes_value: bool, /// The index of the argument. `index` is mutually exclusive with `takes_value` /// and `multiple` + #[doc(hidden)] pub index: Option , /// Determines if multiple instances of the same flag are allowed. `multiple` /// is mutually exclusive with `index` and `takes_value`. /// I.e. `-v -v -v` or `-vvv` + #[doc(hidden)] pub multiple: bool, /// A list of names for other arguments that *may not* be used with this flag + #[doc(hidden)] pub blacklist: Option >, /// A list of possible values for an option or positional argument + #[doc(hidden)] pub possible_vals: Option >, /// A list of names of other arguments that are *required* to be used when /// this flag is used + #[doc(hidden)] pub requires: Option > } diff --git a/src/args/argmatches.rs b/src/args/argmatches.rs index 58e49861..37660ea1 100644 --- a/src/args/argmatches.rs +++ b/src/args/argmatches.rs @@ -5,13 +5,10 @@ use args::flagarg::FlagArg; use args::optarg::OptArg; use args::posarg::PosArg; -/// Used to get information about the arguments that -/// where supplied to the program at runtime. +/// Used to get information about the arguments that where supplied to the program at runtime by +/// the user. To get a new instance of this struct you use `.get_matches()` of the `App` struct. /// /// -/// Fields of `ArgMatches` aren't designed to be used directly, only -/// the methods in order to query information. -/// /// # Example /// /// ```no_run @@ -31,19 +28,17 @@ use args::posarg::PosArg; /// println!("Value for output: {}", o); /// } /// -/// // Although not advised, if you have a required argument -/// // you can call .unwrap() because the program will exit long before -/// // here at noticing the user didn't supply a required argument... -/// // use at your own risk ;) +/// // If you have a required argument you can call .unwrap() because the program will exit long +/// // before this point if the user didn't specify it at runtime. /// println!("Config file: {}", matches.value_of("config").unwrap()); /// -/// // You can check the present of an argument +/// // You can check the presence of an argument /// if matches.is_present("debug") { -/// // Checking if "debug" was present was necessary, -/// // as occurrences returns 0 if a flag isn't found -/// // but we can check how many times "debug" was found -/// // if we allow multiple (if multiple isn't allowed it always be 1 or 0) -/// if matches.occurrences_of("debug") > 1 { +/// // Another way to check if an argument was present, or if it occurred multiple times is to +/// // use occurrences_of() which returns 0 if an argument isn't found at runtime, or the +/// // number of times that it occurred, if it was. To allow an argument to appear more than +/// // once, you must use the .multiple(true) method, otherwise it will only return 1 or 0. +/// if matches.occurrences_of("debug") > 2 { /// println!("Debug mode is REALLY on"); /// } else { /// println!("Debug mode kind of on"); @@ -60,11 +55,15 @@ use args::posarg::PosArg; /// } /// } pub struct ArgMatches<'a> { - // pub matches_of: &'static str, + #[doc(hidden)] pub flags: HashMap<&'a str, FlagArg>, + #[doc(hidden)] pub opts: HashMap<&'a str, OptArg>, + #[doc(hidden)] pub positionals: HashMap<&'a str, PosArg>, + #[doc(hidden)] pub subcommand: Option >>, + #[doc(hidden)] pub usage: Option } @@ -78,6 +77,7 @@ impl<'a> ArgMatches<'a> { /// # use clap::{App, Arg}; /// let matches = App::new("myprog").get_matches(); /// ``` + #[doc(hidden)] pub fn new() -> ArgMatches<'a> { ArgMatches { // matches_of: name, @@ -120,7 +120,7 @@ impl<'a> ArgMatches<'a> { } /// Gets the values of a specific option or positional argument in a vector (i.e. an argument - /// that takes an additional value at runtime). If the option wasn't present at runtime it + /// that takes multiple values at runtime). If the option wasn't present at runtime it /// returns `None` /// /// # Example @@ -151,8 +151,7 @@ impl<'a> ArgMatches<'a> { None } - /// Checks if a flag was argument was supplied at runtime. **DOES NOT** work for - /// option or positional arguments (use `.value_of()` instead) + /// Returns if an argument was present at runtime. /// /// /// # Example @@ -174,10 +173,9 @@ impl<'a> ArgMatches<'a> { false } - /// Checks the number of occurrences of an option, flag, or positional argument at runtime. - /// If an option or flag isn't present it will return `0`, if the option or flag doesn't - /// allow multiple occurrences, it will return `1` no matter how many times it occurred - /// (unless it wasn't prsent) at all. + /// Returns the number of occurrences of an option, flag, or positional argument at runtime. + /// If an argument isn't present it will return `0`. Can be used on arguments which *don't* + /// allow multiple occurrences, but will obviously only return `0` or `1`. /// /// /// # Example @@ -204,7 +202,8 @@ impl<'a> ArgMatches<'a> { 0 } - /// If a subcommand was found, returns the ArgMatches struct associated with it's matches + /// Returns the `ArgMatches` for a particular subcommand or None if the subcommand wasn't + /// present at runtime. /// /// /// # Example @@ -224,7 +223,10 @@ impl<'a> ArgMatches<'a> { None } - /// If a subcommand was found, returns the name associated with it + /// Returns the name of the subcommand used of the parent `App`, or `None` if one wasn't found + /// + /// *NOTE*: Only a single subcommand may be present per `App` at runtime, does *NOT* check for + /// the name of sub-subcommand's names /// /// /// # Example @@ -245,7 +247,8 @@ impl<'a> ArgMatches<'a> { None } - /// If a subcommand was found, returns the name and matches associated with it + /// Returns the name and `ArgMatches` of the subcommand used at runtime or ("", None) if one + /// wasn't found. /// /// /// # Example @@ -266,7 +269,7 @@ impl<'a> ArgMatches<'a> { ("", None) } - /// Returns a slice of the usage + /// Returns a string slice of the usage statement for the `App` (or `SubCommand`) /// /// /// # Example