argparse icon indicating copy to clipboard operation
argparse copied to clipboard

Support parsing arguments partially

Open igorcoding opened this issue 7 years ago • 2 comments

It is very useful to parse args partially, like in Python - https://docs.python.org/3/library/argparse.html#partial-parsing

Typical usecase - add additional options, depending on the value of another option.

igorcoding avatar Jul 05 '18 18:07 igorcoding

igorcoding, there is mutex(...) method for your use-case.

iridiumlynx avatar Sep 28 '18 14:09 iridiumlynx

How does it solve my use case? My goal is to call parse multiple times, for example

  1. User calls program with arguments ./my_program --template=basic --basic_option1=value1
  2. In lua we define argument --template to argparse
  3. Call a hypothetical parse_known_args to parse only arguments we defined and ignore others (because right now parse() fails if encounters unknown args)
  4. Then depending on value of passed template argument we add additional arguments to argparse, using parser:option("--basic_option1", "Option 1")
  5. And then call parse() again and receiving a complete set of user-specified arguments.

This is also useful with the help messages - collect all arguments and only then display all valid options even if user specified template, i.e. ./my_program --template=basic -h

igorcoding avatar Sep 30 '18 16:09 igorcoding