mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 23:02:31 +00:00
9a180c1511
Right now - `default_value="something"` is a raw method - `default_value` uses native types This commit splits the meanings - `default_value="something"` is a raw method - `default_value_t` uses `T::default()` - `default_value_t=expr` uses an expression that evaluates to `T` This is meant to mirror the `value_of` / `value_of_t` API. At the moment, this is limited to `T: Display` to work with clap's default system. Something we can look at in the future is a way to loosen that restriction. One quick win is to specialize when `arg_enum` is set. The main downside is complicating the processing of attributes because it then means we need some processed before others. Since this builds on `clap`s existing default system, this also means users do not get any performance gains out of using `default_value_t`, since we still need to parse it but we also need to convert it to a string. Fixes #1694
47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use clap::Clap;
|
|
|
|
mod utils;
|
|
|
|
use utils::*;
|
|
|
|
#[test]
|
|
fn default_value() {
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
struct Opt {
|
|
#[clap(default_value = "3")]
|
|
arg: i32,
|
|
}
|
|
assert_eq!(Opt { arg: 3 }, Opt::parse_from(&["test"]));
|
|
assert_eq!(Opt { arg: 1 }, Opt::parse_from(&["test", "1"]));
|
|
|
|
let help = get_long_help::<Opt>();
|
|
assert!(help.contains("[default: 3]"));
|
|
}
|
|
|
|
#[test]
|
|
fn default_value_t() {
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
struct Opt {
|
|
#[clap(default_value_t = 3)]
|
|
arg: i32,
|
|
}
|
|
assert_eq!(Opt { arg: 3 }, Opt::parse_from(&["test"]));
|
|
assert_eq!(Opt { arg: 1 }, Opt::parse_from(&["test", "1"]));
|
|
|
|
let help = get_long_help::<Opt>();
|
|
assert!(help.contains("[default: 3]"));
|
|
}
|
|
|
|
#[test]
|
|
fn auto_default_value_t() {
|
|
#[derive(Clap, PartialEq, Debug)]
|
|
struct Opt {
|
|
#[clap(default_value_t)]
|
|
arg: i32,
|
|
}
|
|
assert_eq!(Opt { arg: 0 }, Opt::parse_from(&["test"]));
|
|
assert_eq!(Opt { arg: 1 }, Opt::parse_from(&["test", "1"]));
|
|
|
|
let help = get_long_help::<Opt>();
|
|
assert!(help.contains("[default: 0]"));
|
|
}
|