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
If optional is true and validating the argument using the validator fails, do not consume the argument and use the default value instead.
Add parse function
classMyOptionsextendsOptions{
@option({parse(consume: ()=>string|undefined,unconsume: (arg: string)=>void){constoption=consume();if(option===undefined)returnfalse;// no more arguments availableif(option==='true')returntrue;if(option==='false')returnfalse;constnumeric=+option;if(!Number.isNaN(numeric))returnnumeric;// argument is not in the expected format, push it back so that it may be parsed as another option or parameterunconsume(option);returnfalse;// use a default value},validator(value: boolean|number){if(typeofvalue==='number'&&value<0)thrownewExpectedError('--fix must be a positive number');}})publicfix: boolean|number;}
If a parse function is provided, this function is responsible for consuming the arguments, parsing and converting the value. By calling consume() more than once the parser may consume 0..n of the remaining arguments. validators are still executed after parsing. That means errors about invalid option values can be reported. That's not possible with the optional proposal.
The text was updated successfully, but these errors were encountered:
Hi, thanks for this feature request. I am quite into the second solution (though I might prefer peek and consume), it looks like a more elegant way consuming options while giving developers more flexibility.
While trying to port my CLI, I noticed there is no way to declare options that may not consume the next argument if not necessary.
A popular example are TypeScript's compilerOptions:
In my use case this is not limited to a switch option (boolean value). It's actually an optional boolean or number:
I can think of 2 possible solutions:
Add
optional
flagIf
optional
is true and validating the argument using thevalidator
fails, do not consume the argument and use the default value instead.Add
parse
functionIf a
parse
function is provided, this function is responsible for consuming the arguments, parsing and converting the value. By callingconsume()
more than once the parser may consume 0..n of the remaining arguments.validators
are still executed after parsing. That means errors about invalid option values can be reported. That's not possible with theoptional
proposal.The text was updated successfully, but these errors were encountered: