status-im / nim-confutils

Simplified handling of command line options and config files
Apache License 2.0
64 stars 16 forks source link

Infinite loop processing --help if more than one argument is defined #18

Closed Symb0lica closed 4 years ago

Symb0lica commented 4 years ago

The problem is in:

func firstArgIdx(cmd: CmdInfo): int =
  # This will work correctly only if the command has arguments.
  result = cmd.opts.len - 1
  while result > 0:
    if cmd.opts[result - 1].kind != Arg:
      return

which needs the addition of

   else:
      result -= 1

to prevent an infinite loop. Even this relies on the convention that arguments are defined last to produce a correct help message. Parsing the command line seems to work correctly if this convention is not followed, but the help message doesn't mention any such arguments.

A related but purely cosmetic issue is that there is no space between the description of one argument and the name of the next. I would suggest amending describeInvocation to add

    helpOutput "\p"

to the end of the block starting

    for arg in cmd.args:
zah commented 4 years ago

Thanks for investigating this. I've implemented all suggestions with some additional cosmetic improvements.