Skip to content

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

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

Closed
ericnewton76 opened this issue Nov 4, 2017 · 3 comments
Closed

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

ericnewton76 opened this issue Nov 4, 2017 · 3 comments

Comments

@ericnewton76
Copy link
Member

Issue by ptylenda
Tuesday Sep 20, 2016 at 11:30 GMT
Originally opened as gsscoder/commandline#353


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.

@ericnewton76
Copy link
Member Author

Comment by anthonylangsworth
Saturday Oct 22, 2016 at 12:01 GMT


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

@ericnewton76
Copy link
Member Author

Comment by jdelgado2002
Tuesday Oct 10, 2017 at 19:27 GMT


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"

@moh-hassan
Copy link
Collaborator

Nullable bool is preferd.

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

2 participants