www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Yet another optparse

reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
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.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://pyd.dsource.org
Jan 09 2007
parent reply CyaNox <mark cyanox.nl> writes:
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.
 

Jan 10 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
CyaNox wrote:
 Does your implementation account for the following:
 
 app --file="foo bar"
 app --file "some --file with spaces and quoted"
 

Quoting is taken care of by the shell before the argument even gets to your program. So this: app --file="foo bar" actually becomes this: [app,--file=foo bar] And this: app --file "some --file with spaces and quoted" becomes: [app,--file,some --file with spaces and quoted] My optparse correctly handles both cases. If you want to test for what the shell does for you, just use this simple program: import std.stdio; void main(char[][] args) { writefln(args); } -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Jan 10 2007
parent reply Rueschi <rueschi GIquadrat.de> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kirk McDonald schrieb:
 CyaNox wrote:
 Does your implementation account for the following:

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

Quoting is taken care of by the shell before the argument even gets to your program.

This is not true on Windows systems, where the whole command line is passed in only one string. The parsing is left to the program. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (MingW32) iD8DBQFFpNLLxpVjSwvEWI4RAuw+AKDepC4RezE+17dNx1emsxB348ts5ACeINKZ J9nL+2ur+gNXweSoX3Y6Rqs= =B/SE -----END PGP SIGNATURE-----
Jan 10 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Rueschi wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1
 
 Kirk McDonald schrieb:
 CyaNox wrote:
 Does your implementation account for the following:

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

your program.

This is not true on Windows systems, where the whole command line is passed in only one string. The parsing is left to the program.

Not so. The examples I used in my previous post all worked equally well for me on Windows XP and Linux, and I have been testing optparse on both. -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Jan 10 2007
parent Rueschi <rueschi GIquadrat.de> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Kirk McDonald schrieb:
 Rueschi wrote:
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 Kirk McDonald schrieb:
 CyaNox wrote:
 Does your implementation account for the following:

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

your program.

This is not true on Windows systems, where the whole command line is passed in only one string. The parsing is left to the program.

Not so. The examples I used in my previous post all worked equally well for me on Windows XP and Linux, and I have been testing optparse on both.

So your parser uses the argument array from the main() function which is called by the extern(C) main() from phobos/internal/dmain2.d which is called from the underlying C runtime which calls the Win32 GetCommandLine() API function that returns the whole command line in one string. You should pray that the different C runtime implementations parse the command line string the same way as the linux shell does. If not, you could get different results for exotic arguments :) -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (MingW32) iD8DBQFFpN0NxpVjSwvEWI4RAlmJAKD5Gejwt8YViRM2qvt/r3so2kGZAQCgihMB esvwmPmPLEps7jNqx8oHCrk= =jlF0 -----END PGP SIGNATURE-----
Jan 10 2007