[Mono-list] Program Option Parsing Library

Dan Shryock dan.shryock at gmail.com
Fri Jan 11 18:18:17 EST 2008


I wanted to give your library a try so I upgraded to mono 1.2.6, and
tried to compile the attachment using the following line:

gmcs -langversion:linq -define:TEST Options.cs

The compiler gives several errors, all seem to be related to the
implicit return types of the lambda function not being convertable to
void.  Here's the first error:

Options.cs(420,48): error CS0029: Cannot implicitly convert type
`string' to `void'
Options.cs(420,48): error CS1662: Cannot convert `lambda expression'
to delegate type `System.Action<string>' because some of the return
types in the block are not implicitly convertible to the delegate
return type

Which happens on this block of code:

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

Should I be using a different version of the compiler or passing some
other parameters? Did I somehow not get my mono install upgraded
correctly (I used rpm -Uvh with the opensuse 10.2 x64 rpms from the
single download zip file which matches my distro)?

Any help would be greatly appreciated.

Dan


On Jan 7, 2008 11:42 AM, Jonathan Pryor <jonpryor at vt.edu> wrote:
>
> 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:
>
>         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.
>
> Thoughts?
>
> - Jon
>
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>


More information about the Mono-list mailing list