iffy / nim-argparse

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

Passing an option value without `=` breaks parsing multiple arguments #77

Closed ZoomRmc closed 2 years ago

ZoomRmc commented 2 years ago
import argparse

when isMainModule:
  var p = newParser:
    option("-n", "--number", default=some("0"))
    arg("inputA", nargs = 1)
    arg("inputB", nargs = 1)

  try:
    let opts = p.parse(@["-n 2", "A", "B"])
  except UsageError:
    stderr.writeLine getCurrentExceptionMsg()
    quit(1)

Output: Unknown argument(s): B Changing "-n 2" to "-n=2" works as expected.

This passes, which is unexpected (and, IMO, broken):

import argparse

when isMainModule:
  var p = newParser:
    option("-n", "--number", default=some("0"))
    arg("inputA", nargs = 1)
    arg("inputB", nargs = 1)
  let opts = p.parse(@["-n 2", "A"])
  doAssert opts.number == "0"
  doAssert opts.inputA == "-n 2"
  doAssert opts.inputB == "A"

It looks like argparse can't parse a value for an option without =. The docs are not clear on the rules, the only mention of allowed format is in the table on the doc page:

option(...) option with argument (e.g. --output foo)

Which means passing whitespace delimited option values should work.

iffy commented 2 years ago

You would need to do let opts = p.parse(@["-n", "2", "A"]) Does that work?

Edit to add: Doing @["-n 2", "A"] is equivalent to running the program from a shell like this: > myprog '-n 2' A

ZoomRmc commented 2 years ago

Yep, sorry for the bad issue. Bitten by trying to apply a broken test before running the actual program to test manually. :D

The part on explicitly listing available value passing syntax still stands: