gsscoder / commandline

Terse syntax C# command line parser for .NET with F# support
1.63k stars 293 forks source link

Bool parameters with string values? #477

Closed ajtruckle closed 7 years ago

ajtruckle commented 7 years ago

I have:

[Option('c', "createcalendarevents", Required = false, HelpText = "Creates the events in your calendar.")] public bool CreateCalendarEvents { get; set; }

How do I set it up so that the property is a bool but that we also have a path specified?

nemec commented 7 years ago

I'm not sure what you mean by "also have a path specified". Do you mean value (e.g. true, false)? Bools are converted to 'flags' internally, which means setting is is 'true' and leaving it out is 'false'.

ajtruckle commented 7 years ago

What I mean is, in my code I want a Boolean, eg:

if(options.CreateCalendarEvents)    Do this

So:

command: file -c or file --createcalendarevents

But I also want to specify the path of the file to be read in (to create the events). It seems to me that I either map the option to a string or to some other value. But I was hoping:

file -c="This is the file to the file with the events"

-c will map to the bool property "...." to the associated string

If I have to do this differently please advise the standard.

Thanks.

nemec commented 7 years ago

Thanks, I understand now. Unfortunately that isn't possible - your property is either a boolean (which .NET will not store a string inside) or a string (which you will need to determine the boolean value yourself), but not both.

I would recommend splitting it into two options - one boolean and one string. You could also make the option a string and set the Default parameter to String.Empty. This way, a value of null means -c was not specified, String.Empty means it was specified with no path, and any other value is the path.

ajtruckle commented 7 years ago

It won't get me use a default value of String.Empty.

nemec commented 7 years ago

Use "". They're the same thing but String.Empty isn't a compile time constant. Or use anything, really, even a random string. Just something you can distinguish between "expected input" and "default value".