clap/README.md

143 lines
4.7 KiB
Markdown
Raw Normal View History

2015-03-18 23:43:33 +00:00
# clap
![Travis-CI](https://travis-ci.org/kbknapp/clap-rs.svg?branch=master)
2015-02-25 13:36:33 +00:00
Command Line Argument Parser written in Rust
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
A simply library for parsing command line arguments and subcommands when writing command line and console applications.
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
You can use `clap` to lay out a list of possible valid command line arguments and subcommands, then let `clap` parse the string given by the user at runtime.
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
When using `clap` you define a set of parameters and rules for your arguments and subcommands, then at runtime `clap` will determine their validity.
`clap` also provides the traditional version and help switches 'for free' by parsing the list of possible valid arguments lazily at runtime, and if not already defined by the developer `clap` will autogenerate all applicable "help" and "version" switches (as well as a "help" subcommand if other subcommands are defined as well).
After defining a list of possible valid arguments and subcommands, `clap` gives you a list of valid matches that the user supplied at runtime, or informs the user of their error and exits gracefully. You can use this list to determine the functioning of your program.
2015-03-01 00:22:23 +00:00
2015-03-16 14:55:45 +00:00
## Quick Example
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
```rust
2015-03-21 15:28:39 +00:00
// (Full example with comments in examples/01_QuickExample.rs)
2015-03-16 14:38:43 +00:00
extern crate clap;
use clap::{Arg, App, SubCommand};
2015-03-01 00:22:23 +00:00
2015-03-16 14:55:45 +00:00
fn main() {
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("controls testing features")
.arg(Arg::new("verbose")
2015-03-18 16:54:31 +00:00
.short("v")
.help("print test information verbosely")))
.get_matches();
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);
}
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"),
}
if let Some(ref matches) = matches.subcommand_matches("test") {
2015-03-18 16:54:31 +00:00
if matches.is_present("verbose") {
println!("Printing verbosely...");
} else {
2015-03-18 16:54:31 +00:00
println!("Printing normally...");
}
}
// more porgram logic goes here...
2015-03-16 14:38:43 +00:00
}
```
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
If you were to compile the above program and run it with the flag `--help` or `-h` (or `help` subcommand, since we defined `test` as a subcommand) the following output woud be presented
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
```sh
$ myprog --help
MyApp 1.0
Kevin K. <kbknapp@gmail.com>
Does awesome things
USAGE:
MyApp [FLAGS] [OPTIONS] [POSITIONAL] [SUBCOMMANDS]
2015-03-16 14:38:43 +00:00
FLAGS:
-d Turn debugging information on
-h,--help Prints this message
-v,--version Prints version information
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
OPTIONS:
-c,--config <config> Sets a custom config file
2015-03-01 00:22:23 +00:00
2015-03-16 14:38:43 +00:00
POSITIONAL ARGUMENTS:
output Sets an optional output file
2015-03-15 21:25:36 +00:00
2015-03-16 14:38:43 +00:00
SUBCOMMANDS:
help Prints this message
test Controls testing features
2015-03-16 14:38:43 +00:00
```
## Installation
2015-03-16 14:38:43 +00:00
Add `clap` as a dependecy in your `Cargo.toml` file to use from crates.io:
```
[dependencies]
clap = "*"
```
2015-03-16 14:38:43 +00:00
Or track the latest on the master branch at github:
```
[dependencies.clap]
git = "https://github.com/kbknapp/clap-rs.git"
```
2015-03-16 14:38:43 +00:00
2015-03-01 16:28:42 +00:00
Then run `cargo build` or `cargo update` for your project.
2015-03-01 16:55:53 +00:00
2015-03-16 14:38:43 +00:00
## Usage
Add `extern crate clap;` to your crate root.
2015-03-01 16:55:53 +00:00
## More Information
2015-03-16 14:38:43 +00:00
2015-03-01 16:56:53 +00:00
You can find complete documentation on the [github-pages site](http://kbknapp.github.io/clap-rs/docs/clap/index.html) for this project.
2015-03-18 23:45:30 +00:00
2015-03-20 18:42:40 +00:00
You can also find full usage examples in the examples/ directory of this repo.
2015-03-18 23:45:30 +00:00
## How to build
### Running the tests
```
cargo test
```
### Building the documentation
Run this instead of `cargo doc` to generate the proper module docstring:
```
make doc
```