-
-
Notifications
You must be signed in to change notification settings - Fork 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
Support for pacman-style multi-level arguments #1361
Comments
I think this would be a cool feature to implement, if you don't mind I'd like to clarify my approach and see if that's a good one. Prior to proceeding forward. 🙂 First problem clarification, Potential SyntaxApp::new("MyApp")
.version(crate_version!())
.author(crate_author!())
.subcommand(
App::new_flag("S", "sync")
.arg(
Arg::with_name("refresh")
.short("y")
.long("refresh")).get_matches();
// Goal is to allow
// MyApp -Sy and MyApp -yS Only downside I see is that it could allow for the top level ApproachSome modification of enum AppType {
Normal(String),
Flag(String, String),
} Tell me what you guys think, if this is something that you'd want in the project at all, if my approach sounds reasonable. Feedback would be greatly appreciated 👍 |
@NickHackman Nice to see you're interested! 👌 Feel free to ask for anything you need. Regarding syntax: we must support both long and short option subcommands, like Implementation... well, it will most certainly require modifications in |
Exactly, so I think the easiest solution would be... Solution
Performance
@CreepySkeleton Is that what you were thinking as well? Any advice is appreciated 🙂 If we're on the same page, I'll start on the implementation and open a PR in the coming days! |
@NickHackman Sorry for the delay, so many things to do 😭 The problem is: how to pass the long and short names in? Options:
Regarding implementation, more or less correct. I'm not really sure myself 😄 |
@CreepySkeleton completely on the same page, so I want to preserve the current interface and its consistency as much as possible, but it feels like all the options are fairly hacky.
Completely agree, this feels dirty.
I had a similar idea
I don't like this idea because it's a breaking change, but it feels the most consistent to create a I'm still down to work on this issue, but figuring out a good way for it to mesh with the overall interface is most of the battle here. I really appreciate your help and feedback, thanks 😄 |
This is actually super easy with the replace field added in #1697. The only design issue is that you would have define the subcommand (which we already do) and then define the flags for it again (instead of defining them with subcommand). |
use clap::{App, Arg};
fn main() {
let matches = App::new("pacman")
.subcommand(
App::new("sync").args(&[
Arg::with_name("search")
.short('s')
.long("search")
.help("search remote repositories for matching strings"),
Arg::with_name("info")
.short('i')
.long("info")
.help("view package information (-ii for extended information)"),
Arg::with_name("refresh").short('y').long("refresh").help(
"download fresh package databases from the server (-yy to force a refresh even if up to date)",
),
]),
)
.replace("-S", &["sync"])
.replace("--sync", &["sync"])
.get_matches();
println!("{}", matches.is_present("sync"));
} Is a partial test case using |
Try using this and extending it. Long options are working now, so, see if you can use this replace while parsing the short options and replace stuff there. If not, try something in a similar way. Also, |
@pksunkara Regarding API: the number of possible variants is 3, so we can make something akin struct FlagSubCommand {}
impl FlagSubCommand {
fn new_short(id: &str, short: char) -> App;
fn new_long(id: &str, long: &str) -> App;
fn new_short_long(id: &str, short: char, long: &str) -> App;
} Note: they all return We can also go this way
Dirty, but easy to use and implement.
let me tell you a secret: like, 80 of discussions and disputes here are about "how to mend in with existing interface" entirely 🤣 |
Actually, it propagates the
I like this idea. I'll work on this and have it done
Yeah, integrating with and keeping an interface together is hard 😂 EDIT: Sorry, a lot going on with finals and all by next weekend. |
Just being curious, any news on this topic? I'm using |
Sorry, got a little sidetracked. I'll start working on this now 😄 |
Using `pacman` as an example for `FlagSubCommand` because of clap-rs#1361
Adds support for Flag subcommands, for example: $ pacman -Ss $ pacman --sync -s
Bug or Feature Request Summary
I want to emulate archlinux pacman's style with two-level arguments.
I don't think clap can support this yet.
It feels like subcommands but it is still arguments.
Expected Behavior Summary
app -h shows the first level arguments.
app -Qh shows the second level arguments for first level argument Q.
Example:
First level: pacman -h
Second level: pacman -Sh
Actual Behavior Summary
The text was updated successfully, but these errors were encountered: