D port of docopt

Colin via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Mon Jun 16 01:12:09 PDT 2014


On Monday, 16 June 2014 at 06:51:41 UTC, Jacob Carlborg wrote:
> On 15/06/14 19:35, Bob Tolbert wrote:
>> In order to learn D, I've worked up a port of the docopt
>> commandline parser (original in Python http://docopt.org).
>>
>> https://github.com/rwtolbert/docopt.d
>>
>> Since this is my first code in D, I apologize in advance for 
>> the
>> mix if Python and C++ idioms. Since this is ported from Python,
>> with the intention of staying compatible with future Python
>> versions, some of that is expected, but I look for this as an
>> chance to learn more about D.
>>
>> It is also a pretty useful way to write commandline interfaces.
>> The included example that mimics the git CLI is pretty 
>> impressive.
>>
>> This is also my first submission as a dub project, so 
>> hopefully I
>> got that right as well.
>>
>> Still needs more tests ported from Python, but it does pass the
>> entire functional test suite for the current Python version.
>
> Pretty cool idea. Are you aware of that in D you can, at 
> compile time, parse the doc string and generate a command line 
> parser for that particular documentation.
>
> Looking at the git example [1], it seems a bit complicated and 
> verbose to use after parsing. To determine which arguments was 
> passed to the application.
>
> [1] 
> https://github.com/rwtolbert/docopt.d/blob/master/examples/git/gitD.d

Im actually in the middle of writing this very thing :)
My docopt parser builds a class (using string mixins) depending
on what fields are required from the help text, and then returns
a type of that class at run time. It's not ready for prime time
yet though, so havent uploaded it.

The idea was to have the interface look like:
auto doc = docopt!(HelpText String)(args);


More information about the Digitalmars-d-announce mailing list