Main Page Options

From NDesk

(Difference between revisions)
Jump to: navigation, search
Revision as of 22:50, 26 January 2008 (edit)
JonPryor (Talk | contribs)
(New page: NDesk.Options is a callback-based program option parser for C#. == NDesk.Options Releases == NDesk.Options has had the following releases (in reverse chronological order): * [http://www...)
← Previous diff
Revision as of 23:14, 26 January 2008 (edit) (undo)
JonPryor (Talk | contribs)

Next diff →
Line 28: Line 28:
: git-clone git://git.ndesk.org/pub/scm/ndesk-options : git-clone git://git.ndesk.org/pub/scm/ndesk-options
 +
 +== Background ==
 +
 +I had been doing a lot of work on [http://www.mono-project.com/Monodocer monodocer], and the warning about [http://www.go-mono.com/docs/index.aspx?tlink=0@N%3aMono.GetOptions Mono.GetOptions] being obsolete was getting annoying. So after killing an afternoon coming up with a similar reflection-based alternative, it dawned on me that (1) any such alternative would not be "simple", and (2) using such a library would have a [http://www.codinghorror.com/blog/archives/001025.html high code overhead]. After thinking back on the previous option parsing libraries I've used, I remembered Perl's [http://perldoc.perl.org/Getopt/Long.html Getopt::Long] library, which permits short and concise declarations of the options a program supports.
 +
 +The result: a cunning use of C# 3.0 collection initializers and lambda delegates:
 +
 + string data = null;
 + bool help = false;
 + int verbose = 0;
 +
 + var p = new OptionSet () {
 + { "file=", v => data = v },
 + { "v|verbose", v => { ++verbose } },
 + { "h|?|help", v => help = v != null },
 + };
 + List<string> extra = p.Parse (args);
 +
 +C# 3.0 features are not required, and OptionSet can be used with C# 2.0:
 +
 + string data = null;
 + bool help = false;
 + int verbose = 0;
 +
 + OptionSet p = new OptionSet ()
 + .Add ("v|verbose", delegate (string v) { if (v != null) ++verbose; })
 + .Add ("h|?|help", delegate (string v) { show_help = v != null; })
 + .Add ("n|name=", delegate (string v) { names.Add (v); });
 + List<string> extra = p.Parse (args);
 +
 +The [http://www.ndesk.org/doc/ndesk-options documentation] [http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html has] [http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html#C:NDesk.Options.OptionSet(System.Converter{System.String,System.String}) more] [http://www.ndesk.org/doc/ndesk-options/NDesk.Options/OptionSet.html#M:NDesk.Options.OptionSet.Add%60%601(System.String,System.String,System.Action{%60%600,NDesk.Options.OptionContext}) examples].

Revision as of 23:14, 26 January 2008

NDesk.Options is a callback-based program option parser for C#.

Contents

NDesk.Options Releases

NDesk.Options has had the following releases (in reverse chronological order):

Features

OptionSet currently supports:

  • Boolean options 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 options 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.

Documentation

Class Documentation.

Development

A git repository is available:

git-clone git://git.ndesk.org/pub/scm/ndesk-options

Background

I had been doing a lot of work on monodocer, and the warning about Mono.GetOptions being obsolete was getting annoying. So after killing an afternoon coming up with a similar reflection-based alternative, it dawned on me that (1) any such alternative would not be "simple", and (2) using such a library would have a high code overhead. After thinking back on the previous option parsing libraries I've used, I remembered Perl's Getopt::Long library, which permits short and concise declarations of the options a program supports.

The result: a cunning use of C# 3.0 collection initializers and lambda delegates:

   string data = null;
   bool help   = false;
   int verbose = 0;
   var p = new OptionSet () {
   	{ "file=",      v => data = v },
   	{ "v|verbose",  v => { ++verbose } },
   	{ "h|?|help",   v => help = v != null },
   };
   List<string> extra = p.Parse (args);

C# 3.0 features are not required, and OptionSet can be used with C# 2.0:

   string data = null;
   bool help   = false;
   int verbose = 0;
   OptionSet p = new OptionSet ()
     .Add ("v|verbose", delegate (string v) { if (v != null) ++verbose; })
     .Add ("h|?|help",  delegate (string v) { show_help = v != null; })
     .Add ("n|name=",   delegate (string v) { names.Add (v); });
   List<string> extra = p.Parse (args);

The documentation has more examples.

Retrieved from "http://ndesk.org/Options"