Skip to content

Commit

Permalink
docs: use automatic version and author parsing in examples
Browse files Browse the repository at this point in the history
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
gibfahn committed Nov 26, 2021
1 parent 6fae3f1 commit 4704562
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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'")
Expand Down
6 changes: 3 additions & 3 deletions examples/01a_quick_example.rs
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
Expand Down Expand Up @@ -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'")
Expand Down
6 changes: 3 additions & 3 deletions examples/01b_quick_example.rs
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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 4704562

Please sign in to comment.