| C# option parsing library |
[Feb. 10th, 2008|07:54 pm] |
| [ | Current Mood |
| | accomplished | ] | I wrote a command line option parsing library in C#, based on Perl/CPAN's Getopt::Long. I think it is better than all the others available for C#.
Features include: - Scalar, array, or hash types for option arguments - Incrementable options (e.g. --verbose --verbose --verbose for 3x verbose) - Option bundling if enabled (-zxvf=foo.tar.gz is the same as --gzip --extract --verbose --file foo.tar.gz) - Generic argument types: you can ask that arguments be returned in any numeric type, string or bool, and it Does What You Mean - Optional events to hook into in order to create custom options
Here's an example (NXGetoptLong/t/Simple.cs):
using System;
using System.Collections.Generic;
using NX.Getopt.Long;
using C = System.Console;
namespace NX.Getopt.Long.Tests {
public class Simple {
public static void Main(string[] args)
{
var p = new Parser();
var gzip = new VoidOption("gzip") { Aliases = new string[] { "z" }
};
var extract = new VoidOption("extract") { Aliases = new string[]
{ "x" } };
var verbose = new IncrementableOption("verbose");
var file = new ScalarOption<string>("file");
p.Bundling = Parser.BundlingOptions.Enabled;
p.GetOptions(args, new Option[] { gzip, extract, verbose, file });
C.WriteLine(gzip.Value);
C.WriteLine(extract.Value);
C.WriteLine(verbose.Value);
C.WriteLine(file.Value);
}
}
}
Get the code here. |
|
|