Clap - default values for CLI command line parameters in Rust

Rust Clap default_value default_value_t

Part of the series about clap.

For most of the command line applications, most of the flags are optional and have default values. In this example we'll see how to define the default values for string, integer, floating point number, boolean an even PathBuf command line parameters.

The dependencies in Cargo.toml

examples/clap/default-values/Cargo.toml

[package]
name = "default-values"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.4.11", features = ["derive"] }

The full code

examples/clap/default-values/src/main.rs

use clap::Parser;

#[derive(Parser)]
#[command(version)]
struct Cli {
    #[arg(long, default_value = "127.0.0.1")]
    host: String,

    #[arg(long, default_value_t = 5000)]
    port: i32,

    #[arg(long, default_value_t = 0)]
    small: u8,

    #[arg(long, default_value_t = 0.0)]
    float: f32,


    #[arg(long, default_value_t = false)]
    debug: bool,

    #[arg(long, default_value = ".")]
    path: std::path::PathBuf,
}

fn main() {
    let args = Cli::parse();

    println!("host:  {}", args.host);
    println!("port   {}", args.port);
    println!("small: {}", args.small);
    println!("float: {}", args.float);
    println!("debug: {}", args.debug);
    println!("path:  {}", args.path.display());
    // `PathBuf` cannot be formatted with the default formatter; call `.display()` on it
}

Each parameter can have a default value using an attribute of the arguments.

default_value is used for strings and PathBuf

default_value_t is used for numbers and boolean values.

There are several other default types and there are more arg attributes.

See the full list of arg attributes

Usage

As we set a default value to each one of the arguments we can run the program without providing any information on the command line. Each argument will have its default value.

$ cargo run
host:  127.0.0.1
port   5000
small: 0
float: 0
debug: false
path:  .

We can also pass some or all of the arguments on the command line:

$ cargo run -- --host localhost --port 3000

host:  localhost
port   3000
small: 0
float: 0
debug: false
path:  .

Help showing the default values

When using the --help flag that was added by Clap automatically we'll see all the accepted parameter names along with the default values. Very neat.

$ cargo run -- --help

Usage: default-values [OPTIONS]

Options:
      --host <HOST>    [default: 127.0.0.1]
      --port <PORT>    [default: 5000]
      --small <SMALL>  [default: 0]
      --float <FLOAT>  [default: 0]
      --debug
      --path <PATH>    [default: .]
  -h, --help           Print help
  -V, --version        Print version

It is unclear to me why do we not see [default: false] for the --debug flag. As explained in this issue, flags don't have a user-facing meaning (false/true) so showing the default would be meaningless.

Related Pages

Clap - short command line parameters
Clap - positional command line arguments in Rust
Clap - Command Line Argument Parser for Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo