Python argparse REMAINDER is not clear

argparse, arguments, python

Solution

I ran into this while trying to dispatch options to an underlying utility. The solution I wound up using was `nargs='*'` instead of `nargs=argparse.REMAINDER`, and then just use the "pseudo-argument" `--` to separate the options for my command and the underlying tool:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--myflag', action='store_true')
>>> parser.add_argument('toolopts', nargs='*')
>>> parser.parse_args('--myflag -- -a --help'.split())
Namespace(myflag=True, toolopts=['-a', '--help'])

This is reasonably easy to document in the help output.

Problem

As documentation suggests: argparse.REMAINDER. All the remaining command-line arguments are gathered into a list. This is commonly useful for command line utilities that dispatch to other command line utilities: ``` >>> parser = argparse.ArgumentParser(prog='PROG') >>> parser.add_argument('--foo') >>> parser.add_argument('command') >>> parser.add_argument('args', nargs=argparse.REMAINDER) >>> print parser.parse_args('--foo B cmd --arg1 XX ZZ'.split()) Namespace(args=['--arg1', 'XX', 'ZZ'], command='cmd', foo='B') ``` I tried to use this to exactly the same purpose, but in some circumstances it seems buggy for me (or perhaps I get the concept wrong): ``` import argparse a = argparse.ArgumentParser() a.add_argument('-qa', nargs='?') a.add_argument('-qb', nargs='?') a.add_argument('rest', nargs=argparse.REMAINDER) a.parse_args('-qa test ./otherutil bar -q atr'.split()) ``` Result: ``` test.py: error: ambiguous option: -q could match -qa, -qb ``` So apparently, if the `otherutil` has such arguments which somehow "collide" with the arguments given to `argparse`, it doesn't seem to work correctly. I would expect when `argparse` reaches the `REMAINDER` kind of argument, it just uses up all the strings in the end of the list without any further parsing. Can I reach this effect somehow?

Original source

Related problems