You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varrootCommand=newRootCommand{newOption<string[]?>("--config"){IsRequired=false,},newArgument<string>("source"),newArgument<string>("dest"),};rootCommand.Handler=CommandHandler.Create<string,string,string[]?>((source,dest,config)=>{Console.WriteLine($"The value for 'source' is: {source}");Console.WriteLine($"The value for 'dest' is: {dest}");Console.WriteLine($"The number of config params is: {config?.Length??0}");});rootCommand.Invoke(new[]{"--config","abc","--config","def","/src","/dest"});
Running this code gives you:
Required argument missing for command: CommandLineReproducer
Required argument missing for command: CommandLineReproducer
Usage:
CommandLineReproducer [options] <source> <dest>
Arguments:
<source>
<dest>
Options:
--config <config>
--version Show version information
-?, -h, --help Show help and usage information
In my opinion, this should work.
What will work is if the --config options are put to the end, like so:
However, I think, both calls should behave identical (especially since the error message in the first call - "Required argument missing for command: CommandLineReproducer" - is totally confusing).
These two are equivalent. Note here that '--config' option here takes in the upcoming words as parameters to its array. Now, think about it, if any of the upcoming words are supposed to be required arguments, how would the program know that it's not a parameter to its option array.
Also, I think required arguments are supposed to be used in order, unless they are keyword arguments. Imagine 'src' and 'dest' to be used out of order, would result in disaster if you ask me.
Had the '--config' took a single value, instead of an array of values, then you would be able to mix and match things a little.
Now, I imagine why it would be allowed to use '--config' multiple times is so that you can add values to these options whenever you like with other options. For example -
Even though the options '--config' and '--another-config' are out of order, this would yield as follows -
'--config' will have values [ "abc", "def" ]
'--another-config' will have values [ "alpha", "beta" ]
Hopefully, this clears up the misunderstanding of why you think two missing required argument errors are confusing, because those are being interpreted as options for the '--config' array.
I disagree. It may be that you are technically correct but for an end user of the CLI the current behavior is totally confusing.
If you look at existing CLIs it's customary to specify options before arguments:
git mv <options>… <args>…
I'm not that deep in how the System.CommandLine parser works but if you're right and --config abc --config def is the same as --config abc def than this would always prevent array options before arguments - which is an issue/problem in my opinion.
Maybe this behavior could be made configurable?
And are there examples for CLIs that use the --config abc def form? If not, this form could be dropped entirely.
I've been digging around in the documentation, and conventions. From the documentation, it seems to use POSIX style, so I am hoping it also follows POSIX guidelines. Quote from documentation -
While System.CommandLine can be configured to use these and other prefixes, the default is the POSIX style.
I found the following POSIX recommendations link. One of those which doesn't seem to align well here is the following -
Options may be supplied in any order, or appear multiple times. The interpretation is left up to the particular application program.
I could not use an options multiple times, unless it is an array, and when it is an array, I couldn't use arguments after it.
I agree with you on using the array part, I also don't see a way to use arguments after an options array is used. However, I think we shouldn't use option that takes string array, rather, we should also follow standards for our application, and use something like comma-separated string for multiple values. The array option might be useful at the end to pass all arguments to a sub-command or something.
I also found this nice article, "Conventions for Command Line Options". What I took away from it is that when following guidelines, there's unambiguous way of writing the commands, which this library seems to lack I think.
Consider the following, then I share a few things I find odd -
var integerOption = new Option<int>(new[] { "--an-int", "-i" });
integerOption.IsRequired = false;
integerOption.Argument = new Argument<int>("integerValue", () => 1)
{
Arity = ArgumentArity.ZeroOrOne
};
When using arguments as CommandLine -i -2, parsing should fail because it is ambiguous. At no place in the setup did we say that the argument to -i is required, and yet parsing diagram shows -2 is read as value to -i, when it could be another flag.
When using settings where option is required, and arity is ExactlyOne, and arguments like CommandLine --an-int 5, based on the documentation, the space delimiter should have been allowed, but the parsing fails.
I hope my little bit of research can be useful in current issue or finding new ones. On a side note, I had never used this library before using your code you mentioned in this issue. Also, you may find the parsing diagram feature useful, here.
And are there examples for CLIs that use the --config abc def form? If not, this form could be dropped entirely.
This is configurable using Option.AllowMultipleArgumentsPerToken. (The property name here could probably be made clearer. Suggestions welcome.)
The default for this setting is to allow --config abc def to parse as an array for option --config. Seeing this example makes me think we should swap the default for this property. The behavior does exist in some command line apps, and the configurability is intended to allow System.CommandLine to handle idiosyncratic legacy syntaxes. In this way it's not strictly POSIX but more of a superset intended to accommodate both POSIX and Windows-style syntax.
There's a related issue that needs to be addressed to bring the single-arity behavior into line with POSIX expectations: #1083.
@ksharshveer Just a heads-up, there are a few API ambiguities that come into play with your example by setting the Option.Argument property. We're removing this property to make the behavior clearer. See PR #1148.
When using arguments as CommandLine -i -2, parsing should fail because it is ambiguous. At no place in the setup did we say that the argument to -i is required, and yet parsing diagram shows -2 is read as value to -i, when it could be another flag.
The parser understands that -2 is not a valid identifier as defined in your example, so in the context of this command line, it's unambiguous.
When using settings where option is required, and arity is ExactlyOne, and arguments like CommandLine --an-int 5, based on the documentation, the space delimiter should have been allowed, but the parsing fails.
Can you elaborate on this? It seems to work for me but we might be doing something a little differently.
Like I said earlier, -i is supposed to be optional, but when specified it absolutely expects a value. In this case it thinks -2 is a value not a flag. By the way, when used without any arguments except -i, it works, but behaviour is not what I expect. We have set the getDefaultValue, but when used without passing a value, it overrites our default with the type default value. 0 in this case.
Elaboration on second part -
This should be complete code to reproduce the output I get -
Like I mentioned that in the docs, space is a valid delimiter for use in long option, however, the parse diagram shows error. We can see that the = delimiter works just fine.
Like I said earlier, -i is supposed to be optional, but when specified it absolutely expects a value. In this case it thinks -2 is a value not a flag.
The intention of Opton.IsRequired is to indicate whether the option (with whatever arguments it defines) is required. It doesn't change the parsing of the option's arguments in any way. So for example if you were to set interOption.IsRequired = true, then any command line not containing either -i or --an-int would produce an error. When one of those is specified, the Option.Argument's arity (in the case of int this defaults to ArgumentArity.ExactlyOne) will be enforced, so the following token should strictly be considered an argument to that option.
The distinction between the (space) and = argument delimiters looks surprising. I'll dig into whether that's a bug.
By the way, when used without any arguments except -i, it works, but behaviour is not what I expect. We have set the getDefaultValue, but when used without passing a value, it overrites our default with the type default value. 0 in this case.
Can you provide the precise command line input and parse result?
If you pass an array, it assumes that the string has already been split into appropriate tokens (as happens prior to an invocation of an app's Main method, for example). So the issue with the space delimiter is that your first invocation is the equivalent of passing this on the command line:
Thanks @jonsequitur for clearing things up. I didn't knew that invoke behaved that way for string array vs a complete string. For the other part, it doesn't matter now, since the Option.Argument property will likely be removed.
You're welcome. I was just making sure to understand a bunch of potential usability problems and/or bugs in this thread, and (aside from missing docs), I think we can consider the default value of true for Option.AllowMultipleArgumentsPerToken to be a design bug. Even though fixing it is a breaking change, the fact that the convention is not very common might justify the change.
The default for this setting is to allow --config abc def to parse as an array for option --config. Seeing this example makes me think we should swap the default for this property
@jonsequitur , I strongly agree with this change in default! Any idea on if/when?
Activity
ksharshveer commentedon Jan 30, 2021
I believe that this is not an issue. Let's first look at the following -
These two are equivalent. Note here that '--config' option here takes in the upcoming words as parameters to its array. Now, think about it, if any of the upcoming words are supposed to be required arguments, how would the program know that it's not a parameter to its option array.
Also, I think required arguments are supposed to be used in order, unless they are keyword arguments. Imagine 'src' and 'dest' to be used out of order, would result in disaster if you ask me.
Had the '--config' took a single value, instead of an array of values, then you would be able to mix and match things a little.
Now, I imagine why it would be allowed to use '--config' multiple times is so that you can add values to these options whenever you like with other options. For example -
Even though the options '--config' and '--another-config' are out of order, this would yield as follows -
'--config' will have values [ "abc", "def" ]
'--another-config' will have values [ "alpha", "beta" ]
Hopefully, this clears up the misunderstanding of why you think two missing required argument errors are confusing, because those are being interpreted as options for the '--config' array.
skrysm commentedon Jan 30, 2021
I disagree. It may be that you are technically correct but for an end user of the CLI the current behavior is totally confusing.
If you look at existing CLIs it's customary to specify options before arguments:
I'm not that deep in how the
System.CommandLineparser works but if you're right and--config abc --config defis the same as--config abc defthan this would always prevent array options before arguments - which is an issue/problem in my opinion.Maybe this behavior could be made configurable?
And are there examples for CLIs that use the
--config abc defform? If not, this form could be dropped entirely.ksharshveer commentedon Feb 1, 2021
I've been digging around in the documentation, and conventions. From the documentation, it seems to use POSIX style, so I am hoping it also follows POSIX guidelines. Quote from documentation -
I found the following POSIX recommendations link. One of those which doesn't seem to align well here is the following -
I could not use an options multiple times, unless it is an array, and when it is an array, I couldn't use arguments after it.
I agree with you on using the array part, I also don't see a way to use arguments after an options array is used. However, I think we shouldn't use option that takes string array, rather, we should also follow standards for our application, and use something like comma-separated string for multiple values. The array option might be useful at the end to pass all arguments to a sub-command or something.
I also found this nice article, "Conventions for Command Line Options". What I took away from it is that when following guidelines, there's unambiguous way of writing the commands, which this library seems to lack I think.
Consider the following, then I share a few things I find odd -
CommandLine -i -2, parsing should fail because it is ambiguous. At no place in the setup did we say that the argument to-iis required, and yet parsing diagram shows-2is read as value to-i, when it could be another flag.ExactlyOne, and arguments likeCommandLine --an-int 5, based on the documentation, the space delimiter should have been allowed, but the parsing fails.I hope my little bit of research can be useful in current issue or finding new ones. On a side note, I had never used this library before using your code you mentioned in this issue. Also, you may find the parsing diagram feature useful, here.
jonsequitur commentedon Feb 1, 2021
This is configurable using
Option.AllowMultipleArgumentsPerToken. (The property name here could probably be made clearer. Suggestions welcome.)The default for this setting is to allow
--config abc defto parse as an array for option--config. Seeing this example makes me think we should swap the default for this property. The behavior does exist in some command line apps, and the configurability is intended to allow System.CommandLine to handle idiosyncratic legacy syntaxes. In this way it's not strictly POSIX but more of a superset intended to accommodate both POSIX and Windows-style syntax.There's a related issue that needs to be addressed to bring the single-arity behavior into line with POSIX expectations: #1083.
Also related: #669.
jonsequitur commentedon Feb 1, 2021
@ksharshveer Just a heads-up, there are a few API ambiguities that come into play with your example by setting the
Option.Argumentproperty. We're removing this property to make the behavior clearer. See PR #1148.The parser understands that
-2is not a valid identifier as defined in your example, so in the context of this command line, it's unambiguous.Can you elaborate on this? It seems to work for me but we might be doing something a little differently.
ksharshveer commentedon Feb 1, 2021
Hello @jonsequitur I will try to explain my point of view.
First part -
Here's what I did, I made
-2a flag, by using -Invocations -
Output -
[ CommandLine [ -i <-2> ] ]
[ CommandLine [ -2 ] *[ --an-int <1> ] ]
Like I said earlier,
-iis supposed to be optional, but when specified it absolutely expects a value. In this case it thinks-2is a value not a flag. By the way, when used without any arguments except-i, it works, but behaviour is not what I expect. We have set the getDefaultValue, but when used without passing a value, it overrites our default with the type default value. 0 in this case.Elaboration on second part -
This should be complete code to reproduce the output I get -
Output -
![ CommandLine ] ???--> --an-int 5
[ CommandLine [ --an-int <5> ] ]
Like I mentioned that in the docs, space is a valid delimiter for use in long option, however, the parse diagram shows error. We can see that the
=delimiter works just fine.jonsequitur commentedon Feb 2, 2021
The intention of
Opton.IsRequiredis to indicate whether the option (with whatever arguments it defines) is required. It doesn't change the parsing of the option's arguments in any way. So for example if you were to setinterOption.IsRequired = true, then any command line not containing either-ior--an-intwould produce an error. When one of those is specified, theOption.Argument's arity (in the case ofintthis defaults toArgumentArity.ExactlyOne) will be enforced, so the following token should strictly be considered an argument to that option.The distinction between the
(space) and=argument delimiters looks surprising. I'll dig into whether that's a bug.Can you provide the precise command line input and parse result?
jonsequitur commentedon Feb 2, 2021
I would also recommend changing your code to this:
Setting the argument will soon be disallowed, as it presents a few gotchas because you may overwrite values passed in the constructor.
jonsequitur commentedon Feb 2, 2021
I spotted an issue with the invocations in your last code sample.
You should change this:
to this:
If you pass an array, it assumes that the string has already been split into appropriate tokens (as happens prior to an invocation of an app's
Mainmethod, for example). So the issue with the space delimiter is that your first invocation is the equivalent of passing this on the command line:> [parse] "--an-int 5"ksharshveer commentedon Feb 2, 2021
Thanks @jonsequitur for clearing things up. I didn't knew that invoke behaved that way for string array vs a complete string. For the other part, it doesn't matter now, since the Option.Argument property will likely be removed.
jonsequitur commentedon Feb 2, 2021
You're welcome. I was just making sure to understand a bunch of potential usability problems and/or bugs in this thread, and (aside from missing docs), I think we can consider the default value of
trueforOption.AllowMultipleArgumentsPerTokento be a design bug. Even though fixing it is a breaking change, the fact that the convention is not very common might justify the change.skrysm commentedon Feb 2, 2021
@jonsequitur Thanks for all the information. Setting
Option.AllowMultipleArgumentsPerTokentofalseindeed solve the problem for me.The question now is what to do with this issue? I would close it since my problem is now solved. Or do you want to keep it open?
Balfa commentedon Jan 7, 2022
@jonsequitur , I strongly agree with this change in default! Any idea on if/when?
jonsequitur commentedon Jan 7, 2022
The default for
Option.AllowMultipleArgumentsPerTokenisfalsein the latest release.Balfa commentedon Jan 7, 2022
Ah, excellent, I was hoping to check but haven't had a chance to upgrade yet! Thanks 👍