[Mono-dev] Program Option Parsing Library
Jonathan Pryor
jonpryor at vt.edu
Fri Jan 11 06:26:47 EST 2008
On Thu, 2008-01-10 at 14:39 -0800, Jay Logue wrote:
> 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
Indeed. I like return values. :-)
Which just leaves the question of what _type_ of return value:
List<string> or string[]. Currently I'm using List<string> because (I
figure) it'll require less memory duplication, as I can use a
List<string> internally to store the unprocessed arguments. For a
string[] I'd *still* use a List<string> internally, there would just be
a final `list.ToArray();', which creates a new array...
I'm also not going to have a TryParse() overload, as it would provide no
information about why parsing failed.
> 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 {
> p.Parse(args);
> }
> catch (OptionException ex) {
> Console.Error.WriteLine("foo: {0}", ex.Message);
> Console.Error.WriteLine("Try 'foo --help' for more
> information");
> return 1;
> }
Done. Test.CheckExceptions() now checks the exception message, so you
can see what kind of messages are now produced.
Attached is a new version of Options.cs. Major changes:
- Type conversion error messages are more useful.
- New OptionException type thrown when parsing fails instead of
InvalidOperationException.
- Options.Parse() returns List<string> of unhandled options.
- Better Option argument checking.
Now how should localization be handled? Should it?
- Jon
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Options.cs
Type: text/x-csharp
Size: 21079 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20080111/3f6545b5/attachment.bin
More information about the Mono-devel-list
mailing list