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
{{ message }}
Repository navigation
No way to distinguish between ParseArgument invoked for user-supplied input and default value #1287
When using an option that has a custom parser with isDefault set to true, the custom parser cannot distinguish between being invoked to provide a default value and being invoked to parse input that has no tokens.
Example:
privatestaticIEnumerable<string>Formats{get;}=newList<string>{"foo","bar"};varoption=newOption<List<string>>("--formats",parseArgument:ParseFormats,isDefault:true,description:"Narrows the operation to process only files with these formats. Supported formats: "+string.Join(", ",Formats)));privatestaticList<string>ParseFormats(ArgumentResultresult){varformats=result.Tokens.Select(t =>t.Value).ToList();varinvalidFormats=formats.Where(t =>!Formats.Contains(t));if(invalidFormats.Any()){result.ErrorMessage=$"One or more formats are invalid: {string.Join(", ",invalidFormats)}";}//Not provided or no formats specified on the command lineelseif(formats.Count==0){returnFormats.ToList();}returnformats;}
(Formats cannot be an enum here because the list in the actual code is constructed from a list of objects providing a string property)
In this case the parser cannot constrain input to require one or more tokens because this will also be the case when getting a default value.
Some way to know when it's being invoked for a default value would be nice.
In this case the parser cannot constrain input to require one or more tokens because this will also be the case when getting a default value.
It sounds to me you want to set the min/max number of required arguments for your --formats option. Unless i misunderstood you, is there a particular reason why you are not setting the option argument arity to achieve this?
In this case the parser cannot constrain input to require one or more tokens because this will also be the case when getting a default value.
It sounds to me you want to set the min/max number of required arguments for your --formats option. Unless i have misunderstood you, is there a particular reason why you are not setting the option argument arity to achieve this?
Because i can't do that. The Option API doesn't let you set arity when using the custom parser constructor:
Another thing you can do is to check whether the ArgumentResult is implicit (indicating it was created via the default value because there were no tokens provided on the command line).
This does the job, thank you. I agree that the IsImplicit property should be made public. Being able to differentiate between the default and a user-provided value could be very useful.
#2073 may have fixed this, by giving Argument<T> and Option<T> separate DefaultValueFactory and CustomParser properties, each of which has type Func<ArgumentResult, T>?.
Activity
elgonzo commentedon May 25, 2021
It sounds to me you want to set the min/max number of required arguments for your --formats option. Unless i misunderstood you, is there a particular reason why you are not setting the option argument arity to achieve this?
SamVanheer commentedon May 25, 2021
Because i can't do that. The
OptionAPI doesn't let you set arity when using the custom parser constructor:command-line-api/src/System.CommandLine/Option{T}.cs
Lines 32 to 38 in 56139ef
elgonzo commentedon May 25, 2021
Oh dang... :-(
jonsequitur commentedon Jul 14, 2021
You can now set
Option.Arity.Another thing you can do is to check whether the
ArgumentResultis implicit (indicating it was created via the default value because there were no tokens provided on the command line).Maybe we should make this property public:
command-line-api/src/System.CommandLine/Parsing/ArgumentResult.cs
Line 30 in b751c2e
SamVanheer commentedon Jul 14, 2021
This does the job, thank you. I agree that the
IsImplicitproperty should be made public. Being able to differentiate between the default and a user-provided value could be very useful.SamVanheer commentedon Aug 25, 2023
Does this still require feedback or was my comment above sufficient?
KalleOlaviNiemitalo commentedon Aug 26, 2023
#2073 may have fixed this, by giving Argument<T> and Option<T> separate DefaultValueFactory and CustomParser properties, each of which has type Func<ArgumentResult, T>?.
SamVanheer commentedon Aug 26, 2023
Looks like it, that seems to solve the problem entirely.
[ClassicPageAudit] Fix --skipusageinformation rejected regardless of …
[ClassicPageAudit] Add classicpageauditusage.csv - audit log usage fo…