Thread overview
Yet another optparse
Jan 10, 2007
Kirk McDonald
Jan 10, 2007
CyaNox
Jan 10, 2007
Kirk McDonald
Jan 10, 2007
Rueschi
Jan 10, 2007
Kirk McDonald
Jan 10, 2007
Rueschi
May 13, 2014
Chris Piker
May 13, 2014
Meta
May 14, 2014
Chris Piker
January 10, 2007
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
January 10, 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.
> 
January 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
January 10, 2007
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.
January 10, 2007
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
January 10, 2007
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 :)
May 13, 2014
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 13, 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:
>> 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 14, 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