Skip to content

Commit 960389d

Browse files
committed
docs(README): adds details about optional and new features
1 parent 90e7b08 commit 960389d

File tree

2 files changed

+84
-29
lines changed

2 files changed

+84
-29
lines changed

README.md

+49-22
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ Below are a few of the features which `clap` supports, full descriptions and usa
5050
* **Specific Value Sets**: Positional or Option Arguments can optionally define a specific set of allowed values (i.e. imagine a `--mode` option which may *only* have one of two values `fast` or `slow` such as `--mode fast` or `--mode slow`)
5151
* **Default Values**: Although not specifically provided by `clap` you can achieve this exact functionality from Rust's `Option<&str>.unwrap_or("some default")` method (or `Result<T,String>.unwrap_or(T)` when using typed values)
5252
* **Automatic Version from Cargo.toml**: `clap` is fully compatible with Rust's `env!()` macro for automatically setting the version of your application to the version in your Cargo.toml. See `examples/09_AutoVersion.rs` for how to do this (Thanks to [jhelwig](https://github.com/jhelwig) for pointing this out)
53-
* **Typed Values**: You can use several convenience macros provided by `clap` to get typed values (i.e. `i32`, `u8`, etc.) from positional or option arguments so long as the type you request implements `std::str::FromStr` See the `examples/12_TypedValues.rs`. You can also use `clap`s `simple_enum!` or `arg_enum!` macro to create an enum with variants that automatically implements `std::str::FromStr`. See `examples/13a_EnumValuesAutomatic.rs` for details.
53+
* **Typed Values**: You can use several convenience macros provided by `clap` to get typed values (i.e. `i32`, `u8`, etc.) from positional or option arguments so long as the type you request implements `std::str::FromStr` See the `examples/12_TypedValues.rs`. You can also use `clap`s `simple_enum!` or `arg_enum!` macro to create an enum with variants that automatically implements `std::str::FromStr`. See `examples/13a_EnumValuesAutomatic.rs` for details and performs an ascii case insensitive parse from a `string`->`enum`.
54+
* **Suggestions**: Suggests corrections when the user enter's a typo. For example, if you defined a `--myoption <value>` argument, and the user mistakenly typed `--moyption value` (notice `y` and `o` switched), they would receive a `Did you mean '--myoption' ?` error and exit gracefully. This also works for subcommands and flags. (Thanks to [Byron](https://github.com/Byron) for the implementation) (This feature can optionally be disabled, see 'Optional Dependencies / Features')
55+
* **Colorized (Red) Errors**: Error message are printed in red text (this feature can optionally be disabled, see 'Optional Dependencies / Features').
5456

