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
Flag implies other flags #1879
Comments
On master branch, you should be able to do this with the following: App::new("MyApp")
.replace("--save-all", &["--save-image", "--save-stats", "--save-text"])
.arg(Arg::with_name("save_all")
.long("save-all"))
.arg(Arg::with_name("save_image")
.long("save-image"))
.arg(Arg::with_name("save_stats")
.long("save-stats"))
.arg(Arg::with_name("save_text")
.long("save-text")) |
@camsteffen once you can confirm the proposed solution works for you and meats your intent we can close the issue 😄 |
Interesting, that might work. Just some minor concerns. The example given in the docs for I would expect my use case to be possible at the Ideally, I would like the |
@camsteffen we're in the process of going through all docs, so I'll make sure that one gets some additional explanation to make it clear that it's not just for subcommands. You can think of it literally replacing/inserting the left hand side item ( So the CLI invocation goes from:
You're correct it doesn't automatically add verbiage to the help message of |
@kbknapp Sounds good. I'm still wondering about the example for |
Imagine
Likewise, if instead of
Hopefully this clears it up a little more. I'm going to close this issue, but feel free to continue asking questions if you need more explanation 😉 |
That's true for for all the other conditions too.
It does the replacing during parsing which allows to let it work only on certain apps (or subcommands)
I am actually using it like the below in one of the CLI tools I am working on. App::new("app")
.replace("ll", &["ls", "-l"])
.replace("la", &["ls", "-a"])
.subcommand(App::new("ls")
.arg(Arg::new("all").short('a'))
.arg(Arg::new("long").short('l')) |
Ok, makes sense.
Ah, that answers my question! Ok, I'm happy with |
You can already use it via git dependency in Cargo.toml
You can specify by branch or version ( |
Alas, I finally tried it and it did not work as expected. let matches = clap::App::new("MyApp")
.replace("--save-all", &["--save-a", "--save-b"])
.arg(Arg::with_name("flag").short('f'))
.arg(Arg::with_name("save_all").long("save-all"))
.arg(Arg::with_name("save_a").long("save-a"))
.arg(Arg::with_name("save_b").long("save-b"))
.get_matches_from(&["-f", "--save-all"]);
assert!(matches.is_present("flag")); // fails here
assert!(matches.is_present("save_a"));
assert!(matches.is_present("save_b")); |
You're missing the binary name. It should be: .get_matches_from(&["myapp", "-f", "--save-all"]); If you don't want to include a binary name you must use |
🤦 |
Make sure you completed the following tasks
Describe your use case
I have an application that generates a bunch of files depending on several flags. I want to have a "save-all" flag that implies all of the more granular "save-x" flags. I'm surprised that I can't find any similar request through searching.
Here is an example: (simplified for this post)
Describe the solution you'd like
An
implies_all
option forArg
is what I'm interested in. But I would also addimplies
(singular) andimplied_by
. I believe this is similar todefault_value_if
except that it is for flags.Alternatives, if applicable
I don't know of another convenient way to do this. Am I missing something?
Perhaps it should be named similarly to
default_value_*
? Thedefault_value_*
functions implytakes_value
. Changing this is probably not an option. So it seems to me that thedefault_value_*
space is too different. The word "implies" is intuitive to me.The text was updated successfully, but these errors were encountered: