cod icon indicating copy to clipboard operation
cod copied to clipboard

add support for docopt style help

Open zeteref opened this issue 2 years ago • 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...

zeteref avatar Jun 17 '22 10:06 zeteref

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?

dim-an avatar Jun 17 '22 12:06 dim-an

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...

zeteref avatar Jun 17 '22 13:06 zeteref

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.

https://github.com/dim-an/cod/blob/de10c9bfff394dd59f302515776f1413f26e5a49/parse_doc/parse_help.go#L52

You can get inspiration from argparse parser and its test. https://github.com/dim-an/cod/blob/de10c9bfff394dd59f302515776f1413f26e5a49/parse_doc/argparse.go#L318 https://github.com/dim-an/cod/blob/de10c9bfff394dd59f302515776f1413f26e5a49/parse_doc/argparse_test.go

dim-an avatar Jun 17 '22 13:06 dim-an