Recognise "unqualified" parameters?

Sep 8, 2009 at 11:10 AM

I'm switching our application over to using this library for command line parsing.

It currently accepts one command line argument, without any decoration - just a single word, but this word can be anything.

e.g. ourapp.exe someword

ourapp.exe someotherword

What I want to be able to support is the following:

ourapp.exe someword --newoption somevalue --anotheroption 2

Does this library have the ability to recognise an "unqualified" parameter like this, so that we don't have to change shortcuts all over the place?



Sep 10, 2009 at 6:18 AM
Edited Sep 10, 2009 at 6:18 AM

Hi Tom, first of all, thak you for using Command Line Parser Library!

Using the first parameter as qualified option without any dashes is now implicitly (and partly) supported by CommandLine.dll.

In your options instance type you could define a public field using a type that implements IList<string>. Attach ValueList attribute to this field specifying (e.g.) List<string> (like in the sample code) - the name of the field is not important. After successful parsing, check if the list length is equal to 1 otherwise call the usage's method (marked [HelpOption(...)]. Check also if YourOptions.FieldXXX[0] is equal to someword otherwise call Console.Error.WriteLine(YourOptions.GetUsage()).

At the moment you can't use any library's feature to ensure argument's position.

I'll will take your question in regard for formalize this case in DLL's public object model (in a future version).

Thank you,


Sep 12, 2009 at 9:40 AM

Tom - and everyone interested in this discussion,

I forgot one thing ...

You could avoid to check the number of non-option values, using MaximumElements property of ValueList[Attribute]. In your case set it to 1.



Apr 8, 2011 at 11:48 AM

I've been thinking of creating something similiar for sometime so was really pleased to find this.

I want to parse a command line that takes switches and a parameter. For example;

myapp.exe --option1 param1 [param2] --option2 param1

where param2 is optional.

It should fail parsing if you did not supply the correct number of parameters.

It would be great if the HelpText class understood this as well.

Is this supported in the API?