unstable-doc
only.Expand description
§Tutorial for the Derive API
See the side bar for the Table of Contents
§Quick Start
You can create an application declaratively with a struct
and some
attributes.
First, ensure clap
is available with the derive
feature flag:
$ cargo add clap --features derive
Here is a preview of the type of application you can make:
use std::path::PathBuf;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Optional name to operate on
name: Option<String>,
/// Sets a custom config file
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
/// Turn debugging information on
#[arg(short, long, action = clap::ArgAction::Count)]
debug: u8,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
/// does testing things
Test {
/// lists test values
#[arg(short, long)]
list: bool,
},
}
fn main() {
let cli = Cli::parse();
// You can check the value provided by positional arguments, or option arguments
if let Some(name) = cli.name.as_deref() {
println!("Value for name: {name}");
}
if let Some(config_path) = cli.config.as_deref() {
println!("Value for config: {}", config_path.display());
}
// You can see how many times a particular flag or argument occurred
// Note, only flags can have multiple occurrences
match cli.debug {
0 => println!("Debug mode is off"),
1 => println!("Debug mode is kind of on"),
2 => println!("Debug mode is on"),
_ => println!("Don't be crazy"),
}
// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Some(Commands::Test { list }) => {
if *list {
println!("Printing testing lists...");
} else {
println!("Not printing testing lists...");
}
}
None => {}
}
// Continued program logic goes here...
}
$ 01_quick_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 01_quick_derive[EXE] [OPTIONS] [NAME] [COMMAND]
Commands:
test does testing things
help Print this message or the help of the given subcommand(s)
Arguments:
[NAME] Optional name to operate on
Options:
-c, --config <FILE> Sets a custom config file
-d, --debug... Turn debugging information on
-h, --help Print help
-V, --version Print version
By default, the program does nothing:
$ 01_quick_derive
Debug mode is off
But you can mix and match the various features
$ 01_quick_derive -dd test
Debug mode is on
Not printing testing lists...
See also
- FAQ: When should I use the builder vs derive APIs?
- The cookbook for more application-focused examples
§Configuring the Parser
You use derive Parser
to start building a parser.
use clap::Parser;
#[derive(Parser)]
#[command(name = "MyApp")]
#[command(version = "1.0")]
#[command(about = "Does awesome things", long_about = None)]
struct Cli {
#[arg(long)]
two: String,
#[arg(long)]
one: String,
}
fn main() {
let cli = Cli::parse();
println!("two: {:?}", cli.two);
println!("one: {:?}", cli.one);
}
$ 02_apps_derive --help
Does awesome things
Usage: 02_apps_derive[EXE] --two <TWO> --one <ONE>
Options:
--two <TWO>
--one <ONE>
-h, --help Print help
-V, --version Print version
$ 02_apps_derive --version
MyApp 1.0
You can use #[command(version, about)]
attribute defaults on the struct to fill these fields in from your Cargo.toml
file.
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)] // Read from `Cargo.toml`
struct Cli {
#[arg(long)]
two: String,
#[arg(long)]
one: String,
}
fn main() {
let cli = Cli::parse();
println!("two: {:?}", cli.two);
println!("one: {:?}", cli.one);
}
$ 02_crate_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 02_crate_derive[EXE] --two <TWO> --one <ONE>
Options:
--two <TWO>
--one <ONE>
-h, --help Print help
-V, --version Print version
$ 02_crate_derive --version
clap [..]
You can use #[command]
attributes on the struct to change the application level behavior of clap. Any Command
builder function can be used as an attribute, like Command::next_line_help
.
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(next_line_help = true)]
struct Cli {
#[arg(long)]
two: String,
#[arg(long)]
one: String,
}
fn main() {
let cli = Cli::parse();
println!("two: {:?}", cli.two);
println!("one: {:?}", cli.one);
}
$ 02_app_settings_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 02_app_settings_derive[EXE] --two <TWO> --one <ONE>
Options:
--two <TWO>
--one <ONE>
-h, --help
Print help
-V, --version
Print version
§Adding Arguments
Arguments are inferred from the fields of your struct.
§Positionals
By default, struct fields define positional arguments:
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
name: Option<String>,
}
fn main() {
let cli = Cli::parse();
println!("name: {:?}", cli.name.as_deref());
}
$ 03_03_positional_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_03_positional_derive[EXE] [NAME]
Arguments:
[NAME]
Options:
-h, --help Print help
-V, --version Print version
$ 03_03_positional_derive
name: None
$ 03_03_positional_derive bob
name: Some("bob")
Note that the default ArgAction
is Set
. To
accept multiple values, override the action with Append
via Vec
:
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
name: Vec<String>,
}
fn main() {
let cli = Cli::parse();
println!("name: {:?}", cli.name);
}
$ 03_03_positional_mult_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_03_positional_mult_derive[EXE] [NAME]...
Arguments:
[NAME]...
Options:
-h, --help Print help
-V, --version Print version
$ 03_03_positional_mult_derive
name: []
$ 03_03_positional_mult_derive bob
name: ["bob"]
$ 03_03_positional_mult_derive bob john
name: ["bob", "john"]
§Options
You can name your arguments with a flag:
- Order doesn’t matter
- They can be optional
- Intent is clearer
To specify the flags for an argument, you can use #[arg(short = 'n')]
and/or
#[arg(long = "name")]
attributes on a field. When no value is given (e.g.
#[arg(short)]
), the flag is inferred from the field’s name.
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(short, long)]
name: Option<String>,
}
fn main() {
let cli = Cli::parse();
println!("name: {:?}", cli.name.as_deref());
}
$ 03_02_option_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_02_option_derive[EXE] [OPTIONS]
Options:
-n, --name <NAME>
-h, --help Print help
-V, --version Print version
$ 03_02_option_derive
name: None
$ 03_02_option_derive --name bob
name: Some("bob")
$ 03_02_option_derive --name=bob
name: Some("bob")
$ 03_02_option_derive -n bob
name: Some("bob")
$ 03_02_option_derive -n=bob
name: Some("bob")
$ 03_02_option_derive -nbob
name: Some("bob")
Note that the default ArgAction
is Set
. To
accept multiple occurrences, override the action with Append
via Vec
:
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(short, long)]
name: Vec<String>,
}
fn main() {
let cli = Cli::parse();
println!("name: {:?}", cli.name);
}
$ 03_02_option_mult_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_02_option_mult_derive[EXE] [OPTIONS]
Options:
-n, --name <NAME>
-h, --help Print help
-V, --version Print version
$ 03_02_option_mult_derive
name: []
$ 03_02_option_mult_derive --name bob
name: ["bob"]
$ 03_02_option_mult_derive --name bob --name john
name: ["bob", "john"]
$ 03_02_option_mult_derive --name bob --name=john -n tom -n=chris -nsteve
name: ["bob", "john", "tom", "chris", "steve"]
§Flags
Flags can also be switches that can be on/off:
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(short, long)]
verbose: bool,
}
fn main() {
let cli = Cli::parse();
println!("verbose: {:?}", cli.verbose);
}
$ 03_01_flag_bool_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_01_flag_bool_derive[EXE] [OPTIONS]
Options:
-v, --verbose
-h, --help Print help
-V, --version Print version
$ 03_01_flag_bool_derive
verbose: false
$ 03_01_flag_bool_derive --verbose
verbose: true
$ 03_01_flag_bool_derive --verbose --verbose
? failed
error: the argument '--verbose' cannot be used multiple times
Usage: 03_01_flag_bool_derive[EXE] [OPTIONS]
For more information, try '--help'.
Note that the default ArgAction
for a bool
field is
SetTrue
. To accept multiple flags, override the action with
Count
:
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
}
fn main() {
let cli = Cli::parse();
println!("verbose: {:?}", cli.verbose);
}
$ 03_01_flag_count_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_01_flag_count_derive[EXE] [OPTIONS]
Options:
-v, --verbose...
-h, --help Print help
-V, --version Print version
$ 03_01_flag_count_derive
verbose: 0
$ 03_01_flag_count_derive --verbose
verbose: 1
$ 03_01_flag_count_derive --verbose --verbose
verbose: 2
This also shows that anyArg
method may be used as an attribute.
§Subcommands
Subcommands are derived with #[derive(Subcommand)]
and be added via
#[command(subcommand)]
attribute on the field using that type.
Each instance of a Subcommand can have its own version, author(s), Args,
and even its own subcommands.
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Adds files to myapp
Add { name: Option<String> },
}
fn main() {
let cli = Cli::parse();
// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Commands::Add { name } => {
println!("'myapp add' was used, name is: {name:?}");
}
}
}
We used a struct-variant to define the add
subcommand.
Alternatively, you can use a struct for your subcommand’s arguments:
use clap::{Args, Parser, Subcommand};
#[derive(Parser)]
#[command(version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Adds files to myapp
Add(AddArgs),
}
#[derive(Args)]
struct AddArgs {
name: Option<String>,
}
fn main() {
let cli = Cli::parse();
// You can check for the existence of subcommands, and if found use their
// matches just as you would the top level cmd
match &cli.command {
Commands::Add(name) => {
println!("'myapp add' was used, name is: {:?}", name.name);
}
}
}
$ 03_04_subcommands_derive help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_04_subcommands_derive[EXE] <COMMAND>
Commands:
add Adds files to myapp
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
$ 03_04_subcommands_derive help add
Adds files to myapp
Usage: 03_04_subcommands_derive[EXE] add [NAME]
Arguments:
[NAME]
Options:
-h, --help Print help
-V, --version Print version
$ 03_04_subcommands_derive add bob
'myapp add' was used, name is: Some("bob")
When specifying commands with command: Commands
, they are required.
Alternatively, you could do command: Option<Commands>
to make it optional.
$ 03_04_subcommands_derive
? failed
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_04_subcommands_derive[EXE] <COMMAND>
Commands:
add Adds files to myapp
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
-V, --version Print version
Since we specified #[command(propagate_version = true)]
,
the --version
flag is available in all subcommands:
$ 03_04_subcommands_derive --version
clap [..]
$ 03_04_subcommands_derive add --version
clap-add [..]
§Defaults
We’ve previously showed that arguments can be required
or optional.
When optional, you work with a Option
and can unwrap_or
. Alternatively, you can
set #[arg(default_value_t)]
.
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[arg(default_value_t = 2020)]
port: u16,
}
fn main() {
let cli = Cli::parse();
println!("port: {:?}", cli.port);
}
$ 03_05_default_values_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 03_05_default_values_derive[EXE] [PORT]
Arguments:
[PORT] [default: 2020]
Options:
-h, --help Print help
-V, --version Print version
$ 03_05_default_values_derive
port: 2020
$ 03_05_default_values_derive 22
port: 22
§Validation
An appropriate default parser/validator will be selected for the field’s type. See
value_parser!
for more details.
§Enumerated values
For example, if you have arguments of specific values you want to test for, you can derive
ValueEnum
(any PossibleValue
builder function can be used as a #[value]
attribute on enum variants).
This allows you specify the valid values for that argument. If the user does not use one of those specific values, they will receive a graceful exit with error message informing them of the mistake, and what the possible valid values are
use clap::{Parser, ValueEnum};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// What mode to run the program in
#[arg(value_enum)]
mode: Mode,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum Mode {
/// Run swiftly
Fast,
/// Crawl slowly but steadily
///
/// This paragraph is ignored because there is no long help text for possible values.
Slow,
}
fn main() {
let cli = Cli::parse();
match cli.mode {
Mode::Fast => {
println!("Hare");
}
Mode::Slow => {
println!("Tortoise");
}
}
}
$ 04_01_enum_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 04_01_enum_derive[EXE] <MODE>
Arguments:
<MODE>
What mode to run the program in
Possible values:
- fast: Run swiftly
- slow: Crawl slowly but steadily
Options:
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
$ 04_01_enum_derive -h
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 04_01_enum_derive[EXE] <MODE>
Arguments:
<MODE> What mode to run the program in [possible values: fast, slow]
Options:
-h, --help Print help (see more with '--help')
-V, --version Print version
$ 04_01_enum_derive fast
Hare
$ 04_01_enum_derive slow
Tortoise
$ 04_01_enum_derive medium
? failed
error: invalid value 'medium' for '<MODE>'
[possible values: fast, slow]
For more information, try '--help'.
§Validated values
More generally, you can validate and parse into any data type with Arg::value_parser
.
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Network port to use
#[arg(value_parser = clap::value_parser!(u16).range(1..))]
port: u16,
}
fn main() {
let cli = Cli::parse();
println!("PORT = {}", cli.port);
}
$ 04_02_parse_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 04_02_parse_derive[EXE] <PORT>
Arguments:
<PORT> Network port to use
Options:
-h, --help Print help
-V, --version Print version
$ 04_02_parse_derive 22
PORT = 22
$ 04_02_parse_derive foobar
? failed
error: invalid value 'foobar' for '<PORT>': invalid digit found in string
For more information, try '--help'.
$ 04_02_parse_derive 0
? failed
error: invalid value '0' for '<PORT>': 0 is not in 1..=65535
For more information, try '--help'.
A custom parser can be used to improve the error messages or provide additional validation:
use std::ops::RangeInclusive;
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Network port to use
#[arg(value_parser = port_in_range)]
port: u16,
}
fn main() {
let cli = Cli::parse();
println!("PORT = {}", cli.port);
}
const PORT_RANGE: RangeInclusive<usize> = 1..=65535;
fn port_in_range(s: &str) -> Result<u16, String> {
let port: usize = s
.parse()
.map_err(|_| format!("`{s}` isn't a port number"))?;
if PORT_RANGE.contains(&port) {
Ok(port as u16)
} else {
Err(format!(
"port not in range {}-{}",
PORT_RANGE.start(),
PORT_RANGE.end()
))
}
}
$ 04_02_validate_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 04_02_validate_derive[EXE] <PORT>
Arguments:
<PORT> Network port to use
Options:
-h, --help Print help
-V, --version Print version
$ 04_02_validate_derive 22
PORT = 22
$ 04_02_validate_derive foobar
? failed
error: invalid value 'foobar' for '<PORT>': `foobar` isn't a port number
For more information, try '--help'.
$ 04_02_validate_derive 0
? failed
error: invalid value '0' for '<PORT>': port not in range 1-65535
For more information, try '--help'.
See Arg::value_parser
for more details.
§Argument Relations
You can declare dependencies or conflicts between Arg
s or even
ArgGroup
s.
ArgGroup
s make it easier to declare relations instead of having to list
each individually, or when you want a rule to apply “any but not all” arguments.
Perhaps the most common use of ArgGroup
s is to require one and only one
argument to be present out of a given set. Imagine that you had multiple arguments, and you
want one of them to be required, but making all of them required isn’t feasible because perhaps
they conflict with each other.
ArgGroup
s are automatically created for a struct
with its
ArgGroup::id
being the struct’s name.
use clap::{Args, Parser};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
#[command(flatten)]
vers: Vers,
/// some regular input
#[arg(group = "input")]
input_file: Option<String>,
/// some special input argument
#[arg(long, group = "input")]
spec_in: Option<String>,
#[arg(short, requires = "input")]
config: Option<String>,
}
#[derive(Args)]
#[group(required = true, multiple = false)]
struct Vers {
/// set version manually
#[arg(long, value_name = "VER")]
set_ver: Option<String>,
/// auto inc major
#[arg(long)]
major: bool,
/// auto inc minor
#[arg(long)]
minor: bool,
/// auto inc patch
#[arg(long)]
patch: bool,
}
fn main() {
let cli = Cli::parse();
// Let's assume the old version 1.2.3
let mut major = 1;
let mut minor = 2;
let mut patch = 3;
// See if --set_ver was used to set the version manually
let vers = &cli.vers;
let version = if let Some(ver) = vers.set_ver.as_deref() {
ver.to_string()
} else {
// Increment the one requested (in a real program, we'd reset the lower numbers)
let (maj, min, pat) = (vers.major, vers.minor, vers.patch);
match (maj, min, pat) {
(true, _, _) => major += 1,
(_, true, _) => minor += 1,
(_, _, true) => patch += 1,
_ => unreachable!(),
};
format!("{major}.{minor}.{patch}")
};
println!("Version: {version}");
// Check for usage of -c
if let Some(config) = cli.config.as_deref() {
let input = cli
.input_file
.as_deref()
.unwrap_or_else(|| cli.spec_in.as_deref().unwrap());
println!("Doing work using input {input} and config {config}");
}
}
$ 04_03_relations_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 04_03_relations_derive[EXE] [OPTIONS] <--set-ver <VER>|--major|--minor|--patch> [INPUT_FILE]
Arguments:
[INPUT_FILE] some regular input
Options:
--set-ver <VER> set version manually
--major auto inc major
--minor auto inc minor
--patch auto inc patch
--spec-in <SPEC_IN> some special input argument
-c <CONFIG>
-h, --help Print help
-V, --version Print version
$ 04_03_relations_derive
? failed
error: the following required arguments were not provided:
<--set-ver <VER>|--major|--minor|--patch>
Usage: 04_03_relations_derive[EXE] <--set-ver <VER>|--major|--minor|--patch> [INPUT_FILE]
For more information, try '--help'.
$ 04_03_relations_derive --major
Version: 2.2.3
$ 04_03_relations_derive --major --minor
? failed
error: the argument '--major' cannot be used with '--minor'
Usage: 04_03_relations_derive[EXE] <--set-ver <VER>|--major|--minor|--patch> [INPUT_FILE]
For more information, try '--help'.
$ 04_03_relations_derive --major -c config.toml
? failed
error: the following required arguments were not provided:
<INPUT_FILE|--spec-in <SPEC_IN>>
Usage: 04_03_relations_derive[EXE] -c <CONFIG> <--set-ver <VER>|--major|--minor|--patch> <INPUT_FILE|--spec-in <SPEC_IN>>
For more information, try '--help'.
$ 04_03_relations_derive --major -c config.toml --spec-in input.txt
Version: 2.2.3
Doing work using input input.txt and config config.toml
§Custom Validation
As a last resort, you can create custom errors with the basics of clap’s formatting.
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser};
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// set version manually
#[arg(long, value_name = "VER")]
set_ver: Option<String>,
/// auto inc major
#[arg(long)]
major: bool,
/// auto inc minor
#[arg(long)]
minor: bool,
/// auto inc patch
#[arg(long)]
patch: bool,
/// some regular input
input_file: Option<String>,
/// some special input argument
#[arg(long)]
spec_in: Option<String>,
#[arg(short)]
config: Option<String>,
}
fn main() {
let cli = Cli::parse();
// Let's assume the old version 1.2.3
let mut major = 1;
let mut minor = 2;
let mut patch = 3;
// See if --set-ver was used to set the version manually
let version = if let Some(ver) = cli.set_ver.as_deref() {
if cli.major || cli.minor || cli.patch {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Can't do relative and absolute version change",
)
.exit();
}
ver.to_string()
} else {
// Increment the one requested (in a real program, we'd reset the lower numbers)
let (maj, min, pat) = (cli.major, cli.minor, cli.patch);
match (maj, min, pat) {
(true, false, false) => major += 1,
(false, true, false) => minor += 1,
(false, false, true) => patch += 1,
_ => {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::ArgumentConflict,
"Can only modify one version field",
)
.exit();
}
};
format!("{major}.{minor}.{patch}")
};
println!("Version: {version}");
// Check for usage of -c
if let Some(config) = cli.config.as_deref() {
let input = cli
.input_file
.as_deref()
// 'or' is preferred to 'or_else' here since `Option::as_deref` is 'const'
.or(cli.spec_in.as_deref())
.unwrap_or_else(|| {
let mut cmd = Cli::command();
cmd.error(
ErrorKind::MissingRequiredArgument,
"INPUT_FILE or --spec-in is required when using --config",
)
.exit()
});
println!("Doing work using input {input} and config {config}");
}
}
$ 04_04_custom_derive --help
A simple to use, efficient, and full-featured Command Line Argument Parser
Usage: 04_04_custom_derive[EXE] [OPTIONS] [INPUT_FILE]
Arguments:
[INPUT_FILE] some regular input
Options:
--set-ver <VER> set version manually
--major auto inc major
--minor auto inc minor
--patch auto inc patch
--spec-in <SPEC_IN> some special input argument
-c <CONFIG>
-h, --help Print help
-V, --version Print version
$ 04_04_custom_derive
? failed
error: Can only modify one version field
Usage: clap [OPTIONS] [INPUT_FILE]
For more information, try '--help'.
$ 04_04_custom_derive --major
Version: 2.2.3
$ 04_04_custom_derive --major --minor
? failed
error: Can only modify one version field
Usage: clap [OPTIONS] [INPUT_FILE]
For more information, try '--help'.
$ 04_04_custom_derive --major -c config.toml
? failed
Version: 2.2.3
error: INPUT_FILE or --spec-in is required when using --config
Usage: clap [OPTIONS] [INPUT_FILE]
For more information, try '--help'.
$ 04_04_custom_derive --major -c config.toml --spec-in input.txt
Version: 2.2.3
Doing work using input input.txt and config config.toml
§Testing
clap reports most development errors as debug_assert!
s. Rather than checking every
subcommand, you should have a test that calls
Command::debug_assert
:
use clap::Parser;
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Cli {
/// Network port to use
port: u16,
}
fn main() {
let cli = Cli::parse();
println!("PORT = {}", cli.port);
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
§Next Steps
- Cookbook for application-focused examples
- Explore more features in the Derive reference
- See also
Command
,Arg
,ArgGroup
, andPossibleValue
builder functions which can be used as attributes
- See also
For support, see Discussions