-
-
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
Having two unnamed arguments and making the first optional #636
Comments
Issues are fine for questions 😄 Currently you can't exactly do that, but depending on your actual use case there may be a way to make it work. The reason I say you can't exactly do that, is that unnamed arguments are purely index based, clap doesn't know which value is which, simply first come first served. But in order to prevent an XY problem, could you tell me a little about the actual use case and what you're trying to model? There may be a better way, or something that Thanks for taking the time to do the detailed write up! 👍 |
Oh I can definitely make this work simply by making TEXT the first unnamed argument and CODE the second, optional one. This is what the second code snippet does. I'm fine with that, too, it's not a big deal to have the arguments reverted, but the reason I wanted them to be the way I initially described is that it feels more ergonomic for my use case. I'm new to Rust and I'm trying to create an app for managing todo files (I use them for projects I work alone at because I dislike having issues be out-of-the-band, unversioned in relation to the source code) and I'd like to be able to create a todo just by invoking I'm closing the issue for now. I believe code-wise this could be changed when no |
I'd like this feature as well, and in my case I can't just reorder the arguments. Could you please reopen this? My use case: I'm adding subcommands to git-series similar to "cp" and "mv", which take "source" and "dest" positional arguments in that order; they need to come in that order to follow well-established convention (cp source dest). I also need to make "source" optional, because it has a default (the current series). So, I'd like to make source optional and dest mandatory. In this case, if clap sees one positional argument, it should go in dest, but two positional arguments should go in source and dest. Does that seem reasonable? |
Reopening for @joshtriplett. |
@joshtriplett with the new 2.17.0 allowing things like |
@joshtriplett I could special case this if there are only two positional arguments, but my worry is what if there are 3, or 4? It's also hard to determine at that point if the user input the the first arg correctly, but accidently left off the second, but then clap interprets it differnetly.
Gets interpretted as Which may not be a huge deal in your case, but imagine a hypothetical
Gets interpretted as |
@kbknapp The version from 2.17 almost works, but I don't want to allow multiple arguments (or display it as |
Since there's now a form of look-ahead for the @joshtriplett and @TomasHubelbauer in both cases is the second positional always required? If that's the case I could implement this, because saying, "the final positional is required, but the one prior is not" is the information clap needs to special case. Otherwise I'd have to add something like an |
@kbknapp Yes, the case I have always requires the second positional argument. |
I have this implemented and it's working. I have one final issue to implement, then I'll put out 2.20 on crates.io |
Forgive me if Issues are not meant for questions, neither the contributing guidelines nor the ReadMe say so, so please allow me to ask a question here. I'd like to make
clap
parse the following:todo TEST "Test things"
meansCODE
is TEST,TEXT
is Test things.todo "Test things"
meansCODE
is not set,TEXT
is Test things.todo TEST "Test things" -m
meansCODE
is TEST,TEXT
is Test things,marked
istrue
.I've looked over the examples but am having trouble with making the first argument optional.
Is there a way to do this?
The text was updated successfully, but these errors were encountered: