[Mono-dev] Program Option Parsing Library

Jay Logue jay-MonoDev at toaster.com
Thu Jan 10 17:39:24 EST 2008

Jonathan Pryor wrote:
> I don't see how an out parameter would be better than a return value,
> especially considering that every other .Parse() method in the framework
> actually returns a value.
This is probably well into the realm of personal preference, but my 
reasoning goes like this: The primary purpose of Parse() is not to 
produce a list of the extra, non-option arguments on the command line.  
Rather it is to parse the option arguments and invoke their associated 
actions.  Thus, the non-option arguments are kind of a remainder of the 
function, rather than its result.  Using an out parameter, IMO, make 
this clearer.

This approach also allows for an overload that doesn't take an output 
array, the implication being that an exception will be thrown if any 
non-option arguments are present.
> However, a `bool TryParse(string[] args, out string[] result)' might be
> useful if the use of exceptions is undesirable.  (However, w/o
> exceptions how would the user know what the problem was?)  I'm less sure
> about this idea.
I don't like this idea personally.  You don't gain much by not throwing 
an exception (argument parsing is probably not a performance-sensitive 
operation) and
you want to make the error handling very straightforward and boilerplate 
(so people are likely to get it right).   To that end, if you do end up 
adding a special exception type, I would have the Exception.Message 
value be exactly what should appear on stderr.  E.g.:

        int verbose = 0;
        Options p = new Options () {
            { "v", v => ++verbose },
            { "h|?|help", v => ShowHelp () }

        try {
        catch (OptionException ex) {
            *Console.Error.WriteLine("foo: {0}", ex.Message);*
            Console.Error.WriteLine("Try 'foo --help' for more 
            return 1;
> Thank you for the suggestions.
Thank you for writing this.  I will definitely be using it.

-- Jay

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080110/cf9a042f/attachment.html 

More information about the Mono-devel-list mailing list