Help with Arg::New -- second arg being required when I don't want it to be #5861
-
So I have a little tool I slapped together to help me learn rust. I am running into an issue when creating my second arg list where the default_value is adhered to when nothing is passed at all, but when the first arg is listed I have to fill in the second. I have spent a good while scouring the documentation for something that may point me in the correct direction. So now I must e-beg :) So here is what I have:
My intention is to have it when invoked with no parameters run it as if "all" and "terminal" are selected (which works), and if something needs to be specified, act differently accordingly. So if I invoke it with "dnf" only, to expect the output to be "terminal" (this does not work). But I am being forced to specify both on the cli. So in the latter's case, I must invoke with "dnf terminal", for example. I want to be able to just go "dnf" and have it expect report back as terminal, which the rest of my program can handle dealing with (which it pretty much does). The problem is how can I make the second parameter (and presumably if I need 3rd, 4th, ....) not be required when the first one is specified? Sample of what I mean:
TIA! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This is an ambitious situation in the parser. Unless we check the parse result and rely on the user correctly typing answers for the first parameter, there is no clear way to say whether the last argument is part of the first We don't check the parse result for a couple of reasons
I wouldn't have been able to say what we do in this situation because the nuance involved in that part of the code. If we didn't have the logic to assume the user meant the last positional, the alternative would likely have been an assert since then we could never reach the last argument. A workaround is to use one argument and to override the usage to show it as two. |
Beta Was this translation helpful? Give feedback.
This is an ambitious situation in the parser. Unless we check the parse result and rely on the user correctly typing answers for the first parameter, there is no clear way to say whether the last argument is part of the first
Arg
or the second.We don't check the parse result for a couple of reasons
I wouldn't have been able to say what we do in this situation because the nuance involved in that part of the code. If we didn't have the logic to assume the user meant the last positional, the alternative would likely have been an assert since then we could never reach the last argument.
A…