You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to write a program that's a little bit like ssh: it does some setup, then runs the rest of the command-line arguments as a new process. If there are no other arguments, it just runs a shell. Looking at the clap documentation, I came up with the following pattern:
If I build this and run it with no arguments, I get the default, just as I wanted:
$ ./target/debug/claptest
Command to run: ["/bin/sh"]
If I give it a different command to run, I get the default appended to the command I gave:
$ ./target/debug/claptest foo bar
Command to run: ["foo", "bar", "/bin/sh"]
If there's an explicit value on the command-line, I'd expect that to replace the default, not extend it:
$ ./target/debug/claptest foo bar
Command to run: ["foo", "bar"]
Workarounds
If I don't use .default_value(), I can manually apply the default later, but of course then it doesn't show up in the --help output with all the other defaults.
.occurrences_of() returns 0 if only the default is present, 1 if there's one explicit argument, etc. I could do something like:
let command:Vec<_> = if matches.occurrences_of("command") > 0{
matches.values_of_os("command").unwrap().take(matches.occurrences_of("command"))}else{
matches.values_of_os("command").unwrap()}.collect();
...but man, that's a mouthful.
The text was updated successfully, but these errors were encountered:
I'd be willing to mentor someone to fix this, which should be a super quick fix if someone wants an easy PR. Otherwise I can try and knock it out this week once I get a little caught up.
If anyone is interested, the code to potentially fix this issue is here. You'd simply need to ensure there are no values before adding the defaults.
Rust Version
rustc 1.20.0 (f3d6973f4 2017-08-27)
Affected Version of clap
clap 2.26.2
Steps to Reproduce the issue
I want to write a program that's a little bit like
ssh
: it does some setup, then runs the rest of the command-line arguments as a new process. If there are no other arguments, it just runs a shell. Looking at the clap documentation, I came up with the following pattern:If I build this and run it with no arguments, I get the default, just as I wanted:
If I give it a different command to run, I get the default appended to the command I gave:
If there's an explicit value on the command-line, I'd expect that to replace the default, not extend it:
Workarounds
If I don't use
.default_value()
, I can manually apply the default later, but of course then it doesn't show up in the--help
output with all the other defaults..occurrences_of()
returns 0 if only the default is present, 1 if there's one explicit argument, etc. I could do something like:...but man, that's a mouthful.
The text was updated successfully, but these errors were encountered: