2015-02-28 03:19:48 +00:00
|
|
|
#![crate_type= "lib"]
|
|
|
|
|
2015-03-18 12:41:09 +00:00
|
|
|
#![feature(collections, libc, exit_status)]
|
2015-02-26 21:25:40 +00:00
|
|
|
|
2015-02-28 03:19:48 +00:00
|
|
|
//! A simply library for parsing command line arguments when writing
|
2015-02-28 14:40:53 +00:00
|
|
|
//! command line and console applications.
|
2015-02-28 18:13:43 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! 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.
|
|
|
|
//!
|
|
|
|
//! 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.
|
|
|
|
//!
|
2015-03-16 18:53:49 +00:00
|
|
|
//! Example:
|
2015-02-28 18:13:43 +00:00
|
|
|
//!
|
2015-03-01 21:58:39 +00:00
|
|
|
//! ```no_run
|
2015-03-16 01:05:19 +00:00
|
|
|
//! use clap::{Arg, App, SubCommand};
|
2015-02-28 18:13:43 +00:00
|
|
|
//!
|
|
|
|
//! // ...
|
|
|
|
//!
|
|
|
|
//! let matches = App::new("MyApp")
|
2015-03-16 18:47:09 +00:00
|
|
|
//! .version("1.0")
|
|
|
|
//! .author("Kevin K. <kbknapp@gmail.com>")
|
|
|
|
//! .about("Does awesome things")
|
|
|
|
//! .arg(Arg::new("config")
|
2015-03-16 19:56:42 +00:00
|
|
|
//! .short("c")
|
|
|
|
//! .long("config")
|
|
|
|
//! .help("Sets a custom config file")
|
|
|
|
//! .takes_value(true))
|
2015-03-16 18:47:09 +00:00
|
|
|
//! .arg(Arg::new("output")
|
2015-03-16 19:56:42 +00:00
|
|
|
//! .help("Sets an optional output file")
|
|
|
|
//! .index(1))
|
2015-03-16 18:47:09 +00:00
|
|
|
//! .arg(Arg::new("debug")
|
2015-03-16 19:56:42 +00:00
|
|
|
//! .short("d")
|
|
|
|
//! .multiple(true)
|
|
|
|
//! .help("Turn debugging information on"))
|
2015-03-16 18:47:09 +00:00
|
|
|
//! .subcommand(SubCommand::new("test")
|
2015-03-16 19:56:42 +00:00
|
|
|
//! .about("Has test sub functionality")
|
|
|
|
//! .arg(Arg::new("verbose")
|
|
|
|
//! .short("v")
|
|
|
|
//! .help("Display verbose information")))
|
2015-03-16 18:47:09 +00:00
|
|
|
//! .get_matches();
|
2015-02-28 18:13:43 +00:00
|
|
|
//!
|
2015-03-16 18:47:09 +00:00
|
|
|
//! if let Some(o) = matches.value_of("output") {
|
|
|
|
//! println!("Value for output: {}", o);
|
|
|
|
//! }
|
2015-02-28 18:13:43 +00:00
|
|
|
//!
|
2015-03-16 18:47:09 +00:00
|
|
|
//! if let Some(c) = matches.value_of("config") {
|
|
|
|
//! println!("Value for config: {}", c);
|
|
|
|
//! }
|
2015-02-28 18:13:43 +00:00
|
|
|
//!
|
|
|
|
//! match matches.occurrences_of("debug") {
|
2015-03-16 18:47:09 +00:00
|
|
|
//! 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"),
|
2015-02-28 18:13:43 +00:00
|
|
|
//! }
|
2015-03-16 19:56:42 +00:00
|
|
|
//!
|
2015-03-15 21:17:52 +00:00
|
|
|
//! if let Some(ref matches) = matches.subcommand_matches("test") {
|
2015-03-16 18:47:09 +00:00
|
|
|
//! if matches.is_present("verbose") {
|
|
|
|
//! println!("Printing verbose test info...");
|
|
|
|
//! } else {
|
|
|
|
//! println!("Not printing regular test info...");
|
|
|
|
//! }
|
|
|
|
//! }
|
2015-03-15 21:17:52 +00:00
|
|
|
//!
|
2015-02-28 18:13:43 +00:00
|
|
|
//! // more porgram logic goes here...
|
|
|
|
//! ```
|
2015-02-28 18:18:47 +00:00
|
|
|
//!
|
|
|
|
//! If you were to compile the above program and run it with the flag `--help` or `-h` the following output woud be presented
|
|
|
|
//!
|
|
|
|
//! ```sh
|
|
|
|
//! $ myprog --help
|
|
|
|
//! MyApp 1.0
|
|
|
|
//! Kevin K. <kbknapp@gmail.com>
|
|
|
|
//! Does awesome things
|
|
|
|
//!
|
2015-02-28 18:49:33 +00:00
|
|
|
//! USAGE:
|
2015-03-16 18:47:09 +00:00
|
|
|
//! MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
|
2015-02-28 18:18:47 +00:00
|
|
|
//!
|
|
|
|
//! FLAGS:
|
2015-03-16 18:47:09 +00:00
|
|
|
//! -d Turn debugging information on
|
|
|
|
//! -h,--help Prints this message
|
|
|
|
//! -v,--version Prints version information
|
2015-02-28 18:18:47 +00:00
|
|
|
//!
|
|
|
|
//! OPTIONS:
|
2015-03-16 18:47:09 +00:00
|
|
|
//! -c,--config <config> Sets a custom config file
|
2015-02-28 18:49:33 +00:00
|
|
|
//!
|
2015-02-28 18:18:47 +00:00
|
|
|
//! POSITIONAL ARGUMENTS:
|
2015-03-16 18:47:09 +00:00
|
|
|
//! output Sets an optional output file
|
2015-03-15 21:17:52 +00:00
|
|
|
//!
|
|
|
|
//! SUBCOMMANDS:
|
2015-03-16 18:47:09 +00:00
|
|
|
//! help Prints this message
|
|
|
|
//! test Has test sub-functionality
|
2015-02-28 18:18:47 +00:00
|
|
|
//! ```
|
2015-02-28 03:19:48 +00:00
|
|
|
|
2015-02-28 15:45:31 +00:00
|
|
|
pub use argmatches::ArgMatches;
|
2015-02-25 13:37:25 +00:00
|
|
|
pub use arg::Arg;
|
|
|
|
pub use app::App;
|
2015-03-15 21:17:52 +00:00
|
|
|
pub use subcommand::SubCommand;
|
2015-02-25 13:37:25 +00:00
|
|
|
|
|
|
|
mod app;
|
|
|
|
mod argmatches;
|
|
|
|
mod arg;
|
|
|
|
mod args;
|
2015-03-06 03:44:11 +00:00
|
|
|
mod subcommand;
|
2015-03-03 18:57:19 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2015-03-03 19:19:44 +00:00
|
|
|
use super::*;
|
|
|
|
|
2015-03-16 18:47:09 +00:00
|
|
|
#[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);
|
|
|
|
}
|
2015-03-03 19:18:56 +00:00
|
|
|
}
|