clap/examples/07_option_args.md
Ed Page bfa02fd418 test: More thoroughly test examples
This ports our example testing over to [trycmd](https://docs.rs/) so
we can:
- More thoroughly test our examples
- Provide always-up-to-date example usage

The old way of testing automatically picked up examples.  This new way
requires we have a `.md` file that uses the example in some way.

Notes:
- Moved overall example description to the `.md` file
- I added cross-linking between related examples
- `14_groups` had a redundant paragraph (twice talked about "one and
  only one"
2021-11-23 13:13:41 -06:00

1.9 KiB

Option arguments are those that take an additional value, such as "-c value". In clap they support three types of specification, those with short() as "-o some", or those with long() as "--option value" or "--option=value"

Options also support a multiple setting, which is discussed in the example below.

Let's look at their help:

$ 07_option_args --help
MyApp 

USAGE:
    07_option_args[EXE] [OPTIONS] --input <input> [output]

ARGS:
    <output>    the output file to use

OPTIONS:
    -c, --config <FILE>    the config file to use
    -h, --help             Print help information
    -i, --input <input>    the input file to use

First, we see that --input is required:

$ 07_option_args
? failed
error: The following required arguments were not provided:
    --config <FILE>
    --input <input>

USAGE:
    07_option_args[EXE] [OPTIONS] --input <input> [output]

For more information try --help

But --input also requires --config:

$ 07_option_args --input input.txt --input another.txt
? failed
error: The following required arguments were not provided:
    --config <FILE>

USAGE:
    07_option_args[EXE] [OPTIONS] --input <input> --config <FILE> [output]

For more information try --help

Everything works now that we specify both:

$ 07_option_args --input input.txt --input another.txt --config config.toml
An input file was specified
An input file: input.txt
An input file: input.txt
An input file: another.txt
The "input" argument was used 2 times

But we can't mix this with output:

$ 07_option_args --input input.txt --input another.txt --config config.toml output.txt
? failed
error: The argument '<output>' cannot be used with '--input <input>'

USAGE:
    07_option_args[EXE] --input <input> --input <input> --config <FILE>

For more information try --help

That requires passing it in by itself:

$ 07_option_args output.txt
The "input" argument was used 0 times