digitalmars.D - Yet another optparse
- Kirk McDonald (37/37) Jan 09 2007 Knowing that D already has (by my count) three command-line argument
- CyaNox (5/49) Jan 10 2007 Does your implementation account for the following:
- Kirk McDonald (21/26) Jan 10 2007 Quoting is taken care of by the shell before the argument even gets to
- Rueschi (11/20) Jan 10 2007 -----BEGIN PGP SIGNED MESSAGE-----
- Kirk McDonald (7/22) Jan 10 2007 Not so. The examples I used in my previous post all worked equally well
- Rueschi (16/35) Jan 10 2007 -----BEGIN PGP SIGNED MESSAGE-----
- Chris Piker (11/19) May 12 2014 This code does not compile with the current version of phobos.
- Meta (2/24) May 13 2014 Have you looked up std.getopt?
- Chris Piker (7/8) May 13 2014 Yes [1], it works fine inside it's limited domain. Your old
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
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
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
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Kirk McDonald schrieb:CyaNox wrote: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-----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.
Jan 10 2007
Rueschi wrote:-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Kirk McDonald schrieb: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.orgCyaNox wrote: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.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.
Jan 10 2007
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Kirk McDonald schrieb:Rueschi wrote: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----------BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Kirk McDonald schrieb: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.CyaNox wrote: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.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.
Jan 10 2007
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.dThis 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
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:Have you looked up std.getopt?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.dThis 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 13 2014
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