Skip to content

Cannot pass "true" or "false" to a string option #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ptylenda opened this issue Sep 20, 2016 · 2 comments
Open

Cannot pass "true" or "false" to a string option #353

ptylenda opened this issue Sep 20, 2016 · 2 comments

Comments

@ptylenda
Copy link

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.

@anthonylangsworth
Copy link
Contributor

This appears to be fixed in the master branch, @ptylenda . I cannot reproduce the issue. Please let me know if I am incorrect.

@jdelgado2002
Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants