commandline
commandline copied to clipboard
Cannot pass "true" or "false" to a string option
Define a string option:
[Option('e', "engineering", SetName = "console", Required = false, HelpText = "Flag whether the build is engineering build", ")]
public string Engineering { get; set; }
It is intended to support not only "true" and "false" values and therefore is not boolean (and also not an enum, because it gives us less flexibility)
When you call the executable with "-e true", you receive:
Unhandled Exception: System.ArgumentException: Object of type 'System.Boolean' c
annot be converted to type 'System.String'.
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo
culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo cul
ture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder bi
nder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, Bindi
ngFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invoke
Attr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, O
bject[] index)
at CommandLine.Core.ReflectionExtensions.SetValue[T](PropertyInfo property, T
instance, Object value)
at CommandLine.Core.ReflectionExtensions.<>c__DisplayClass6_0`1.<SetPropertie
s>b__0(T current, SpecificationProperty specProp)
at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source
, TAccumulate seed, Func`3 func)
at CommandLine.Core.InstanceBuilder.<>c__DisplayClass0_2`1.<Build>b__15()
at CommandLine.Core.InstanceBuilder.<>c__DisplayClass0_0`1.<Build>b__7()
at CommandLine.Parser.ParseArguments[T](IEnumerable`1 args)
I think it should be possible to assign ANY text value to a string object, it should not be limited by automatic type recognition.
This appears to be fixed in the master branch, @ptylenda . I cannot reproduce the issue. Please let me know if I am incorrect.
I think I got the boolean logic down @ptylenda , if you include the flag, in your case -e, then it will get mapped to true, excluding it will set it to false.
regardless of what I pass as a parameter I was always getting "true"