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
next sibling 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"
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.
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"
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.
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
prev sibling parent reply "Chris Piker" <chris hoopjump.com> writes:
On Wednesday, 10 January 2007 at 03:57:58 UTC, 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
This code does not compile with the current version of phobos. Most updates are straight forward except for one loop using an old version of find. Has anyone out there updated this old module? If so I would find it useful. Please let me know if it's considered bad form on this forum to revive old (in this case ancient) threads. Thanks -- Chris
May 12 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Tuesday, 13 May 2014 at 06:54:08 UTC, Chris Piker wrote:
 On Wednesday, 10 January 2007 at 03:57:58 UTC, 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
This code does not compile with the current version of phobos. Most updates are straight forward except for one loop using an old version of find. Has anyone out there updated this old module? If so I would find it useful. Please let me know if it's considered bad form on this forum to revive old (in this case ancient) threads. Thanks -- Chris
Have you looked up std.getopt?
May 13 2014
parent "Chris Piker" <chris hoopjump.com> writes:
On Tuesday, 13 May 2014 at 13:10:22 UTC, Meta wrote:
 Have you looked up std.getopt?
Yes [1], it works fine inside it's limited domain. Your old module provideds many of the features that I (and what few users I have) expect in a command line interface. -- [1] http://forum.dlang.org/thread/azbykdclfozvgdabffef forum.dlang.org
May 13 2014