-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option
arg cannot have None
as default value
#5078
Comments
#[derive(Parser)]
struct Args {
#[arg(long, num_args = 0..=1, default_value = "[]")]
foo: Option<usize>,
} says to parse #[derive(Parser)]
struct Args {
#[arg(long, num_args = 0..=1, default_value_t = None)]
foo: Option<usize>,
} Recognizes #[derive(Parser)]
struct Args {
#[arg(long, num_args = 0..=1, default_value_t)]
foo: Option<usize>,
} Says to use `Default::default
Not for me: $ ./clap-5078.rs --foo []
warning: `package.edition` is unspecified, defaulting to `2021`
warning: unused variable: `args`
--> clap-5078.rs:19:9
|
19 | let args = Args::parse();
| ^^^^ help: if this is intentional, prefix it with an underscore: `_args`
|
= note: `#[warn(unused_variables)]` on by default
warning: `clap-5078` (bin "clap-5078") generated 1 warning (run `cargo fix --bin "clap-5078"` to apply 1 suggestion)
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `/home/epage/.cargo/target/92/58a7b79897d359/debug/clap-5078 --foo '[]'`
error: invalid value '[]' for '--foo [<FOO>]': invalid digit found in string
For more information, try '--help'. You might be running into shell specific logic where
I'm not sure what this is referring to. It doesn't do that for me. I removed any defaulting and it runs as I would expect unless I'm missing something about what you are trying to accomplish. |
This is referring to overriding the "assumed intent", https://docs.rs/clap/latest/clap/_derive/index.html#arg-types. From my testing, it makes the core type
You may be right about this being shell-specific behavior. It appears equivalent to P.S. |
It might help if you stepped back and explained what you are trying to accomplish. What behavior do you want out of |
I want For context, my application wraps a function that takes Additionally, by default, the CLI sets a max width, |
So you want to opt-out of special You can then do: fn parser(raw: &str) -> Result<OptionUsize, String> {
if raw == "None" || raw.is_empty() {
Ok(None)
} else {
raw.parse::<usize>().map(Some)
}
}
...
#[derive(Parser)]
struct Args {
#[arg(long, value_parser = parser)]
foo: OptionUsize,
} |
That work for my case. I suppose the question then is, why does Clap not support |
We don't know what the intention is for an empty string. |
Please complete the following tasks
Rust Version
rustc 1.70.0 (90c541806 2023-05-31) (built from a source tarball)
Clap Version
4.3.21
Minimal reproducible code
Steps to reproduce the bug with the above code
cargo run
Actual Behaviour
The program exits with
error: invalid value '[]' for '--foo [<FOO>]': invalid digit found in string
.Expected Behaviour
The program should run with
args.foo
set toNone
.Additional Context
num_args = 0..=1
disables the impliedrequired = false
behavior ofOption<T>
.--foo []
is valid if entered from the command line. It is only not valid when used as a default value.default_value_t = None
fails becauseOption
does not implementDisplay
.Debug Output
[clap_builder::builder::command]Command::_do_parse
[clap_builder::builder::command]Command::_build: name="owm"
[clap_builder::builder::command]Command::_propagate:owm
[clap_builder::builder::command]Command::_check_help_and_version:owm expand_help_tree=false
[clap_builder::builder::command]Command::long_help_exists
[clap_builder::builder::command]Command::_check_help_and_version: Building default --help
[clap_builder::builder::command]Command::_propagate_global_args:owm
[clap_builder::builder::debug_asserts]Command::_debug_asserts
[clap_builder::builder::debug_asserts]Arg::_debug_asserts:foo
[clap_builder::builder::debug_asserts]Arg::_debug_asserts:help
[clap_builder::builder::debug_asserts]Command::_verify_positionals
[clap_builder::parser::parser]Parser::get_matches_with
[clap_builder::parser::parser]Parser::add_defaults
[clap_builder::parser::parser]Parser::add_defaults:iter:foo:
[clap_builder::parser::parser]Parser::add_default_value: doesn't have conditional defaults
[clap_builder::parser::parser]Parser::add_default_value:iter:foo: has default vals
[clap_builder::parser::parser]Parser::add_default_value:iter:foo: wasn't used
[clap_builder::parser::parser]Parser::react action=Set, identifier=None, source=DefaultValue
[clap_builder::parser::arg_matcher]ArgMatcher::start_custom_arg: id="foo", source=DefaultValue
[clap_builder::parser::parser]Parser::push_arg_values: ["[]"]
[clap_builder::parser::parser]Parser::add_single_val_to_arg: cur_idx:=1
[clap_builder::builder::command]Command::color: Color setting...
[clap_builder::builder::command]Auto
[clap_builder::builder::command]Command::color: Color setting...
[clap_builder::builder::command]Auto
error: invalid value '[]' for '--foo []': invalid digit found in string
The text was updated successfully, but these errors were encountered: