[Mono-dev] Program Option Parsing Library

Jonathan Pryor jonpryor at vt.edu
Wed Jan 9 14:23:23 EST 2008


I've been doing a lot of work on monodocer, and (for some unknown
reason) decided that the warning about the deprecation of
Mono.GetOptions was annoying so I thought I'd come up with a
replacement.

This replacement is NOT currently intended to be stable, nor to be
bundled with Mono itself for public use.

It is also extremely crack-addled, which is why I'm liking it so much,
and why I thought I'd share it with you.

Crack-addled?  How else would you describe this cunning combination of
collection initializers and lambda delegates?

        bool help = false;
        int verbose = 0;
        string source = null;
        
        var p = new Options () {
                { "h|?|help",  v => help = v != null },
                { "v|verbose", v => { ++verbose; } },
                { "source=",   v => source = v },
        };
        p.Parse (new string[]{"--help", "-v", "-v", "-source=foo"})
                .ToArray ();
        
After p.Parse().ToArray(), help=true, verbose=2, and source="foo".

It is inspired by Perl's Getopt::Long library, except that all option
processing is done via delegates (i.e. Perl `sub's) and not via by-ref
variables (as Perl also supports).

No reflection is used unless you use TypeConverter to implicitly convert
strings to random managed types:

        int n = 0;
        var p = new Options () {
                { "n=", (int v) => n = v }
        };

Options currently supports:

      * Parameters of the form: -flag, --flag, /flag, -flag=value,
        --flag=value, /flag=value, -flag:value,
        --flag:value, /flag:value, -flag value, --flag value, /flag
        value.
      * "boolean" parameters of the form: -flag, --flag, and /flag.
        Boolean parameters can have a `+' or `-' appended to explicitly
        enable or disable the flag (in the same fashion as mcs -debug+).
        For boolean callbacks, the provided value is non-null for
        enabled, and null for disabled.
      * "value" parameters with a required value (append `=' to the
        option name) or an optional value (append `:' to the option
        name). The option value can either be in the current option
        (--opt=value) or in the following parameter (--opt value). The
        actual value is provided as the parameter to the callback
        delegate, unless it's (1) optional and (2) missing, in which
        case null is passed.
      * "bundled" parameters which must start with a single `-' and
        consists of only single characters. In this manner, -abc would
        be a shorthand for -a -b -c.
      * Option processing is disabled when -- is encountered.

Furthermore, it does not treat '-' options differently from '--' or '/'
options, aside from the aforementioned bundling support.

Regardless, at ~270 LOC for the associated classes (not including
comments or tests), I think it's a reasonably concise and useful library
for command-line option processing.

See also: 
    http://www.jprl.com/Blog/archive/development/mono/2008/Jan-07.html

Thoughts?

- Jon

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Options.cs
Type: text/x-csharp
Size: 18529 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080109/ceb781b0/attachment.bin 


More information about the Mono-devel-list mailing list