clap: Combination of options causes clap to apply validator to both default and user-specified option
rustc version: rustc 1.12.0 (3191fbae9 2016-09-23) clap Version: 2.14.0
In one of my new projects, I noticed that it was dying with an error about the default argument value failing “path is readable” validation, even when I was trying to override it with an explicit argument.
I started working on a simplified testcase and found that this bug is very picky. If any of these conditions aren’t met, it will act properly and validate either the default or the user-provided value but not both:
global(true)
must be specified- A
default_value
must be specified - A
validator
must be provided - A subcommand must be defined and passed on the command line
…but, if all of them are, it insists on validating both the default and the user-provided value.
It’s possible there’s more I could discover, but I’m sick and it’s getting late, so here’s how far I managed to minimize and pin down the test case so far:
extern crate clap;
use clap::{App,Arg,SubCommand};
const DEFAULT_INPATH: &'static str = "/dev/sr1";
fn main() {
App::new("testcase")
.arg(Arg::with_name("inpath")
.global(true)
.default_value(DEFAULT_INPATH)
.validator(|x| {
print!("Testing {}\n", x);
if x == DEFAULT_INPATH {
print!("ERROR: Validator ran on default string\n");
}
Ok(())
}))
.subcommand(SubCommand::with_name("test"))
.get_matches_from(&["testcase", "/dev/sr0", "test"]);
}
EDIT: I also just noticed that it’s validating the default after the user-provided value:
ssokolow@monolith testcase % cargo run
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
Running `target/debug/testcase`
Testing /dev/sr0
Testing /dev/sr1
ERROR: Validator ran on default string
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (8 by maintainers)
Will do. I’ve been meaning to test for days but more pressing concerns just keep popping up.