Option Parsing: Allowing Multiple Argument Values
• CommentsSome options need to take a sequence of argument values. There are several ways to accomplish this using the Nito.KitchenSink Option Parsing library.
Enumeration Flags
If the option values are a series of enumerated flags, then the built-in enumeration parser will handle multiple values automatically:
> CommandLineParsingTest.exe
None
> CommandLineParsingTest.exe /favorite-things Mittens
Mittens
> CommandLineParsingTest.exe /favorite-things Mittens,Kittens
Mittens, Kittens
> CommandLineParsingTest.exe /favorite-things "Mittens, Snowflakes"
Mittens, Snowflakes
> CommandLineParsingTest.exe /favorite-things DogBites
Could not parse DogBites as FavoriteThings
Using a Property Setter for Individual Values
The example above works well enough for enumerations, but not all arguments are that simple. In these situations, we can take advantage of the fact that arguments are applied to the options class by property setters.
The following example allows multiple individual values for an argument. As each argument value is set, it is saved into a collection of values.
Note that using a property setter in this fashion is not a good OOP practice; however, the adverse design affects are contained within the options class.
> CommandLineParsingTest.exe
> CommandLineParsingTest.exe -n 3
3
> CommandLineParsingTest.exe -n 3 -n 6
3, 6
> CommandLineParsingTest.exe -n 3,6
Could not parse 3,6 as Int32
Note that the last test failed; the options class above only allows multiple individual arguments, not a group of values.
Using a Property Setter for Grouped Values
In this case, we want to be able to pass a sequence of values (delimited somehow) as a single argument, and have them interpreted as multiple individual values.
We can again take advantage of the property setter hack, but we have to do our own parsing of the delimited value. We will use a property type of string to prevent automatic parsing.
> CommandLineParsingTest.exe -n 3 -n 6
3, 6
> CommandLineParsingTest.exe -n 3;6
3, 6
This works, but still feels a bit “hackish”. We’re out of time for today, but in a few weeks we’ll revisit this problem when we talk about custom argument parsers.