Command-line arguments

Kirk McDonald kirklin.mcdonald at gmail.com
Fri Jul 4 17:17:09 PDT 2008


Matt wrote:
> Is there an established library in D for handling command-line arguments?
> 
> In particular:
> - handling differences between Windows and UNIX shells (i.e. wildcard expansion)
> - handling equivalent options such as "-n 10" === --count=10
> - handling combination of multiple flags, e.g. "-lcf" == "-l -f -c"
> 
> Thanks
> 
> Matt

I wrote one of these a while back, which is based on Python's optparse 
library. I wrote it to work in both Phobos and Tango, but I haven't 
tested it against any recent versions of Tango.

It handles your second and third bullet points. It is relatively stupid 
about the first one. (It relies on the shell to quote things, etc.)

Two source files:
http://dsource.org/projects/pyd/browser/misc/optimpl.d
http://dsource.org/projects/pyd/browser/misc/optparse.d

Example for Phobos:
http://dsource.org/projects/pyd/browser/misc/opttest.d

Example for Tango:
http://dsource.org/projects/pyd/browser/misc/opttest_tango.d

Documentation:
http://dsource.org/projects/pyd/browser/misc/optparse.html

If you've ever used Python's optparse, the API should be at least a 
little familiar. Otherwise, well, it's not that complicated. :-) The 
examples should give you a sense of what it looks like.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list