Yet another optparse

CyaNox mark at cyanox.nl
Wed Jan 10 00:14:15 PST 2007


Does your implementation account for the following:

app --file="foo bar"
app --file "some --file with spaces and quoted"

Greetings.

Kirk McDonald wrote:
> Knowing that D already has (by my count) three command-line argument 
> parsers, I have gone and written my own, anyway. As with at least one 
> other of the parsers that I've seen, it is (at least loosely) based on 
> Python's optparse library. You can find it here:
> 
> http://dsource.org/projects/pyd/browser/misc/optparse.d
> 
> An example of its use can be found here:
> 
> http://dsource.org/projects/pyd/browser/misc/opttest.d
> 
> Or right here:
> 
> module test;
> import optparse;
> 
> void main(char[][] args) {
>     auto parser = new OptionParser;
>     // Stores the option's argument.
>     parser.add_option("-f", "--file");
>     // Appends the option's argument to a list.
>     parser.add_option(["-I", "--import"], Action.Append);
>     auto options = parser.parse_args(args);
> 
>     char[] file = options["file"];
>     char[][] imports = options.list("import");
>     writefln("file: ", file);
>     writefln("imports: ", imports);
>     // Any arguments that don't start with '-' are stored
>     // in the args array.
>     writefln("leftovers: ", options.args);
> }
> 
> $ ./test -Isomedir --import otherdir --file=somefile anotherfile
> file: somefile
> imports: [somedir,otherdir]
> leftovers: [anotherfile]
> 
> Optparse has a number of other features, including support for callbacks 
> and integer arguments. The opttest.d file above shows off some of these 
> features.
> 
> It's released under the MIT license, so feel free to use it for whatever.
> 



More information about the Digitalmars-d mailing list