p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.58k stars 244 forks source link

Arguments following another argument with explicit .choices() will incorrectly inherit the choices #370

Open tlemo opened 1 month ago

tlemo commented 1 month ago
  args.add_argument("--foo");
  args.add_argument("--bar").choices("a", "b", "c");

With the latest version, passing foo before bar works as expected:

test --foo=x --bar=a

But if foo is passed after bar it's incorrectly rejected:

test --bar=a --foo=x

Invalid argument "--foo" - allowed options: {a, b, c}"
p-ranav commented 1 month ago

This was fixed here. I'll make a new release soon (long overdue). In the meantime, can you check with the latest commit in the main branch?

p-ranav commented 1 month ago

Created a new release: https://github.com/p-ranav/argparse/releases/tag/v3.1

mikisch81 commented 3 weeks ago

@p-ranav I still see this issue with 3.1 version

mikisch81 commented 3 weeks ago

@p-ranav Ok to be more specific the issue happens when I set --bar to accept variable length of arguments:

  argparse::ArgumentParser args("arg_test");
  args.add_argument("--foo");
  args.add_argument("--bar")
      .nargs(1, 3)
      .choices("a", "b", "c");

So if I give all 3 choices it works ok, but if I give 1 or 2, then it fails:

> ./arg_test --bar a b c --foo x
>
> ./arg_test --bar a b --foo x
Invalid argument "--foo" - allowed options: {a, b, c}
Usage: arg_test [--help] [--version] [--foo VAR] [--bar VAR...]

Optional arguments:
  -h, --help     shows help message and exits
  -v, --version  prints version information and exits
  --foo
  --bar          [nargs=1..3]
>
Eng-MohamedHussien commented 2 weeks ago

Hi @p-ranav @mikisch81, Regarding variable length list of arguments:

program.add_argument("--input_files")
  .nargs(1, 3);  // This accepts 1 to 3 arguments.
Eng-MohamedHussien commented 2 weeks ago