forked from clap-rs/clap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: use automatic version and author parsing in examples
Folks getting started with clap are likely to want to use the automatic version and author parsing initially. Hopefully those who do want to hard-code a value will note that that can be done as well from the SubCommand version parameter. Refs: clap-rs#3034
- Loading branch information
Showing
3 changed files
with
16 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,16 +133,18 @@ clap = "3.0.0-beta.5" | |
The first example shows the simplest way to use `clap`, by defining a struct. If you're familiar with the `structopt` crate you're in luck, it's the same! (In fact it's the exact same code running under the covers!) | ||
|
||
```rust,ignore | ||
// (Full example with detailed comments in examples/01d_quick_example.rs) | ||
// (Full example with detailed comments in clap_derive/examples/example.rs) | ||
// | ||
// This example demonstrates clap's full 'custom derive' style of creating arguments which is the | ||
// simplest method of use, but sacrifices some flexibility. | ||
use clap::{AppSettings, Parser}; | ||
/// This doc string acts as a help message when the user runs '--help' | ||
/// as do all doc strings on fields | ||
/// | ||
/// The version and author are parsed from the Cargo.toml unless specified. | ||
#[derive(Parser)] | ||
#[clap(version = "1.0", author = "Kevin K. <[email protected]>")] | ||
#[clap(version, author)] | ||
struct Opts { | ||
/// Sets a custom config file. Could have been an Option<T> with no default too | ||
#[clap(short, long, default_value = "default.conf")] | ||
|
@@ -211,12 +213,12 @@ This second method shows a method using the 'Builder Pattern' which allows more | |
// | ||
// This example demonstrates clap's "builder pattern" method of creating arguments | ||
// which the most flexible, but also most verbose. | ||
use clap::{Arg, App}; | ||
use clap::{App, Arg, crate_version, crate_authors}; | ||
fn main() { | ||
let matches = App::new("My Super Program") | ||
.version("1.0") | ||
.author("Kevin K. <[email protected]>") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about("Does awesome things") | ||
.arg(Arg::new("config") | ||
.short('c') | ||
|
@@ -283,12 +285,12 @@ The next example shows a far less verbose method, but sacrifices some of the adv | |
// | ||
// This example demonstrates clap's "usage strings" method of creating arguments | ||
// which is less verbose | ||
use clap::App; | ||
use clap::{App, crate_version, crate_authors}; | ||
fn main() { | ||
let matches = App::new("myapp") | ||
.version("1.0") | ||
.author("Kevin K. <[email protected]>") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about("Does awesome things") | ||
.arg("-c, --config=[FILE] 'Sets a custom config file'") | ||
.arg("<INPUT> 'Sets the input file to use'") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use clap::App; | ||
use clap::{crate_authors, crate_version, App}; | ||
|
||
fn main() { | ||
// This example shows how to create an application with several arguments using usage strings, which can be | ||
|
@@ -30,8 +30,8 @@ fn main() { | |
// - A subcommand "help" (automatically generated by clap because we specified a subcommand of our own) | ||
// + Used by "$ myapp help" (same functionality as "-h" or "--help") | ||
let matches = App::new("MyApp") | ||
.version("1.0") | ||
.author("Kevin K. <[email protected]>") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about("Does awesome things") | ||
.arg("-c, --config=[FILE] 'Sets a custom config file'") | ||
.arg("<output> 'Sets an optional output file'") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
use clap::{App, Arg}; | ||
use clap::{crate_authors, crate_version, App, Arg}; | ||
|
||
fn main() { | ||
// This method shows the traditional, and slightly more configurable way to set up arguments. This method is | ||
|
@@ -32,8 +32,8 @@ fn main() { | |
// - A subcommand "help" (automatically generated by clap because we specified a subcommand of our own) | ||
// + Used by "$ myapp help" (same functionality as "-h" or "--help") | ||
let matches = App::new("MyApp") | ||
.version("1.0") | ||
.author("Kevin K. <[email protected]>") | ||
.version(crate_version!()) | ||
.author(crate_authors!()) | ||
.about("Does awesome things") | ||
.arg( | ||
Arg::new("config") | ||
|