5557
## Quick Example
56-
58+
5759
The following two examples show a quick example of some of the very basic functionality of `clap`. For more advanced usage, such as requirements, exclusions, groups, multiple values and occurrences see the [video tutorials](https://www.youtube.com/playlist?list=PLza5oFLQGTl0Bc_EU_pBNcX-rhVqDTRxv), [documentation](http://kbknapp.github.io/clap-rs/clap/index.html), or `examples/` directory of this repository.
58-
60+
5961
*NOTE:* Both examples are functionally the same, but show two different styles in which to use `clap`
60-
62+
6163
```rust
6264
// (Full example with detailed comments in examples/01a_QuickExample.rs)
6365
//
@@ -85,21 +87,21 @@ fn main() {
8587
// Calling .unwrap() is safe here because "INPUT" is required (if "INPUT" wasn't
8688
// required we could have used an 'if let' to conditionally get the value)
8789
println!("Using input file: {}", matches.value_of("INPUT").unwrap());
88-
90+
8991
// Gets a value for config if supplied by user, or defaults to "default.conf"
9092
let config = matches.value_of("CONFIG").unwrap_or("default.conf");
9193
println!("Value for config: {}", config);
92-
94+
9395
// Vary the output based on how many times the user used the "debug" flag
94-
// (i.e. 'myapp -d -d -d' or 'myapp -ddd' vs 'myapp -d'
96+
// (i.e. 'myapp -d -d -d' or 'myapp -ddd' vs 'myapp -d'
9597
match matches.occurrences_of("debug") {
9698
0 => println!("Debug mode is off"),
9799
1 => println!("Debug mode is kind of on"),
98100
2 => println!("Debug mode is on"),
99101
3 | _ => println!("Don't be crazy"),
100102
}
101103

102-
// You can information about subcommands by requesting their matches by name
104+
// You can information about subcommands by requesting their matches by name
103105
// (as below), requesting just the name used, or both at the same time
104106
if let Some(matches) = matches.subcommand_matches("test") {
105107
if matches.is_present("verbose") {
@@ -114,11 +116,11 @@ fn main() {
114116
```
115117

116118
The following example is functionally the same as the one above, but this method allows more advanced configuration options (not shown in this small example), or even dynamically generating arguments when desired. Both methods can be used together to get the best of both worlds (see the documentation, examples, or video tutorials).
117-
119+
118120
```rust
119121
// (Full example with detailed comments in examples/01b_QuickExample.rs)
120122
//
121-
// This example demonstrates clap's full 'builder pattern' style of creating arguments which is
123+
// This example demonstrates clap's full 'builder pattern' style of creating arguments which is
122124
// more verbose, but allows easier editting, and at times more advanced options, or the possibility
123125
// to generate arguments dynamically.
124126
extern crate clap;
@@ -154,21 +156,21 @@ fn main() {
154156
// Calling .unwrap() is safe here because "INPUT" is required (if "INPUT" wasn't
155157
// required we could have used an 'if let' to conditionally get the value)
156158
println!("Using input file: {}", matches.value_of("INPUT").unwrap());
157-
159+
158160
// Gets a value for config if supplied by user, or defaults to "default.conf"
159161
let config = matches.value_of("CONFIG").unwrap_or("default.conf");
160162
println!("Value for config: {}", config);
161-
163+
162164
// Vary the output based on how many times the user used the "debug" flag
163-
// (i.e. 'myapp -d -d -d' or 'myapp -ddd' vs 'myapp -d'
165+
// (i.e. 'myapp -d -d -d' or 'myapp -ddd' vs 'myapp -d'
164166
match matches.occurrences_of("debug") {
165167
0 => println!("Debug mode is off"),
166168
1 => println!("Debug mode is kind of on"),
167169
2 => println!("Debug mode is on"),
168170
3 | _ => println!("Don't be crazy"),
169171
}
170-
171-
// You can information about subcommands by requesting their matches by name
172+
173+
// You can information about subcommands by requesting their matches by name
172174
// (as below), requesting just the name used, or both at the same time
173175
if let Some(matches) = matches.subcommand_matches("test") {
174176
if matches.is_present("verbose") {
@@ -197,7 +199,7 @@ FLAGS:
197199
-d Turn debugging information on
198200
-h, --help Prints this message
199201
-v, --version Prints version information
200-
202+
201203
OPTIONS:
202204
-c, --config <CONFIG> Sets a custom config file
203205

@@ -227,7 +229,7 @@ To try out the pre-built example use the following stes:
227229
To test out `clap`'s default auto-generated help/version follow these steps:
228230
* Create a new cargo project `$ cargo new fake --bin && cd fake`
229231
* Add `clap` to your `Cargo.toml`
230-
*
232+
*
231233
```toml
232234
[dependencies]
233235
clap = "*"
@@ -268,6 +270,32 @@ Define a list of valid arguments for your program (see the [documentation](https
268270

269271
Then run `cargo build` or `cargo update && cargo build` for your project.
270272

273+
### Optional Dependencies / Features
274+
275+
If you'd like to keep your dependency list to **only** `clap`, you can disable any features that require an additional dependency. To do this, add this to your `Cargo.toml`:
276+
277+
```toml
278+
[dependencies.clap]
279+
version = "*"
280+
default-features = false
281+
```
282+
283+
You can also selectively enable only the features you'd like to include, by adding:
284+
285+
```toml
286+
[dependencies.clap]
287+
version = "*"
288+
default-features = false
289+
290+
# Cherry-pick the features you'd like to use
291+
features = [ "suggestions", "color" ]
292+
```
293+
294+
The following is a list of optional `clap` features:
295+
296+
* **"suggestions"**: Turns on the `Did you mean '--myoption' ?` feature for when users make typos.
297+
* **"color"**: Turns on red error messages.
298+
271299
### More Information
272300

273301
You can find complete documentation on the [github-pages site](http://kbknapp.github.io/clap-rs/clap/index.html) for this project.
@@ -292,17 +320,16 @@ Contributions are always welcome! And there is a multitude of ways in which you
292320
4. Make your changes, and commit (`git commit -am "your message"`) (I try to use a [conventional](https://github.com/ajoslin/conventional-changelog/blob/master/CONVENTIONS.md) changelog format so I can update it using [clog](https://github.com/thoughtram/clog))
293321
5. If applicable, run the tests (See below)
294322
6. Push your changes back to your fork (`git push origin your-branch`)
295-
7. Create a pull request! (You can also create the pull request right away, and we'll merge when ready. This a good way to discuss proposed changes.)
323+
7. Create a pull request! (You can also create the pull request right away, and we'll merge when ready. This a good way to discuss proposed changes.)
296324

297325
Another really great way to help is if you find an interesting, or helpful way in which to use `clap`. You can either add it to the `examples/` directory, or file an issue and tell me. I'm all about giving credit where credit is due :)
298326

299327
### Running the tests
300328

301-
If contributing, you can run the tests as follows (assuming you've cloned the repo to `clap-rs/`
329+
If contributing, you can run the tests as follows (assuming you're in the `clap-rs` directory)
302330

303331
```
304-
cd clap-rs && cargo test
305-
cd clap-tests && make test
332+
cargo test && make -C clap-tests test
306333
```
307334

308335
### Goals
@@ -331,7 +358,7 @@ Although I do my best to keep breaking changes to a minimum, being that this a s
331358
- `Arg::possible_values()`, `Arg::value_names()`, `Arg::requires_all()`, `Arg::mutually_excludes_all()` [deprecated], `Arg::conflicts_with_all()`
332359
+ No longer take a `Vec<&str>`, instead they take a generic `IntoIterator<Item=AsRef<str>>` which means you cannot use an inline `vec![]` but it means the methods are now far more flexible, especially for dynamic value generation.
333360
+ Instead use something that conforms to the `IntoIterator` trait, or something like:
334-
361+
335362
```rust
336363
let my_vals = ["value1", "value2", "value3"];
337364
...

src/lib.rs

+35-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![crate_type= "lib"]
22

3+
// DOCS
34
//! # clap
45
//!
56
//! ![Travis-CI](https://travis-ci.org/kbknapp/clap-rs.svg?branch=master) [![Crates.io](https://img.shields.io/crates/v/clap.svg)]() [![Crates.io](https://img.shields.io/crates/l/clap.svg)]() [![Join the chat at https://gitter.im/kbknapp/clap-rs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/kbknapp/clap-rs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
@@ -18,7 +19,7 @@
1819
//!
1920
//! ## Features
2021
//!
21-
//! Below are a few of the features which `clap` supports, full descriptions and usage can be found in the [documentation](http://kbknapp.github.io/clap-rs/docs/clap/index.html) and `examples/` directory
22+
//! Below are a few of the features which `clap` supports, full descriptions and usage can be found in the [documentation](http://kbknapp.github.io/clap-rs/clap/index.html) and `examples/` directory
2223
//!
2324
//! * **Auto-generated Help, Version, and Usage information**
2425
//! - Can optionally be fully, or partially overridden if you want a custom help, version, or usag
@@ -52,11 +53,13 @@
5253
//! * **Specific Value Sets**: Positional or Option Arguments can optionally define a specific set of allowed values (i.e. imagine a `--mode` option which may *only* have one of two values `fast` or `slow` such as `--mode fast` or `--mode slow`)
5354
//! * **Default Values**: Although not specifically provided by `clap` you can achieve this exact functionality from Rust's `Option<&str>.unwrap_or("some default")` method (or `Result<T,String>.unwrap_or(T)` when using typed values)
5455
//! * **Automatic Version from Cargo.toml**: `clap` is fully compatible with Rust's `env!()` macro for automatically setting the version of your application to the version in your Cargo.toml. See `examples/09_AutoVersion.rs` for how to do this (Thanks to [jhelwig](https://github.com/jhelwig) for pointing this out)
55-
//! * **Typed Values**: You can use several convenience macros provided by `clap` to get typed values (i.e. `i32`, `u8`, etc.) from positional or option arguments so long as the type you request implements `std::str::FromStr` See the `examples/12_TypedValues.rs`. You can also use `clap`s `simple_enum!` or `arg_enum!` macro to create an enum with variants that automatically implements `std::str::FromStr`. See `examples/13a_EnumValuesAutomatic.rs` for details.
56+
//! * **Typed Values**: You can use several convenience macros provided by `clap` to get typed values (i.e. `i32`, `u8`, etc.) from positional or option arguments so long as the type you request implements `std::str::FromStr` See the `examples/12_TypedValues.rs`. You can also use `clap`s `simple_enum!` or `arg_enum!` macro to create an enum with variants that automatically implements `std::str::FromStr`. See `examples/13a_EnumValuesAutomatic.rs` for details and performs an ascii case insensitive parse from a `string`->`enum`.
57+
//! * **Suggestions**: Suggests corrections when the user enter's a typo. For example, if you defined a `--myoption <value>` argument, and the user mistakenly typed `--moyption value` (notice `y` and `o` switched), they would receive a `Did you mean '--myoption' ?` error and exit gracefully. This also works for subcommands and flags. (Thanks to [Byron](https://github.com/Byron) for the implementation) (This feature can optionally be disabled, see 'Optional Dependencies / Features')
58+
//! * **Colorized (Red) Errors**: Error message are printed in red text (this feature can optionally be disabled, see 'Optional Dependencies / Features').
5659
//!
5760
//! ## Quick Example
5861
//!
59-
//! The following two examples show a quick example of some of the very basic functionality of `clap`. For more advanced usage, such as requirements, exclusions, groups, multiple values and occurrences see the [video tutorials](https://www.youtube.com/playlist?list=PLza5oFLQGTl0Bc_EU_pBNcX-rhVqDTRxv), [documentation](http://kbknapp.github.io/clap-rs/docs/clap/index.html), or `examples/` directory of this repository.
62+
//! The following two examples show a quick example of some of the very basic functionality of `clap`. For more advanced usage, such as requirements, exclusions, groups, multiple values and occurrences see the [video tutorials](https://www.youtube.com/playlist?list=PLza5oFLQGTl0Bc_EU_pBNcX-rhVqDTRxv), [documentation](http://kbknapp.github.io/clap-rs/clap/index.html), or `examples/` directory of this repository.
6063
//!
6164
//! *NOTE:* Both examples are functionally the same, but show two different styles in which to use `clap`
6265
//!
@@ -270,9 +273,35 @@
270273
//!
271274
//! Then run `cargo build` or `cargo update && cargo build` for your project.
272275
//!
276+
//! ### Optional Dependencies / Features
277+
//!
278+
//! If you'd like to keep your dependency list to **only** `clap`, you can disable any features that require an additional dependency. To do this, add this to your `Cargo.toml`:
279+
//!
280+
//! ```ignore
281+
//! [dependencies.clap]
282+
//! version = "*"
283+
//! default-features = false
284+
//! ```
285+
//!
286+
//! You can also selectively enable only the features you'd like to include, by adding:
287+
//!
288+
//! ```ignore
289+
//! [dependencies.clap]
290+
//! version = "*"
291+
//! default-features = false
292+
//!
293+
//! # Cherry-pick the features you'd like to use
294+
//! features = [ "suggestions", "color" ]
295+
//! ```
296+
//!
297+
//! The following is a list of optional `clap` features:
298+
//!
299+
//! * **"suggestions"**: Turns on the `Did you mean '--myoption' ?` feature for when users make typos.
300+
//! * **"color"**: Turns on red error messages.
301+
//!
273302
//! ### More Information
274303
//!
275-
//! You can find complete documentation on the [github-pages site](http://kbknapp.github.io/clap-rs/docs/clap/index.html) for this project.
304+
//! You can find complete documentation on the [github-pages site](http://kbknapp.github.io/clap-rs/clap/index.html) for this project.
276305
//!
277306
//! You can also find usage examples in the `examples/` directory of this repo.
278307
//!
@@ -300,11 +329,10 @@
300329
//!
301330
//! ### Running the tests
302331
//!
303-
//! If contributing, you can run the tests as follows (assuming you've cloned the repo to `clap-rs/`
332+
//! If contributing, you can run the tests as follows (assuming you're in the `clap-rs/` directory)
304333
//!
305334
//! ```ignore
306-
//! cd clap-rs && cargo test
307-
//! cd clap-tests && make test
335+
//! cargo test && make -C clap-tests test
308336
//! ```
309337
//!
310338
//! ### Goals

0 commit comments

Comments
 (0)