-
Notifications
You must be signed in to change notification settings - Fork 10
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
Make "type: string" options greedy? #77
Comments
"It is not consumed if it is option-like." seems like the proper approach, because the user's intention (which is more important than the parseArgs consumer's) is clearly to pass it as an option. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Thanks for consolidating all that research @shadowspawn! Definitely helped me build a more informed opinion on the matter. This is a tough one.. I like that The tough part for me is while this change would align with notable standards (e.g. POSIX) it differs from other popular arg parsing Node libraries (e.g. Yargs and Minimist) with the exception of Commander. While my instinct says we should stick to established standards, a part of me thinks it might be better to use idiomatic conventions already used throughout the Node ecosystem. That being said, I don't have a strong opinion either way and would be happy with either approach 👍 |
For my own interest, I went through the ~800 Commander bugs for ones related to greedy/not-greedy. Commander supports required greedy option-argument and optional choosy option-argument, so gets issues both ways around. I don't know what the historical usage ratio is, but expect more required option-argument than optional option-argument. Issues related to greedy behaviour: 3 The not-greedy issues had examples calls of:
|
This comment was marked as outdated.
This comment was marked as outdated.
If it's not greedy, what's the alternative for when you want to pass a string starting with Can you do it with The non-greedy behavior seems a lot more palatable if it's always possible to have an error message which tells the user what to write instead.
For negative numbers in particular, that's pretty close to what Python does, except that Python says "any numeric options" rather than "specifically that digit". I think "any numeric options" is the more reasonable of the two, since it's weird as a user if |
Yes.
Good suggestion, with caveat it only assists in |
Modified proposal: the The error in strict mode for There is not a "right" answer, and split feedback. This enhanced approach might suit a majority.
|
I'm all for the modified proposal and gave my 👍 for whatever that's worth 😄 However, I want to put it out there that I don't think erroring on option-like-arguments reduces ambiguity of user intent. It feels very situational to the tool being created. Take for instance this Apologies for prolonging a resolution 😬 Feel free to disregard this opinion as I think there will be more opportunities to refine greedy behavior post MVP (e.g. global |
My description was not great. The "ambiguity" is: did the user intend that, or did they make a mistake? Following your example context:
Did the user forget the pattern argument after We aren't sure so in strict mode we can throw an error just in case, and suggest how to change command if was intended. e.g.
Feedback is good. I wasn't sure how this hybrid would be received, and checking it seems reasonable before pushing it. 😄 |
Tiny bit of feedback on the error message: if it's possible to suggest they use a long-form argument with (But that's just my intuition; no hard data either way. And the custom message is much better than nothing!) |
In somewhat of an aside, the grep example starts with an ambiguity/conflict between whether an argument is an option or a positional. This is a nice example of the
|
Thanks @shadowspawn The examples you provided help clear up the ambiguity we're trying to avoid 👍 |
@bcoe Did you have an opinion on this? Ok to ignore if you are feeling bandwidth limited! Summary of feedback and modified proposal in: #77 (comment) |
Short version
Making an option with an expected option-argument (i.e.
type: string
) always consume the next argument has some benefits:[-c option-argument]
)getopts_long
, Commander)Long version
This is a revisit for a deeper dive into specifically whether a string option should be greedy. Previously raised in #25 and #75 (comment) but not considered in depth.
Given the program:
If there is an argument following the string option, does the parsing result depend on the contents of the argument? (Consider following argument one of:
foo
,-x
,--xx
,-5
,true
,-
,--
,---
, or empty string.)Some simple examples:
Let's take a look at the reference specifications and implementations I identified in #76.
POSIX
Utility Conventions and notes
An option with a mandatory option-argument (our "string" option) should be described in the usage like:
[-c option_argument]
Guideline 7 states: Option-arguments should not be optional. This is clarified in the notes to make it clear that the option is greedy:
GNU
Program Argument Conventions adds long options and does not change the handling of mandatory option-arguments.
getopt_long
Greedy. An option with a mandatory option-argument consumes the next argument.
Commander
Greedy. An option with a mandatory option-argument consumes the next argument.
Minimist
The option-argument is optional. It is not consumed if it is option-like. Negative numbers are not consumed.
(The details actually vary between short and long options, but this may be a historical accident rather than by design! Different behaviours for
-
, leading---
, and empty string.)Yargs
The option-argument is optional. It is not consumed if it is option-like. There is an exception added for negative numbers, which are consumed.
(The details actually vary between short and long options, but this may be a historical accident rather than by design! Different behaviours for
-
and leading---
.)Minimist and Yargs likely rationale
Minimist and Yargs using zero-config parsing assume options may take an argument. This allows multiple options which are variously parsed as boolean or string options. For example with Minimist:
parseArgs
does not have the same constraints as we are assuming auto-discovered options are boolean.The text was updated successfully, but these errors were encountered: