/// <summary> /// Initializes a new instance of the <see cref="Option"/> class. /// </summary> /// <param name="aliases">The set of strings that can be used on the command line to specify the option.</param> /// <param name="description">The description of the option shown in help.</param> /// <param name="argumentType">The type that the option's argument(s) can be parsed to.</param> /// <param name="getDefaultValue">A delegate used to get a default value for the option when it is not specified on the command line.</param> /// <param name="arity">The arity of the option.</param> public Option( string[] aliases, string?description = null, Type?argumentType = null, Func <object?>?getDefaultValue = null, ArgumentArity arity = default) : this(aliases, description, CreateArgument(argumentType, getDefaultValue, arity)) { }
public static ConvertArgument DefaultConvertArgument(Type type) => symbol => { switch (ArgumentArity.DefaultForType(type).MaximumNumberOfArguments) { case 1: return(Parse(type, symbol.Arguments.SingleOrDefault())); default: return(ParseMany(type, symbol.Arguments)); } };
private static Argument?CreateArgument(Type?argumentType, Func <object?>?getDefaultValue, ArgumentArity arity) { if (argumentType is null && getDefaultValue is null && !arity.IsNonDefault) { return(null); } var rv = new Argument(); if (argumentType is not null) { rv.ValueType = argumentType; } if (getDefaultValue is not null) { rv.SetDefaultValueFactory(getDefaultValue); } if (arity.IsNonDefault) { rv.Arity = arity; } return(rv); }