optparse module

Daniel Keep daniel.keep.lists at gmail.com
Fri Apr 14 12:39:29 PDT 2006



Lars Ivar Igesund wrote:
> Daniel Keep wrote:
> 
>> Frank Benoit wrote:
>>> Or this one
>>>
>>> http://svn.dsource.org/projects/ddl/trunk/utils/ArgParser.d
>> That one's pretty nice; much shorter than mine.
>>
>> But then, mine produces a hash of boxes of pre-formatted values.  With
>> that one, you have to write a new delegate for each option.
> 
> Hmm, I'm not totally sure I understand what you mean, could you provide an
> example?

Well, from what I can see of ArgParser, it parses the arguments, and
calls delegates that you bind to specific argument prefixes.  So I
assume you do the processing on the arguments in the delegates.

With optparse, you use prefab objects (or make your own) whose job is to
tell the parser what names to bind to, and to handle parsing arguments
(much like ArgParser).  The difference is that the parsed value is then
stored in a hash of Box'es.  So if you had this object:

# Object debugLevel = new NumericOption!(int)(
#     "d", // "short" name
#     "debuglevel", // "long" name
#     "DebugLevel") // key to store parsed value under

Then once you've parsed your command line, you can access the final
value like this:

# unbox!(int)(optionParser["DebugLevel"]);

ArgParser does roughly the same thing--optparse just automates a few of
the steps a little.

>> Plus, it doesn't have the way, way cool OptionParser.showHelp() method
>> ^_^.
> 
> Does it print the arguments handled by optparse, like a default help sortof?
> 

Yeah.  For every option that you add, it will print its long and short
names (whichever exist), its argument name (if it has one), and a help
string.  I would also sort them alphabetically, except array.sort
doesn't seem to work if "array" is an array of objects :P

> ArgParser is only meant to be a low level arg/opt parser. I also think it
> should be easy to extend, both internally, and on top of it.
> 

Fair enough.  I wrote optparse basically to handle the 90% of cases I
could think of.  The other 10% should be mostly possible :P

-- 

v1sw5+8Yhw5ln4+5pr6OFma8u6+7Lw4Tm6+7l6+7D
a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP    http://hackerkey.com/



More information about the Digitalmars-d mailing list