dim-an/cod

add support for docopt style help

Opened this issue · 3 comments

It would be nice if cod could learn help in the format proposed by docopt http://docopt.org/

for example if i try to make cod learn their default example:

$ ./naval_fate --help
Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate -h | --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
┌──> /tmp/naval_fate --help                                                                                                                                                                                                         
└─── cod: learn this command? [yn?] > y
cod: learned completions: "--speed=" "--moored" "--drifting" and 3 more

Only flag options are learned. However I'm not sure how wildly spread the docopt format actually is...

It is nice idea, I looked at docopt some time ago, but I don't use docopt myself. Do you use it in your projects?

I try to but I fail a lot :D. It seems to be the only well defined specification for writing help texts that i can find so everytime i need to write help i end up looking at docopt

Perhaps i will try to make a pull request, i was wanting to learn go for a while now anyways...

PRs are welcome :)

Here is list of supported parsers:

https://github.com/dim-an/cod/blob/master/parse_doc/parse_help.go#L30-L33

Last one is default parser that parses everything. Special parsers go before default parser (right now we only have argparse parser). They return error if they think that they don't support current help text.

res, err = parsers[idx].Parse(ctx)

You can get inspiration from argparse parser and its test.

func (argparseParser) Parse(context parseContext) (res *parseResult, err error) {

https://github.com/dim-an/cod/blob/de10c9bfff394dd59f302515776f1413f26e5a49/parse_doc/argparse_test.go