diff --git a/docs/clap/app/sidebar-items.js b/docs/clap/app/sidebar-items.js index 0143df19..5bec2122 100644 --- a/docs/clap/app/sidebar-items.js +++ b/docs/clap/app/sidebar-items.js @@ -1 +1 @@ -initSidebarItems({"struct":[["App","Used to create a representation of the program and all possible command line arguments for parsing at runtime."]]}); \ No newline at end of file +initSidebarItems({"struct":[["App","Used to create a representation of the program and all possible command line arguments for parsing at runtime."]]}); \ No newline at end of file diff --git a/docs/clap/index.html b/docs/clap/index.html index 4131e797..fd6e0208 100644 --- a/docs/clap/index.html +++ b/docs/clap/index.html @@ -45,95 +45,95 @@ [-] [+] [src]

A simply library for parsing command line arguments when writing - command line and console applications.

+command line and console applications.

You can use clap to lay out a list of possible valid command line arguments and let clap parse the string given by the user at runtime. - When using clap you define a set of parameters and rules for your arguments and at runtime clap will determine their validity. - Also, clap provides the traditional version and help switches 'for free' by parsing the list of possible valid arguments lazily at runtime. - i.e. only when it's been determined that the user wants or needs to see the help and version information.

+When using clap you define a set of parameters and rules for your arguments and at runtime clap will determine their validity. +Also, clap provides the traditional version and help switches 'for free' by parsing the list of possible valid arguments lazily at runtime. +i.e. only when it's been determined that the user wants or needs to see the help and version information.

After defining a list of possible valid arguments you get a list of matches that the user supplied at runtime. You can then use this list to - determine the functioning of your program.

+determine the functioning of your program.

-

# Example

-
- use clap::{Arg, App, SubCommand};
+

Example

+use clap::{Arg, App, SubCommand};
 
- // ...
+// ...
  
- 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")
+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();
+                                   .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") {
+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"),
- }
+       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 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...");
-        }
-    }
+           println!("Printing verbose test info...");
+       } else {
+           println!("Not printing regular test info...");
+       }
+   }
 
- // more porgram logic goes here...
+// more porgram logic goes here...
 

If you were to compile the above program and run it with the flag --help or -h the following output woud be presented

-
 $ myprog --help
- MyApp 1.0
- Kevin K. <kbknapp@gmail.com>
- Does awesome things
+
$ myprog --help
+MyApp 1.0
+Kevin K. <kbknapp@gmail.com>
+Does awesome things
  
- USAGE:
+USAGE:
     MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
  
- FLAGS:
-    -d              Turn debugging information on
-    -h,--help       Prints this message
-    -v,--version    Prints version information
+FLAGS:
+    -d               Turn debugging information on
+    -h,--help        Prints this message
+    -v,--version     Prints version information
  
- OPTIONS:
+OPTIONS:
     -c,--config <config>        Sets a custom config file
 
- POSITIONAL ARGUMENTS:
-    output          Sets an optional output file
+POSITIONAL ARGUMENTS:
+    output            Sets an optional output file
 
- SUBCOMMANDS:
-    help            Prints this message
-        test            Has test sub-functionality
+SUBCOMMANDS:
+    help             Prints this message
+    test             Has test sub-functionality
 

Structs

@@ -141,7 +141,7 @@ diff --git a/docs/clap/sidebar-items.js b/docs/clap/sidebar-items.js index 0715b4d6..68fecddc 100644 --- a/docs/clap/sidebar-items.js +++ b/docs/clap/sidebar-items.js @@ -1 +1 @@ -initSidebarItems({"struct":[["App","Used to create a representation of the program and all possible command line arguments for parsing at runtime."],["Arg","The abstract representation of a command line argument used by the consumer of the library. "],["ArgMatches","Used to get information about the arguments that where supplied to the program at runtime."],["SubCommand","The abstract representation of a command line subcommand used by the consumer of the library. "]]}); \ No newline at end of file +initSidebarItems({"struct":[["App","Used to create a representation of the program and all possible command line arguments for parsing at runtime."],["Arg","The abstract representation of a command line argument used by the consumer of the library. "],["ArgMatches","Used to get information about the arguments that where supplied to the program at runtime."],["SubCommand","The abstract representation of a command line subcommand used by the consumer of the library. "]]}); \ No newline at end of file diff --git a/docs/clap/struct.App.html b/docs/clap/struct.App.html index 45ea88ca..19958b2c 100644 --- a/docs/clap/struct.App.html +++ b/docs/clap/struct.App.html @@ -51,24 +51,24 @@ pub about: Option<&'static str>, // some fields omitted }

Used to create a representation of the program and all possible command line arguments - for parsing at runtime.

+for parsing at runtime.

Stores a list of all posisble arguments, as well as information displayed to the user such as - help and versioning information.

+help and versioning information.

-

# Example

-
- let myprog = App::new("myprog")
-                   .author("Me, me@mail.com")
-                      .version("1.0.2")
-                   .about("Explains in brief what the program does")
-                   .arg(
-                            Arg::new("in_file").index(1)
-                        // Add other possible command line argument options here...
-                    )
-                   .get_matches();
+

Example

+let myprog = App::new("myprog")
+                  .author("Me, me@mail.com")
+                     .version("1.0.2")
+                  .about("Explains in brief what the program does")
+                  .arg(
+                           Arg::new("in_file").index(1)
+                       // Add other possible command line argument options here...
+                   )
+                  .get_matches();
 
- // Your pogram logic starts here...
+// Your pogram logic starts here...
 

Fields

App

Used to create a representation of the program and all possible command line arguments - for parsing at runtime.

+for parsing at runtime.

name

The name displayed to the user when showing version and help/usage information

@@ -119,46 +119,46 @@ showing the usage.

fn arg(self, a: Arg) -> App

Adds an argument to the list of valid possibilties

-

# Example

-
- .arg(Arg::new("config")
-                .short("c")
+

Example

+.arg(Arg::new("config")
+               .short("c")
             // Additional argument configuration goes here...
- )
+)
 

fn args(self, args: Vec<Arg>) -> App

Adds multiple arguments to the list of valid possibilties

-

# Example

-
- .args( vec![Arg::new("config").short("c"),
-                Arg::new("debug").short("d")])
+

Example

+.args( vec![Arg::new("config").short("c"),
+               Arg::new("debug").short("d")])
 

fn subcommand(self, subcmd: App) -> App

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.

+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.

-

# Example

-
- .subcommand(SubCommand::new("config")
-                .about("Controls configuration features")
-                .arg(Arg::new("config_file")
-                        .index(1)
-                        .help("Configuration file to use")))
+

Example

+.subcommand(SubCommand::new("config")
+               .about("Controls configuration features")
+               .arg(Arg::new("config_file")
+                       .index(1)
+                       .help("Configuration file to use")))
             // Additional subcommand configuration goes here, such as arguments...
- )
+)
 

fn subcommands(self, subcmds: Vec<App>) -> App

Adds multiple subcommands to the list of valid possibilties

-

# Example

-
- .subcommands( vec![
-        SubCommand::new("config").about("Controls configuration functionality")
-                                 .arg(Arg::new("config_file").index(1)),
-        SubCommand::new("debug").about("Controls debug functionality")])
+

Example

+.subcommands( vec![
+       SubCommand::new("config").about("Controls configuration functionality")
+                                .arg(Arg::new("config_file").index(1)),
+       SubCommand::new("debug").about("Controls debug functionality")])
 

fn get_matches(self) -> ArgMatches

diff --git a/docs/clap/struct.Arg.html b/docs/clap/struct.Arg.html index d345f105..5eabb9cf 100644 --- a/docs/clap/struct.Arg.html +++ b/docs/clap/struct.Arg.html @@ -170,16 +170,16 @@ arguments, they do not need to be set for each.

