I would like to make my python script run from the command line when supplies with some arguments. However, one of the arguments should be a list of options specific to one
I really like the-wolf's approach using variable length collections as explicit string arguments.
In my opinion, nargs='*'
has noteworthy drawbacks: when attempting to collect strings as positional arguments (at least one string must be present), or if you are trying to use subparsers, you will find that nargs='*'
and nargs='+'
use greedy completion, and they don't seem to stop consuming for any good reasons. Even if syntax for an optional argument or a number comes up, the string() type will keep consuming. (This gets harder to anticipate with subparsers).
Best case, arguments placed after (positional and optional) are being ignored, and worse, you are likely passing corrupt data types to the argparse array.
We should be able to define a custom ActionType that is looking for a quoted string. If it finds one, then we adapt the-wolf's examples (nearly verbatim it seems).
This keep things clean in argparse and makes general use of variable collections much less bitchy.