iffy / nim-argparse

Argument parsing for Nim
MIT License
120 stars 8 forks source link

With parse(), no way to print help for subcommand #62

Closed iffy closed 1 year ago

iffy commented 3 years ago
var p = newParser:
  command "foo":
    flag("-a")
  command "bar":
    flag("-b")
try:
  var opts = p.parse(@["foo", "--help"])
except ShortCircuit as e:
  if e.flag == "argparse_help":
    echo p.help # how do I get p.fooParser.help or know that they passed in foo?
amaank404 commented 1 year ago

This has been really bugging me out, just asking for status on this issue please. If it is not gonna get fixed anytime soon, please let me know

iffy commented 1 year ago

Turns out it was easy -- thanks for letting me know it was bugging you.

iffy commented 1 year ago

There's now a .help field on the ShortCircuit error object so this should work:

try:
  var opts = p.parse()
except ShortCircuit as err:
  if err.flag == "argparse_help":
    echo err.help

https://github.com/iffy/nim-argparse#parse

amaank404 commented 1 year ago

OMG! You are a legend! Thank you so much

yaroslav-gwit commented 1 year ago

Just wanted to say thank you. Before you implemented the change, I had to use this monkey patch 🙈:

if opts.command == "vm":
    var vmList = vmList()
else:
    echo execCmdEx(os.getAppFilename() & " vm --help")[0].strip

Essentially executing a shell sub-command, and calling --help on it.