fn mutually_excludes_all(self, names: Vec<&'static str>) -> Arg

Sets a mutually exclusive arguments by names. I.e. when using this argument, - the following argument can't be present.

+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.

+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:

- .mutually_excludes_all(
-        vec!["debug", "input"])
+.mutually_excludes_all(
+       vec!["debug", "input"])
 

fn requires(self, name: &'static str) -> Arg

Sets an argument by name that is required when this one is presnet I.e. when @@ -193,15 +193,15 @@ using this argument, the following argument must be present.

fn requires_all(self, names: Vec<&'static str>) -> Arg

Sets arguments by names that are required when this one is presnet I.e. when - using this argument, the following arguments must be present.

+using this argument, the following arguments must be present.

NOTE: Mutually exclusive rules take precedence over being required - by default.

+by default.

Example:

- .requires_all(
-        vec!["debug", "input"])
+.requires_all(
+       vec!["debug", "input"])
 

fn takes_value(self, tv: bool) -> Arg

Specifies that the argument takes an additional value at run time.

diff --git a/docs/clap/struct.SubCommand.html b/docs/clap/struct.SubCommand.html index c267ff68..7f917f56 100644 --- a/docs/clap/struct.SubCommand.html +++ b/docs/clap/struct.SubCommand.html @@ -50,16 +50,16 @@ }

The abstract representation of a command line subcommand used by the consumer of the library.

This struct is used by the library consumer and describes all the valid options of the subcommand for - their program. SubCommands are treated like "sub apps" and contain all the same possibilities (such as - their own arguments and subcommands).

+their program. SubCommands are treated like "sub apps" and contain all the same possibilities (such as +their own arguments and subcommands).

-

# Example

-
- SubCommand::new("conifg")
-                .about("Used for configuration")
-                .arg(Arg::new("config_file")
-                           .help("The configuration file to use")
-                           .index(1))
+

Example

+SubCommand::new("conifg")
+               .about("Used for configuration")
+               .arg(Arg::new("config_file")
+                          .help("The configuration file to use")
+                          .index(1))
 

Fields

name
matches

Methods

impl SubCommand

fn new(name: &'static str) -> App

diff --git a/docs/search-index.js b/docs/search-index.js index 180bb2bd..d719f2de 100644 --- a/docs/search-index.js +++ b/docs/search-index.js @@ -1,3 +1,3 @@ var searchIndex = {}; -searchIndex['clap'] = {"items":[[0,"","clap","A simply library for parsing command line arguments when writing\n command line and console applications."],[3,"ArgMatches","","Used to get information about the arguments that\nwhere supplied to the program at runtime."],[12,"matches_of","","",0],[12,"flags","","",0],[12,"opts","","",0],[12,"positionals","","",0],[12,"subcommand","","",0],[3,"Arg","","The abstract representation of a command line argument used by the consumer of the library.\n "],[12,"name","","The unique name of the argument, required",1],[12,"short","","The short version (i.e. single character) of the argument, no preceding `-`\n**NOTE:** `short` is mutually exclusive with `index`",1],[12,"long","","The long version of the flag (i.e. word) without the preceding `--`\n**NOTE:** `long` is mutually exclusive with `index`",1],[12,"help","","The string of text that will displayed to the user when the application's\n`help` text is displayed",1],[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.",1],[12,"takes_value","","Determines if this argument is an option, vice a flag or positional and\nis mutually exclusive with `index` and `multiple`",1],[12,"index","","The index of the argument. `index` is mutually exclusive with `takes_value`\nand `multiple`",1],[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`",1],[12,"blacklist","","A list of names for other arguments that *may not* be used with this flag",1],[12,"requires","","A list of names of other arguments that are *required* to be used when\nthis flag is used",1],[3,"App","","Used to create a representation of the program and all possible command line arguments\n for parsing at runtime."],[12,"name","","The name displayed to the user when showing version and help/usage information",2],[12,"author","","A string of author(s) if desired. Displayed when showing help/usage information",2],[12,"version","","The version displayed to the user",2],[12,"about","","A brief explaination of the program that gets displayed to the user when shown help/usage information",2],[3,"SubCommand","","The abstract representation of a command line subcommand used by the consumer of the library.\n "],[12,"name","","",3],[12,"matches","","",3],[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.",2],[11,"author","","Sets a string of author(s)",2],[11,"about","","Sets a string briefly describing what the program does",2],[11,"version","","Sets a string of the version number",2],[11,"usage","","Sets a custom usage string to over-ride the one auto-generated by `clap`\n*NOTE:* You do not need to specify the \"USAGE: \" portion, as that will \nstill be applied by `clap`, you only need to specify the portion starting\nwith the binary name. \n*NOTE:* This will not replace the entire help message, only the portion\nshowing the usage.",2],[11,"arg","","Adds an argument to the list of valid possibilties",2],[11,"args","","Adds multiple arguments to the list of valid possibilties",2],[11,"subcommand","","Adds a subcommand to the list of valid possibilties. Subcommands\n are effectively sub apps, because they can contain their own arguments\n and subcommands. They also function just like apps, in that they get their\n own auto generated help and version switches.",2],[11,"subcommands","","Adds multiple subcommands to the list of valid possibilties",2],[11,"get_matches","","",2],[11,"new","","Creates a new instance of `ArgMatches`. This ins't called directly, but\nthrough the `.get_matches()` method of `App`",0],[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`",0],[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)",0],[11,"occurrences_of","","Checks the number of occurrences of a flag at runtime.",0],[11,"subcommand_matches","","If a subcommand was found, returns the ArgMatches struct associated with it's matches",0],[11,"subcommand_name","","If a subcommand was found, returns the name associated with it",0],[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. ",1],[11,"short","","Sets the short version of the argument without the preceding `-`.",1],[11,"long","","Sets the long version of the argument without the preceding `--`.",1],[11,"help","","Sets the help text of the argument that will be displayed to the user\nwhen they print the usage/help information. ",1],[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.",1],[11,"mutually_excludes","","Sets a mutually exclusive argument by name. I.e. when using this argument,\nthe following argument can't be present.",1],[11,"mutually_excludes_all","","Sets a mutually exclusive arguments by names. I.e. when using this argument,\n the following argument can't be present.",1],[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.",1],[11,"requires_all","","Sets arguments by names that are required when this one is presnet I.e. when\n using this argument, the following arguments *must* be present.",1],[11,"takes_value","","Specifies that the argument takes an additional value at run time.\n \n**NOTE:** When setting this to `true` the `name` of the argument\nwill be used when printing the help/usage information to the user. ",1],[11,"index","","Specifies the index of a positional argument starting at 1.\n \n**NOTE:** When setting this, any `short` or `long` values you set\nare ignored as positional arguments cannot have a `short` or `long`.\nAlso, the name will be used when printing the help/usage information \nto the user. ",1],[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.\n \n**NOTE:** When setting this, any `takes_value` or `index` values you set\nare ignored as flags cannot have a values or an `index`.",1],[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.",3]],"paths":[[3,"ArgMatches"],[3,"Arg"],[3,"App"],[3,"SubCommand"]]}; +searchIndex['clap'] = {"items":[[0,"","clap","A simply library for parsing command line arguments when writing\ncommand line and console applications."],[3,"ArgMatches","","Used to get information about the arguments that\nwhere supplied to the program at runtime."],[12,"matches_of","","",0],[12,"flags","","",0],[12,"opts","","",0],[12,"positionals","","",0],[12,"subcommand","","",0],[3,"Arg","","The abstract representation of a command line argument used by the consumer of the library.\n "],[12,"name","","The unique name of the argument, required",1],[12,"short","","The short version (i.e. single character) of the argument, no preceding `-`\n**NOTE:** `short` is mutually exclusive with `index`",1],[12,"long","","The long version of the flag (i.e. word) without the preceding `--`\n**NOTE:** `long` is mutually exclusive with `index`",1],[12,"help","","The string of text that will displayed to the user when the application's\n`help` text is displayed",1],[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.",1],[12,"takes_value","","Determines if this argument is an option, vice a flag or positional and\nis mutually exclusive with `index` and `multiple`",1],[12,"index","","The index of the argument. `index` is mutually exclusive with `takes_value`\nand `multiple`",1],[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`",1],[12,"blacklist","","A list of names for other arguments that *may not* be used with this flag",1],[12,"requires","","A list of names of other arguments that are *required* to be used when\nthis flag is used",1],[3,"App","","Used to create a representation of the program and all possible command line arguments\nfor parsing at runtime."],[12,"name","","The name displayed to the user when showing version and help/usage information",2],[12,"author","","A string of author(s) if desired. Displayed when showing help/usage information",2],[12,"version","","The version displayed to the user",2],[12,"about","","A brief explaination of the program that gets displayed to the user when shown help/usage information",2],[3,"SubCommand","","The abstract representation of a command line subcommand used by the consumer of the library.\n "],[12,"name","","",3],[12,"matches","","",3],[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.",2],[11,"author","","Sets a string of author(s)",2],[11,"about","","Sets a string briefly describing what the program does",2],[11,"version","","Sets a string of the version number",2],[11,"usage","","Sets a custom usage string to over-ride the one auto-generated by `clap`\n*NOTE:* You do not need to specify the \"USAGE: \" portion, as that will \nstill be applied by `clap`, you only need to specify the portion starting\nwith the binary name. \n*NOTE:* This will not replace the entire help message, only the portion\nshowing the usage.",2],[11,"arg","","Adds an argument to the list of valid possibilties",2],[11,"args","","Adds multiple arguments to the list of valid possibilties",2],[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.",2],[11,"subcommands","","Adds multiple subcommands to the list of valid possibilties",2],[11,"get_matches","","",2],[11,"new","","Creates a new instance of `ArgMatches`. This ins't called directly, but\nthrough the `.get_matches()` method of `App`",0],[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`",0],[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)",0],[11,"occurrences_of","","Checks the number of occurrences of a flag at runtime.",0],[11,"subcommand_matches","","If a subcommand was found, returns the ArgMatches struct associated with it's matches",0],[11,"subcommand_name","","If a subcommand was found, returns the name associated with it",0],[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. ",1],[11,"short","","Sets the short version of the argument without the preceding `-`.",1],[11,"long","","Sets the long version of the argument without the preceding `--`.",1],[11,"help","","Sets the help text of the argument that will be displayed to the user\nwhen they print the usage/help information. ",1],[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.",1],[11,"mutually_excludes","","Sets a mutually exclusive argument by name. I.e. when using this argument,\nthe following argument can't be present.",1],[11,"mutually_excludes_all","","Sets a mutually exclusive arguments by names. I.e. when using this argument,\nthe following argument can't be present.",1],[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.",1],[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.",1],[11,"takes_value","","Specifies that the argument takes an additional value at run time.\n \n**NOTE:** When setting this to `true` the `name` of the argument\nwill be used when printing the help/usage information to the user. ",1],[11,"index","","Specifies the index of a positional argument starting at 1.\n \n**NOTE:** When setting this, any `short` or `long` values you set\nare ignored as positional arguments cannot have a `short` or `long`.\nAlso, the name will be used when printing the help/usage information \nto the user. ",1],[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.\n \n**NOTE:** When setting this, any `takes_value` or `index` values you set\nare ignored as flags cannot have a values or an `index`.",1],[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.",3]],"paths":[[3,"ArgMatches"],[3,"Arg"],[3,"App"],[3,"SubCommand"]]}; initSearch(searchIndex); diff --git a/docs/src/clap/app.rs/app.rs.html b/docs/src/clap/app.rs/app.rs.html index 64001b45..7c3267f9 100644 --- a/docs/src/clap/app.rs/app.rs.html +++ b/docs/src/clap/app.rs/app.rs.html @@ -936,10 +936,10 @@ /// # use clap::{App, Arg}; /// let myprog = App::new("myprog") /// .author("Me, me@mail.com") -/// .version("1.0.2") +/// .version("1.0.2") /// .about("Explains in brief what the program does") /// .arg( -/// Arg::new("in_file").index(1) +/// Arg::new("in_file").index(1) /// // Add other possible command line argument options here... /// ) /// .get_matches(); @@ -947,831 +947,831 @@ /// // Your pogram logic starts here... /// ``` pub struct App { - /// The name displayed to the user when showing version and help/usage information - pub name: &'static str, - /// A string of author(s) if desired. Displayed when showing help/usage information - pub author: Option<&'static str>, - /// The version displayed to the user - pub version: Option<&'static str>, - /// A brief explaination of the program that gets displayed to the user when shown help/usage information - pub about: Option<&'static str>, - flags: HashMap<&'static str, FlagArg>, - opts: HashMap<&'static str, OptArg>, - positionals_idx: BTreeMap<u8, PosArg>, - subcommands: HashMap<&'static str, Box<App>>, - // positionals_name: HashMap<&'static str, PosArg>, - needs_long_help: bool, - needs_long_version: bool, - needs_short_help: bool, - needs_short_version: bool, - needs_subcmd_help: bool, - required: HashSet<&'static str>, - arg_list: HashSet<&'static str>, - short_list: HashSet<char>, - long_list: HashSet<&'static str>, - blacklist: HashSet<&'static str>, - usage_str: Option<&'static str>, - bin_name: Option<String> + /// The name displayed to the user when showing version and help/usage information + pub name: &'static str, + /// A string of author(s) if desired. Displayed when showing help/usage information + pub author: Option<&'static str>, + /// The version displayed to the user + pub version: Option<&'static str>, + /// A brief explaination of the program that gets displayed to the user when shown help/usage information + pub about: Option<&'static str>, + flags: HashMap<&'static str, FlagArg>, + opts: HashMap<&'static str, OptArg>, + positionals_idx: BTreeMap<u8, PosArg>, + subcommands: HashMap<&'static str, Box<App>>, + // positionals_name: HashMap<&'static str, PosArg>, + needs_long_help: bool, + needs_long_version: bool, + needs_short_help: bool, + needs_short_version: bool, + needs_subcmd_help: bool, + required: HashSet<&'static str>, + arg_list: HashSet<&'static str>, + short_list: HashSet<char>, + long_list: HashSet<&'static str>, + blacklist: HashSet<&'static str>, + usage_str: Option<&'static str>, + bin_name: Option<String> } impl App { - /// Creates a new instance of an application requiring a name (such as the binary). Will be displayed - /// to the user when they print version or help and usage information. - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// let prog = App::new("myprog") - /// # .get_matches(); - /// ``` - pub fn new(n: &'static str) -> App { - App { - name: n, - author: None, - about: None, - version: None, - flags: HashMap::new(), - opts: HashMap::new(), - positionals_idx: BTreeMap::new(), - subcommands: HashMap::new(), - // positionals_name: HashMap::new(), - needs_long_version: true, - needs_long_help: true, - needs_short_help: true, - needs_subcmd_help: true, - needs_short_version: true, - required: HashSet::new(), - arg_list: HashSet::new(), - short_list: HashSet::new(), - long_list: HashSet::new(), - usage_str: None, - blacklist: HashSet::new(), - bin_name: None, - } - } + /// Creates a new instance of an application requiring a name (such as the binary). Will be displayed + /// to the user when they print version or help and usage information. + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// let prog = App::new("myprog") + /// # .get_matches(); + /// ``` + pub fn new(n: &'static str) -> App { + App { + name: n, + author: None, + about: None, + version: None, + flags: HashMap::new(), + opts: HashMap::new(), + positionals_idx: BTreeMap::new(), + subcommands: HashMap::new(), + // positionals_name: HashMap::new(), + needs_long_version: true, + needs_long_help: true, + needs_short_help: true, + needs_subcmd_help: true, + needs_short_version: true, + required: HashSet::new(), + arg_list: HashSet::new(), + short_list: HashSet::new(), + long_list: HashSet::new(), + usage_str: None, + blacklist: HashSet::new(), + bin_name: None, + } + } - /// Sets a string of author(s) - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// # let app = App::new("myprog") - /// .author("Kevin <kbknapp@gmail.com>") - /// # .get_matches(); - /// ``` - pub fn author(mut self, a: &'static str) -> App { - self.author = Some(a); - self - } + /// Sets a string of author(s) + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// # let app = App::new("myprog") + /// .author("Kevin <kbknapp@gmail.com>") + /// # .get_matches(); + /// ``` + pub fn author(mut self, a: &'static str) -> App { + self.author = Some(a); + self + } - /// Sets a string briefly describing what the program does - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// # let app = App::new("myprog") - /// .about("Does really amazing things to great people") - /// # .get_matches(); - /// ``` - pub fn about(mut self, a: &'static str) -> App { - self.about = Some(a); - self - } + /// Sets a string briefly describing what the program does + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// # let app = App::new("myprog") + /// .about("Does really amazing things to great people") + /// # .get_matches(); + /// ``` + pub fn about(mut self, a: &'static str) -> App { + self.about = Some(a); + self + } - /// Sets a string of the version number - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// # let app = App::new("myprog") - /// .version("v0.1.24") - /// # .get_matches(); - /// ``` - pub fn version(mut self, v: &'static str) -> App { - self.version = Some(v); - self - } - - /// Sets a custom usage string to over-ride the one auto-generated by `clap` - /// *NOTE:* You do not need to specify the "USAGE: " 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 - /// showing the usage. - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// # let app = App::new("myprog") - /// .usage("myapp [-clDas] <some_file>") - /// # .get_matches(); - /// ``` - pub fn usage(mut self, u: &'static str) -> App { - self.usage_str = Some(u); - self - } + /// Sets a string of the version number + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// # let app = App::new("myprog") + /// .version("v0.1.24") + /// # .get_matches(); + /// ``` + pub fn version(mut self, v: &'static str) -> App { + self.version = Some(v); + self + } + + /// Sets a custom usage string to over-ride the one auto-generated by `clap` + /// *NOTE:* You do not need to specify the "USAGE: " 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 + /// showing the usage. + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// # let app = App::new("myprog") + /// .usage("myapp [-clDas] <some_file>") + /// # .get_matches(); + /// ``` + pub fn usage(mut self, u: &'static str) -> App { + self.usage_str = Some(u); + self + } - /// Adds an argument to the list of valid possibilties - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// # let app = App::new("myprog") - /// .arg(Arg::new("config") - /// .short("c") - /// // Additional argument configuration goes here... - /// ) - /// # .get_matches(); - /// ``` - pub fn arg(mut self, a: Arg) -> App { - if self.arg_list.contains(a.name) { - panic!("Argument name must be unique, \"{}\" is already in use", a.name); - } else { - self.arg_list.insert(a.name); - } - if let Some(ref s) = a.short { - if self.short_list.contains(s) { - panic!("Argument short must be unique, -{} is already in use", s); - } else { - self.short_list.insert(*s); - } - } - if let Some(ref l) = a.long { - if self.long_list.contains(l) { - panic!("Argument long must be unique, --{} is already in use", l); - } else { - self.long_list.insert(l); - } - } - if a.required { - self.required.insert(a.name); - } - if let Some(i) = a.index { - self.positionals_idx.insert(i, PosArg { - name: a.name, - index: i, - required: a.required, - blacklist: a.blacklist, - requires: a.requires, - help: a.help, - value: None - }); - } else if a.takes_value { - if a.short == None && a.long == None { - panic!("An argument that takes a value must have either a .short() or .long() [or both] assigned"); - } - self.opts.insert(a.name, OptArg { - name: a.name, - short: a.short, - long: a.long, - blacklist: a.blacklist, - help: a.help, - requires: a.requires, - required: a.required, - value: None - }); - } else { - if let Some(ref l) = a.long { - if *l == "help" { - self.needs_long_help = false; - } else if *l == "version" { - self.needs_long_version = false; - } - } - if let Some(ref s) = a.short { - if *s == 'h' { - self.needs_short_help = false; - } else if *s == 'v' { - self.needs_short_version = false; - } - } - if a.short == None && a.long == None { - panic!("A flag argument must have either a .short() or .long() [or both] assigned"); - } - // Flags can't be required - if self.required.contains(a.name) { - self.required.remove(a.name); - } - self.flags.insert(a.name, FlagArg{ - name: a.name, - short: a.short, - long: a.long, - help: a.help, - blacklist: a.blacklist, - multiple: a.multiple, - requires: a.requires, - occurrences: 1 - }); - } - self - } + /// Adds an argument to the list of valid possibilties + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// # let app = App::new("myprog") + /// .arg(Arg::new("config") + /// .short("c") + /// // Additional argument configuration goes here... + /// ) + /// # .get_matches(); + /// ``` + pub fn arg(mut self, a: Arg) -> App { + if self.arg_list.contains(a.name) { + panic!("Argument name must be unique, \"{}\" is already in use", a.name); + } else { + self.arg_list.insert(a.name); + } + if let Some(ref s) = a.short { + if self.short_list.contains(s) { + panic!("Argument short must be unique, -{} is already in use", s); + } else { + self.short_list.insert(*s); + } + } + if let Some(ref l) = a.long { + if self.long_list.contains(l) { + panic!("Argument long must be unique, --{} is already in use", l); + } else { + self.long_list.insert(l); + } + } + if a.required { + self.required.insert(a.name); + } + if let Some(i) = a.index { + self.positionals_idx.insert(i, PosArg { + name: a.name, + index: i, + required: a.required, + blacklist: a.blacklist, + requires: a.requires, + help: a.help, + value: None + }); + } else if a.takes_value { + if a.short == None && a.long == None { + panic!("An argument that takes a value must have either a .short() or .long() [or both] assigned"); + } + self.opts.insert(a.name, OptArg { + name: a.name, + short: a.short, + long: a.long, + blacklist: a.blacklist, + help: a.help, + requires: a.requires, + required: a.required, + value: None + }); + } else { + if let Some(ref l) = a.long { + if *l == "help" { + self.needs_long_help = false; + } else if *l == "version" { + self.needs_long_version = false; + } + } + if let Some(ref s) = a.short { + if *s == 'h' { + self.needs_short_help = false; + } else if *s == 'v' { + self.needs_short_version = false; + } + } + if a.short == None && a.long == None { + panic!("A flag argument must have either a .short() or .long() [or both] assigned"); + } + // Flags can't be required + if self.required.contains(a.name) { + self.required.remove(a.name); + } + self.flags.insert(a.name, FlagArg{ + name: a.name, + short: a.short, + long: a.long, + help: a.help, + blacklist: a.blacklist, + multiple: a.multiple, + requires: a.requires, + occurrences: 1 + }); + } + self + } - /// Adds multiple arguments to the list of valid possibilties - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg}; - /// # let app = App::new("myprog") - /// .args( vec![Arg::new("config").short("c"), - /// Arg::new("debug").short("d")]) - /// # .get_matches(); - /// ``` - pub fn args(mut self, args: Vec<Arg>) -> App { - for arg in args.into_iter() { - self = self.arg(arg); - } - self - } + /// Adds multiple arguments to the list of valid possibilties + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg}; + /// # let app = App::new("myprog") + /// .args( vec![Arg::new("config").short("c"), + /// Arg::new("debug").short("d")]) + /// # .get_matches(); + /// ``` + pub fn args(mut self, args: Vec<Arg>) -> App { + for arg in args.into_iter() { + self = self.arg(arg); + } + 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. - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg, SubCommand}; - /// # let app = App::new("myprog") - /// .subcommand(SubCommand::new("config") - /// .about("Controls configuration features") - /// .arg(Arg::new("config_file") - /// .index(1) - /// .help("Configuration file to use"))) - /// // Additional subcommand configuration goes here, such as arguments... - /// ) - /// # .get_matches(); - /// ``` - pub fn subcommand(mut self, subcmd: App) -> App { - if subcmd.name == "help" { self.needs_subcmd_help = false; } - self.subcommands.insert(subcmd.name, Box::new(subcmd)); - 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. + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg, SubCommand}; + /// # let app = App::new("myprog") + /// .subcommand(SubCommand::new("config") + /// .about("Controls configuration features") + /// .arg(Arg::new("config_file") + /// .index(1) + /// .help("Configuration file to use"))) + /// // Additional subcommand configuration goes here, such as arguments... + /// ) + /// # .get_matches(); + /// ``` + pub fn subcommand(mut self, subcmd: App) -> App { + if subcmd.name == "help" { self.needs_subcmd_help = false; } + self.subcommands.insert(subcmd.name, Box::new(subcmd)); + self + } - /// Adds multiple subcommands to the list of valid possibilties - /// - /// # Example - /// - /// ```no_run - /// # use clap::{App, Arg, SubCommand}; - /// # let app = App::new("myprog") - /// .subcommands( vec![ - /// SubCommand::new("config").about("Controls configuration functionality") - /// .arg(Arg::new("config_file").index(1)), - /// SubCommand::new("debug").about("Controls debug functionality")]) - /// # .get_matches(); - /// ``` - pub fn subcommands(mut self, subcmds: Vec<App>) -> App { - for subcmd in subcmds.into_iter() { - self = self.subcommand(subcmd); - } - self - } + /// Adds multiple subcommands to the list of valid possibilties + /// + /// # Example + /// + /// ```no_run + /// # use clap::{App, Arg, SubCommand}; + /// # let app = App::new("myprog") + /// .subcommands( vec![ + /// SubCommand::new("config").about("Controls configuration functionality") + /// .arg(Arg::new("config_file").index(1)), + /// SubCommand::new("debug").about("Controls debug functionality")]) + /// # .get_matches(); + /// ``` + pub fn subcommands(mut self, subcmds: Vec<App>) -> App { + for subcmd in subcmds.into_iter() { + self = self.subcommand(subcmd); + } + self + } - fn exit(&self) { - unsafe { libc::exit(0); } - } + fn exit(&self) { + unsafe { libc::exit(0); } + } - fn report_error(&self, msg: String, help: bool, quit: bool) { - println!("{}", msg); - if help { self.print_usage(true); } - if quit { env::set_exit_status(1); self.exit(); } - } + fn report_error(&self, msg: String, help: bool, quit: bool) { + println!("{}", msg); + if help { self.print_usage(true); } + if quit { env::set_exit_status(1); self.exit(); } + } - fn print_usage(&self, more_info: bool) { - println!("USAGE:"); - if let Some(u) = self.usage_str { - println!("\t{}",u); - } else { - let flags = ! self.flags.is_empty(); - let pos = ! self.positionals_idx.is_empty(); - let req_pos = self.positionals_idx.values().filter_map(|ref x| if x.required { Some(x.name) } else {None}) - .fold(String::new(), |acc, ref name| acc + &format!("<{}> ", name.to_uppercase())[..]); - let req_opts = self.opts.values().filter(|ref x| x.required) - .fold(String::new(), |acc, ref o| acc + &format!("{}{} ",if let Some(s) = o.short { - format!("-{} ", s) - } else { - format!("--{}=",o.long.unwrap()) - },o.name.to_uppercase())); - let opts = ! self.opts.is_empty(); - let subcmds = ! self.subcommands.is_empty(); + fn print_usage(&self, more_info: bool) { + println!("USAGE:"); + if let Some(u) = self.usage_str { + println!("\t{}",u); + } else { + let flags = ! self.flags.is_empty(); + let pos = ! self.positionals_idx.is_empty(); + let req_pos = self.positionals_idx.values().filter_map(|ref x| if x.required { Some(x.name) } else {None}) + .fold(String::new(), |acc, ref name| acc + &format!("<{}> ", name.to_uppercase())[..]); + let req_opts = self.opts.values().filter(|ref x| x.required) + .fold(String::new(), |acc, ref o| acc + &format!("{}{} ",if let Some(s) = o.short { + format!("-{} ", s) + } else { + format!("--{}=",o.long.unwrap()) + },o.name.to_uppercase())); + let opts = ! self.opts.is_empty(); + let subcmds = ! self.subcommands.is_empty(); - print!("\t{} {} {} {} {}", if let Some(ref name) = self.bin_name { &name[..] } else { self.name }, - if flags {"[FLAGS]"} else {""}, - if opts { - if req_opts.is_empty() { "[OPTIONS]" } else { &req_opts[..] } - } else { "" }, - if pos { - if req_pos.is_empty() { "[POSITIONAL]"} else { &req_pos[..] } - } else {""}, - if subcmds {"[SUBCOMMANDS]"} else {""}); - } + print!("\t{} {} {} {} {}", if let Some(ref name) = self.bin_name { &name[..] } else { self.name }, + if flags {"[FLAGS]"} else {""}, + if opts { + if req_opts.is_empty() { "[OPTIONS]" } else { &req_opts[..] } + } else { "" }, + if pos { + if req_pos.is_empty() { "[POSITIONAL]"} else { &req_pos[..] } + } else {""}, + if subcmds {"[SUBCOMMANDS]"} else {""}); + } - if more_info { - println!("\nFor more information try --help"); - } - } + if more_info { + println!("\nFor more information try --help"); + } + } - fn print_help(&self) { - self.print_version(false); - let flags = ! self.flags.is_empty(); - let pos = ! self.positionals_idx.is_empty(); - let opts = ! self.opts.is_empty(); - let subcmds = ! self.subcommands.is_empty(); + fn print_help(&self) { + self.print_version(false); + let flags = ! self.flags.is_empty(); + let pos = ! self.positionals_idx.is_empty(); + let opts = ! self.opts.is_empty(); + let subcmds = ! self.subcommands.is_empty(); - if let Some(author) = self.author { - println!("{}", author); - } - if let Some(about) = self.about { - println!("{}", about); - } - println!(""); - self.print_usage(false); - if flags || opts || pos || subcmds { - println!(""); - } - if flags { - println!(""); - println!("FLAGS:"); - for v in self.flags.values() { - println!("\t{}{}\t{}", - if let Some(s) = v.short{format!("-{}",s)}else{format!(" ")}, - if let Some(l) = v.long {format!(",--{}",l)}else {format!(" \t")}, - if let Some(h) = v.help {h} else {" "} ); - } - } - if opts { - println!(""); - println!("OPTIONS:"); - for v in self.opts.values() { - let mut needs_tab = false; - println!("\t{}{}{}\t{}", - if let Some(ref s) = v.short{format!("-{} ",s)}else{format!(" ")}, - if let Some(ref l) = v.long {format!(",--{}=",l)}else {needs_tab = true; format!(" ")}, - format!("{}", v.name.to_uppercase()), - if let Some(ref h) = v.help {if needs_tab {format!("\t{}", *h)} else { format!("{}", *h) } } else {format!(" ")} ); - } - } - if pos { - println!(""); - println!("POSITIONAL ARGUMENTS:"); - for v in self.positionals_idx.values() { - println!("\t{}\t\t\t{}", v.name, - if let Some(h) = v.help {h} else {" "} ); - } - } - if subcmds { - println!(""); - println!("SUBCOMMANDS:"); - for sc in self.subcommands.values() { - println!("\t{}\t\t{}", sc.name, - if let Some(a) = sc.about {a} else {" "} ); - } - } + if let Some(author) = self.author { + println!("{}", author); + } + if let Some(about) = self.about { + println!("{}", about); + } + println!(""); + self.print_usage(false); + if flags || opts || pos || subcmds { + println!(""); + } + if flags { + println!(""); + println!("FLAGS:"); + for v in self.flags.values() { + println!("\t{}{}\t{}", + if let Some(s) = v.short{format!("-{}",s)}else{format!(" ")}, + if let Some(l) = v.long {format!(",--{}",l)}else {format!(" \t")}, + if let Some(h) = v.help {h} else {" "} ); + } + } + if opts { + println!(""); + println!("OPTIONS:"); + for v in self.opts.values() { + let mut needs_tab = false; + println!("\t{}{}{}\t{}", + if let Some(ref s) = v.short{format!("-{} ",s)}else{format!(" ")}, + if let Some(ref l) = v.long {format!(",--{}=",l)}else {needs_tab = true; format!(" ")}, + format!("{}", v.name.to_uppercase()), + if let Some(ref h) = v.help {if needs_tab {format!("\t{}", *h)} else { format!("{}", *h) } } else {format!(" ")} ); + } + } + if pos { + println!(""); + println!("POSITIONAL ARGUMENTS:"); + for v in self.positionals_idx.values() { + println!("\t{}\t\t\t{}", v.name, + if let Some(h) = v.help {h} else {" "} ); + } + } + if subcmds { + println!(""); + println!("SUBCOMMANDS:"); + for sc in self.subcommands.values() { + println!("\t{}\t\t{}", sc.name, + if let Some(a) = sc.about {a} else {" "} ); + } + } - self.exit(); - } + self.exit(); + } - fn print_version(&self, quit: bool) { - println!("{} {}", self.name, if let Some(v) = self.version {v} else {""} ); - if quit { self.exit(); } - } + fn print_version(&self, quit: bool) { + println!("{} {}", self.name, if let Some(v) = self.version {v} else {""} ); + if quit { self.exit(); } + } - fn check_for_help_and_version(&self, arg: char) { - if arg == 'h' && self.needs_short_help { - self.print_help(); - } else if arg == 'v' && self.needs_short_version { - self.print_version(true); - } - } + fn check_for_help_and_version(&self, arg: char) { + if arg == 'h' && self.needs_short_help { + self.print_help(); + } else if arg == 'v' && self.needs_short_version { + self.print_version(true); + } + } - fn parse_long_arg(&mut self, matches: &mut ArgMatches ,full_arg: &String) -> Option<&'static str> { - let mut arg = full_arg.as_slice().trim_left_matches(|c| c == '-'); - let mut found = false; + fn parse_long_arg(&mut self, matches: &mut ArgMatches ,full_arg: &String) -> Option<&'static str> { + let mut arg = full_arg.as_slice().trim_left_matches(|c| c == '-'); + let mut found = false; - if arg == "help" && self.needs_long_help { - self.print_help(); - } else if arg == "version" && self.needs_long_version { - self.print_version(true); - } + if arg == "help" && self.needs_long_help { + self.print_help(); + } else if arg == "version" && self.needs_long_version { + self.print_version(true); + } - let mut arg_val: Option<String> = None; + let mut arg_val: Option<String> = None; - if arg.contains("=") { - let arg_vec: Vec<&str> = arg.split("=").collect(); - arg = arg_vec[0]; - arg_val = Some(arg_vec[1].to_string()); - } + if arg.contains("=") { + let arg_vec: Vec<&str> = arg.split("=").collect(); + arg = arg_vec[0]; + arg_val = Some(arg_vec[1].to_string()); + } - for (k, v) in self.opts.iter() { - if let Some(ref l) = v.long { - if *l == arg { - if self.blacklist.contains(k) { - self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), - true, true); - } - matches.opts.insert(k, OptArg{ - name: v.name, - short: v.short, - long: v.long, - help: v.help, - required: v.required, - blacklist: None, - requires: None, - value: arg_val.clone() - }); - match arg_val { - None => { return Some(v.name); }, - _ => { return None; } - } - } - } - } + for (k, v) in self.opts.iter() { + if let Some(ref l) = v.long { + if *l == arg { + if self.blacklist.contains(k) { + self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), + true, true); + } + matches.opts.insert(k, OptArg{ + name: v.name, + short: v.short, + long: v.long, + help: v.help, + required: v.required, + blacklist: None, + requires: None, + value: arg_val.clone() + }); + match arg_val { + None => { return Some(v.name); }, + _ => { return None; } + } + } + } + } - for (k, v) in self.flags.iter() { - if let Some(ref l) = v.long { - if *l != arg { continue; } - found = true; - let mut multi = false; - if let Some(ref mut f) = matches.flags.get_mut(k) { - f.occurrences = if f.multiple { f.occurrences + 1 } else { 1 }; - multi = true; - } - if ! multi { - if self.blacklist.contains(k) { - self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), - true, true); - } - matches.flags.insert(k, FlagArg{ - name: v.name, - short: v.short, - long: v.long, - help: v.help, - multiple: v.multiple, - occurrences: v.occurrences, - blacklist: None, - requires: None - }); - if self.required.contains(k) { - self.required.remove(k); - } - if let Some(ref bl) = v.blacklist { - if ! bl.is_empty() { - for name in bl.iter() { - self.blacklist.insert(name); - } - } - } - } - if let Some(ref reqs) = v.requires { - if ! reqs.is_empty() { - for n in reqs.iter() { - if matches.opts.contains_key(n) { continue; } - if matches.flags.contains_key(n) { continue; } - if matches.positionals.contains_key(n) { continue; } - self.required.insert(n); - } - } - } - break; - } - } + for (k, v) in self.flags.iter() { + if let Some(ref l) = v.long { + if *l != arg { continue; } + found = true; + let mut multi = false; + if let Some(ref mut f) = matches.flags.get_mut(k) { + f.occurrences = if f.multiple { f.occurrences + 1 } else { 1 }; + multi = true; + } + if ! multi { + if self.blacklist.contains(k) { + self.report_error(format!("The argument --{} is mutually exclusive with one or more other arguments", arg), + true, true); + } + matches.flags.insert(k, FlagArg{ + name: v.name, + short: v.short, + long: v.long, + help: v.help, + multiple: v.multiple, + occurrences: v.occurrences, + blacklist: None, + requires: None + }); + if self.required.contains(k) { + self.required.remove(k); + } + if let Some(ref bl) = v.blacklist { + if ! bl.is_empty() { + for name in bl.iter() { + self.blacklist.insert(name); + } + } + } + } + if let Some(ref reqs) = v.requires { + if ! reqs.is_empty() { + for n in reqs.iter() { + if matches.opts.contains_key(n) { continue; } + if matches.flags.contains_key(n) { continue; } + if matches.positionals.contains_key(n) { continue; } + self.required.insert(n); + } + } + } + break; + } + } - if ! found { - self.report_error( - format!("Argument --{} isn't valid", arg), - true, true); - } - None - } + if ! found { + self.report_error( + format!("Argument --{} isn't valid", arg), + true, true); + } + None + } - fn parse_short_arg(&mut self, matches: &mut ArgMatches ,full_arg: &String) -> Option<&'static str> { - let arg = full_arg.as_slice().trim_left_matches(|c| c == '-'); - if arg.len() > 1 { - // Multiple flags using short i.e. -bgHlS - for c in arg.chars() { - self.check_for_help_and_version(c); - if ! self.parse_single_short_flag(matches, c) { - self.report_error( - format!("Argument -{} isn't valid",c), - true, true); - } - } - } else { - // Short flag or opt - let arg_c = arg.char_at(0); - self.check_for_help_and_version(arg_c); + fn parse_short_arg(&mut self, matches: &mut ArgMatches ,full_arg: &String) -> Option<&'static str> { + let arg = full_arg.as_slice().trim_left_matches(|c| c == '-'); + if arg.len() > 1 { + // Multiple flags using short i.e. -bgHlS + for c in arg.chars() { + self.check_for_help_and_version(c); + if ! self.parse_single_short_flag(matches, c) { + self.report_error( + format!("Argument -{} isn't valid",c), + true, true); + } + } + } else { + // Short flag or opt + let arg_c = arg.char_at(0); + self.check_for_help_and_version(arg_c); - if ! self.parse_single_short_flag(matches, arg_c) { - for (k, v) in self.opts.iter() { - if let Some(s) = v.short { - if s == arg_c { - return Some(k) - } - } - } + if ! self.parse_single_short_flag(matches, arg_c) { + for (k, v) in self.opts.iter() { + if let Some(s) = v.short { + if s == arg_c { + return Some(k) + } + } + } - self.report_error( - format!("Argument -{} isn't valid",arg_c), - true, true); - } + self.report_error( + format!("Argument -{} isn't valid",arg_c), + true, true); + } - } - None - } + } + None + } - fn parse_single_short_flag(&mut self, matches: &mut ArgMatches, arg: char) -> bool { - for (k, v) in self.flags.iter() { - if let Some(s) = v.short { - if s != arg { continue; } + fn parse_single_short_flag(&mut self, matches: &mut ArgMatches, arg: char) -> bool { + for (k, v) in self.flags.iter() { + if let Some(s) = v.short { + if s != arg { continue; } - if !matches.flags.contains_key(k) { - if self.blacklist.contains(k) { - self.report_error(format!("The argument -{} is mutually exclusive with one or more other arguments", arg), - false, true); - } - matches.flags.insert(k, FlagArg{ - name: v.name, - short: v.short, - long: v.long, - help: v.help, - multiple: v.multiple, - occurrences: v.occurrences, - blacklist: None, - requires: None - }); - if self.required.contains(k) { - self.required.remove(k); - } - if let Some(ref reqs) = v.requires { - if ! reqs.is_empty() { - for n in reqs.iter() { - if matches.opts.contains_key(n) { continue; } - if matches.flags.contains_key(n) { continue; } - if matches.positionals.contains_key(n) { continue; } - self.required.insert(n); - } - } - } - if let Some(ref bl) = v.blacklist { - if ! bl.is_empty() { - for name in bl.iter() { - self.blacklist.insert(name); - } - } - } - } else if matches.flags.get(k).unwrap().multiple { - matches.flags.get_mut(k).unwrap().occurrences += 1 - } + if !matches.flags.contains_key(k) { + if self.blacklist.contains(k) { + self.report_error(format!("The argument -{} is mutually exclusive with one or more other arguments", arg), + false, true); + } + matches.flags.insert(k, FlagArg{ + name: v.name, + short: v.short, + long: v.long, + help: v.help, + multiple: v.multiple, + occurrences: v.occurrences, + blacklist: None, + requires: None + }); + if self.required.contains(k) { + self.required.remove(k); + } + if let Some(ref reqs) = v.requires { + if ! reqs.is_empty() { + for n in reqs.iter() { + if matches.opts.contains_key(n) { continue; } + if matches.flags.contains_key(n) { continue; } + if matches.positionals.contains_key(n) { continue; } + self.required.insert(n); + } + } + } + if let Some(ref bl) = v.blacklist { + if ! bl.is_empty() { + for name in bl.iter() { + self.blacklist.insert(name); + } + } + } + } else if matches.flags.get(k).unwrap().multiple { + matches.flags.get_mut(k).unwrap().occurrences += 1 + } - return true; - } - } - false - } + return true; + } + } + false + } - fn validate_blacklist(&self, matches: &ArgMatches) { - if ! self.blacklist.is_empty() { - for name in self.blacklist.iter() { - for (k, v) in matches.flags.iter() { - if k == name { - self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments", - if let Some(s) = v.short { - format!("-{}", s) - } else if let Some(l) = v.long { - format!("--{}", l) - } else { - format!("\"{}\"", v.name) - }), - true, true); - } - } - for (k, v) in matches.opts.iter() { - if k == name { - self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments", - if let Some(s) = v.short { - format!("-{}", s) - } else if let Some(l) = v.long { - format!("--{}", l) - } else { - format!("\"{}\"", v.name) - }), - true, true); - } - } - for (k, v) in matches.positionals.iter() { - if k == name { - self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments",v.name), - false, true); - } - } - } - } - } + fn validate_blacklist(&self, matches: &ArgMatches) { + if ! self.blacklist.is_empty() { + for name in self.blacklist.iter() { + for (k, v) in matches.flags.iter() { + if k == name { + self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments", + if let Some(s) = v.short { + format!("-{}", s) + } else if let Some(l) = v.long { + format!("--{}", l) + } else { + format!("\"{}\"", v.name) + }), + true, true); + } + } + for (k, v) in matches.opts.iter() { + if k == name { + self.report_error(format!("The argument {} is mutually exclusive with one or more other arguments", + if let Some(s) = v.short { + format!("-{}", s) + } else if let Some(l) = v.long { + format!("--{}", l) + } else { + format!("\"{}\"", v.name) + }), + true, true); + } + } + for (k, v) in matches.positionals.iter() { + if k == name { + self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments",v.name), + false, true); + } + } + } + } + } - fn create_help_and_version(&mut self) { - if self.needs_long_help { - self.flags.insert("clap_help", FlagArg{ - name: "clap_help", - short: if self.needs_short_help { Some('h') } else { None }, - long: Some("help"), - help: Some("Prints this message"), - blacklist: None, - multiple: false, - requires: None, - occurrences: 1 - }); - } - if self.needs_long_version { - self.flags.insert("clap_version", FlagArg{ - name: "clap_version", - short: if self.needs_short_help { Some('v') } else { None }, - long: Some("version"), - help: Some("Prints version information"), - blacklist: None, - multiple: false, - requires: None, - occurrences: 1 - }); - } - if self.needs_subcmd_help && ! self.subcommands.is_empty() { - self.subcommands.insert("help", Box::new(App::new("help").about("Prints this message"))); - } - } + fn create_help_and_version(&mut self) { + if self.needs_long_help { + self.flags.insert("clap_help", FlagArg{ + name: "clap_help", + short: if self.needs_short_help { Some('h') } else { None }, + long: Some("help"), + help: Some("Prints this message"), + blacklist: None, + multiple: false, + requires: None, + occurrences: 1 + }); + } + if self.needs_long_version { + self.flags.insert("clap_version", FlagArg{ + name: "clap_version", + short: if self.needs_short_help { Some('v') } else { None }, + long: Some("version"), + help: Some("Prints version information"), + blacklist: None, + multiple: false, + requires: None, + occurrences: 1 + }); + } + if self.needs_subcmd_help && ! self.subcommands.is_empty() { + self.subcommands.insert("help", Box::new(App::new("help").about("Prints this message"))); + } + } - fn get_matches_from(&mut self, matches: &mut ArgMatches, it: &mut IntoIter<String>) { - self.create_help_and_version(); + fn get_matches_from(&mut self, matches: &mut ArgMatches, it: &mut IntoIter<String>) { + self.create_help_and_version(); - // let mut needs_val = false; - let mut subcmd_name: Option<&'static str> = None; - let mut needs_val_of: Option<&'static str> = None; - let mut pos_counter = 1; - while let Some(arg) = it.next() { - let arg_slice = arg.as_slice(); - let mut skip = false; - if let Some(nvo) = needs_val_of { - if let Some(ref opt) = self.opts.get(nvo) { - if self.blacklist.contains(opt.name) { - self.report_error( - format!("The argument {} is mutually exclusive with one or more other arguments", - if let Some(long) = opt.long { - format!("--{}",long) - }else{ - format!("-{}",opt.short.unwrap()) - }),true, true); - } - matches.opts.insert(nvo, OptArg{ - name: opt.name, - short: opt.short, - long: opt.long, - help: opt.help, - requires: None, - blacklist: None, - required: opt.required, - value: Some(arg.clone()) - }); - if let Some(ref bl) = opt.blacklist { - if ! bl.is_empty() { - for name in bl.iter() { - self.blacklist.insert(name); - } - } - } - if self.required.contains(opt.name) { - self.required.remove(opt.name); - } - if let Some(ref reqs) = opt.requires { - if ! reqs.is_empty() { - for n in reqs.iter() { - if matches.opts.contains_key(n) { continue; } - if matches.flags.contains_key(n) { continue; } - if matches.positionals.contains_key(n) { continue; } - self.required.insert(n); - } - } - } - skip = true; - } - } - if skip { - needs_val_of = None; - continue; - } - if arg_slice.starts_with("--") { - // Single flag, or option long version - needs_val_of = self.parse_long_arg(matches, &arg); + // let mut needs_val = false; + let mut subcmd_name: Option<&'static str> = None; + let mut needs_val_of: Option<&'static str> = None; + let mut pos_counter = 1; + while let Some(arg) = it.next() { + let arg_slice = arg.as_slice(); + let mut skip = false; + if let Some(nvo) = needs_val_of { + if let Some(ref opt) = self.opts.get(nvo) { + if self.blacklist.contains(opt.name) { + self.report_error( + format!("The argument {} is mutually exclusive with one or more other arguments", + if let Some(long) = opt.long { + format!("--{}",long) + }else{ + format!("-{}",opt.short.unwrap()) + }),true, true); + } + matches.opts.insert(nvo, OptArg{ + name: opt.name, + short: opt.short, + long: opt.long, + help: opt.help, + requires: None, + blacklist: None, + required: opt.required, + value: Some(arg.clone()) + }); + if let Some(ref bl) = opt.blacklist { + if ! bl.is_empty() { + for name in bl.iter() { + self.blacklist.insert(name); + } + } + } + if self.required.contains(opt.name) { + self.required.remove(opt.name); + } + if let Some(ref reqs) = opt.requires { + if ! reqs.is_empty() { + for n in reqs.iter() { + if matches.opts.contains_key(n) { continue; } + if matches.flags.contains_key(n) { continue; } + if matches.positionals.contains_key(n) { continue; } + self.required.insert(n); + } + } + } + skip = true; + } + } + if skip { + needs_val_of = None; + continue; + } + if arg_slice.starts_with("--") { + // Single flag, or option long version + needs_val_of = self.parse_long_arg(matches, &arg); - } else if arg_slice.starts_with("-") { - needs_val_of = self.parse_short_arg(matches, &arg); - } else { - // Positional or Subcommand - if let Some(sca) = self.subcommands.get(arg_slice) { - if sca.name == "help" { - self.print_help(); - } - subcmd_name = Some(sca.name); - break; - } + } else if arg_slice.starts_with("-") { + needs_val_of = self.parse_short_arg(matches, &arg); + } else { + // Positional or Subcommand + if let Some(sca) = self.subcommands.get(arg_slice) { + if sca.name == "help" { + self.print_help(); + } + subcmd_name = Some(sca.name); + break; + } - if self.positionals_idx.is_empty() { // || self.positionals_name.is_empty() { - self.report_error( - format!("Found positional argument {}, but {} doesn't accept any", arg, self.name), - true, true); - } - if let Some(ref p) = self.positionals_idx.get(&pos_counter) { - if self.blacklist.contains(p.name) { - self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments", arg), - true, true); - } - matches.positionals.insert(p.name, PosArg{ - name: p.name, - help: p.help, - required: p.required, - blacklist: None, - requires: None, - value: Some(arg.clone()), - index: pos_counter - }); - if let Some(ref bl) = p.blacklist { - if ! bl.is_empty() { - for name in bl.iter() { - self.blacklist.insert(name); - } - } - } - if self.required.contains(p.name) { - self.required.remove(p.name); - } - if let Some(ref reqs) = p.requires { - if ! reqs.is_empty() { - for n in reqs.iter() { - if matches.opts.contains_key(n) { continue; } - if matches.flags.contains_key(n) { continue; } - if matches.positionals.contains_key(n) { continue; } - self.required.insert(n); - } - } - } - pos_counter += 1; - } else { - self.report_error(format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), true, true); - } - } - } + if self.positionals_idx.is_empty() { // || self.positionals_name.is_empty() { + self.report_error( + format!("Found positional argument {}, but {} doesn't accept any", arg, self.name), + true, true); + } + if let Some(ref p) = self.positionals_idx.get(&pos_counter) { + if self.blacklist.contains(p.name) { + self.report_error(format!("The argument \"{}\" is mutually exclusive with one or more other arguments", arg), + true, true); + } + matches.positionals.insert(p.name, PosArg{ + name: p.name, + help: p.help, + required: p.required, + blacklist: None, + requires: None, + value: Some(arg.clone()), + index: pos_counter + }); + if let Some(ref bl) = p.blacklist { + if ! bl.is_empty() { + for name in bl.iter() { + self.blacklist.insert(name); + } + } + } + if self.required.contains(p.name) { + self.required.remove(p.name); + } + if let Some(ref reqs) = p.requires { + if ! reqs.is_empty() { + for n in reqs.iter() { + if matches.opts.contains_key(n) { continue; } + if matches.flags.contains_key(n) { continue; } + if matches.positionals.contains_key(n) { continue; } + self.required.insert(n); + } + } + } + pos_counter += 1; + } else { + self.report_error(format!("Positional argument \"{}\" was found, but {} wasn't expecting any", arg, self.name), true, true); + } + } + } - match needs_val_of { - Some(ref a) => { - self.report_error( - format!("Argument \"{}\" requires a value but none was supplied", a), - true, true); - } - _ => {} - } - if ! self.required.is_empty() { - self.report_error("One or more required arguments were not supplied".to_string(), - true, true); - } + match needs_val_of { + Some(ref a) => { + self.report_error( + format!("Argument \"{}\" requires a value but none was supplied", a), + true, true); + } + _ => {} + } + if ! self.required.is_empty() { + self.report_error("One or more required arguments were not supplied".to_string(), + true, true); + } - self.validate_blacklist(&matches); + self.validate_blacklist(&matches); - if let Some(sc_name) = subcmd_name { - if let Some(ref mut sc) = self.subcommands.get_mut(sc_name) { - let mut new_matches = ArgMatches::new(sc_name); - sc.get_matches_from(&mut new_matches, it); - matches.subcommand = Some((sc_name, Box::new(SubCommand{ - name: sc_name, - matches: new_matches}))); - } - } - } + if let Some(sc_name) = subcmd_name { + if let Some(ref mut sc) = self.subcommands.get_mut(sc_name) { + let mut new_matches = ArgMatches::new(sc_name); + sc.get_matches_from(&mut new_matches, it); + matches.subcommand = Some((sc_name, Box::new(SubCommand{ + name: sc_name, + matches: new_matches}))); + } + } + } - pub fn get_matches(mut self) -> ArgMatches { - let mut matches = ArgMatches::new(self.name); + pub fn get_matches(mut self) -> ArgMatches { + let mut matches = ArgMatches::new(self.name); - let args = env::args().collect::<Vec<_>>(); - let mut it = args.into_iter(); - if let Some(name) = it.next() { - let p = Path::new(&name[..]); - if let Some(f) = p.file_name() { - match f.to_os_string().into_string() { - Ok(s) => self.bin_name = Some(s), - Err(_) => {} - } - } - } - self.get_matches_from(&mut matches, &mut it ); + let args = env::args().collect::<Vec<_>>(); + let mut it = args.into_iter(); + if let Some(name) = it.next() { + let p = Path::new(&name[..]); + if let Some(f) = p.file_name() { + match f.to_os_string().into_string() { + Ok(s) => self.bin_name = Some(s), + Err(_) => {} + } + } + } + self.get_matches_from(&mut matches, &mut it ); - matches - } + matches + } } diff --git a/docs/src/clap/arg.rs/arg.rs.html b/docs/src/clap/arg.rs/arg.rs.html index 0e48e089..48afd778 100644 --- a/docs/src/clap/arg.rs/arg.rs.html +++ b/docs/src/clap/arg.rs/arg.rs.html @@ -400,7 +400,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` @@ -427,295 +427,295 @@ /// 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 + } } diff --git a/docs/src/clap/argmatches.rs/argmatches.rs.html b/docs/src/clap/argmatches.rs/argmatches.rs.html index b7120321..98540824 100644 --- a/docs/src/clap/argmatches.rs/argmatches.rs.html +++ b/docs/src/clap/argmatches.rs/argmatches.rs.html @@ -325,15 +325,15 @@ /// # 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 @@ -348,19 +348,19 @@ /// 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) @@ -375,7 +375,7 @@ /// 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; } } @@ -385,7 +385,7 @@ return true; } false - } + } /// Checks the number of occurrences of a flag at runtime. /// diff --git a/docs/src/clap/lib.rs/lib.rs.html b/docs/src/clap/lib.rs/lib.rs.html index 58df1bb0..9e1963a6 100644 --- a/docs/src/clap/lib.rs/lib.rs.html +++ b/docs/src/clap/lib.rs/lib.rs.html @@ -220,50 +220,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... //! ``` @@ -277,22 +277,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; @@ -310,46 +310,46 @@ 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); + } } diff --git a/docs/src/clap/subcommand.rs/subcommand.rs.html b/docs/src/clap/subcommand.rs/subcommand.rs.html index 66b3ade3..24316236 100644 --- a/docs/src/clap/subcommand.rs/subcommand.rs.html +++ b/docs/src/clap/subcommand.rs/subcommand.rs.html @@ -101,31 +101,31 @@ /// # 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) + } }