[Mono-dev] Program Option Parsing Library

Jonathan Pryor jonpryor at vt.edu
Mon Jan 7 14:42:18 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

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:

        Foo f = null;
        var p = new Options () {
                { "foo=", (Foo v) => f = v }

Support is currently highly limited: it only supports "bool" arguments
(e.g. -a, -debug+, -debug-) and arguments with an optional or required
value (e.g. --color=auto, -color=auto, /color=auto).

Furthermore, it does not treat '-' options differently from '--' or '/'
options, and thus does no do any kind of option bundling (e.g. treating
'-lac' as equivalent to '-l -a -c').

Regardless, at ~250 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.


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

More information about the Mono-devel-list mailing list