-
-
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
Feature request: allow specifying argument multiple value terminator #782
Comments
clap already supports Is that what you're talking about? |
This works, but has limitations:
Is this use case (like find(1) in Rust) too tricky and justifies doing additional command line parsing outside clap, inside application? |
Can you show me an example invocation of what you're trying to do?
Is already supported. The result is two values of |
It requires escaping of It should be usable in something like this
Arguments supplied to to |
Ah ok, I think I understand now. Correct me if I'm wrong (again 😜 ), but you're looking for a way to end an array of values with a specified character? So really, it's a way to specify the array terminator, not delimiter. I.e. you want Currently, this isn't supported, as you've asserted. It would be an easy additoin though. There are a few issues slightly higher on the bucket list first, but I should be able to get this shortly. |
Yes, it is array terminator.
User-overridable separator may complicate this a bit. With fixed separator it is tricky to "recursively re-enter" the same program. Also currently named options don't accept array values. Also non-final positional arguments shoud be able to handle this: |
It wouldn't be that hard depending on how exactly you define the CLI.
What do you mean by array values?
This gives
You'd have to do a double parse of the arguments, but since that takes nanoseconds, it's not a big deal. Suppose this get implemented, you'd just do something like let pre_matches = App::new("prog")
.arg(Arg::with_name("pos1")
.required(true))
.arg(Arg::with_name("dummy-arg")
.multiple(true)
.allow_hyphen_values(true))
.get_matches();
let matchse = App::new("prog")
.arg(Arg::with_name("pos1")
.required(true))
.arg(Arg::with_name("pass-through")
.long("pass")
.multiple(true)
.terminator(pre_matches.value_of("pos1").unwrap())
.allow_hyphen_values(true))
.arg(Arg::with_name("pos2"))
.arg(Arg::with_name("pos3"))
.get_matches(); Then you could easily run,
You could also do an optional terminator like so: let pass_through_arg = Arg::with_name("pass-through")
.long("pass")
.multiple(true)
.allow_hyphen_values(true);
let app = App::new("prog")
.arg(Arg::with_name("term")
.long("terminator")
.takes_value(true))
.arg(Arg::with_name("pos1"))
.arg(Arg::with_name("pos2"));
let mut matches = app
.arg(pass_through_arg.terminator(";")) // Set a default terminator of ;
.get_matches();
matches = if let Some(term) = matches.value_of("term") {
app.arg(pass_through_arg.terminator(term))
.get_matches()
} else {
matches
}; Then you could run,
|
Looks reasonable. |
OK. The only thing not implemented yet is the |
Are you planning to implement this yourself of will wait for a pull request? |
I plan on implementing it once I finish the current issue I'm working on. Depending on what sort of free time I've got it could be done and merged within the next few days. Of course, if someone beats me to it I'll gladly accept 😉 |
…lues with a given value One can now specificy a value termintaor that will stop the parsing of multiple values upon reaching this special value. Closes #782
…lues with a given value One can now specificy a value termintaor that will stop the parsing of multiple values upon reaching this special value. Closes #782
…lues with a given value One can now specificy a value termintaor that will stop the parsing of multiple values upon reaching this special value. Closes #782
Should overridable terminator by double parsing pattern be documented somewhere as a best practice (to avoid flourishing of hardcoded separator command line APIs which are harmful in the long term)? |
@vi it wouldn't hurt to document it somewhere, perhaps in the I don't think there'll be a huge rush of hard-coded terminators though. 😜 Once I get the big 2.20 update out, I can try to put an example together, unless someone beats me to it. |
Rust Version
rustc 1.15.0-nightly (0bd2ce62b 2016-11-19)
Affected Version of clap
2.19.2
Description
clap should be able to accept array of command line arguments, delimited by
;
or--
or just end of command line arguments, suitable to be used for spawning a command with arguments specified by user without any escaping/unescaping, like in find(1) or xargs(1).The text was updated successfully, but these errors were encountered: