Message225484
If you have an optional nargs=argparse.REMAINDER argument defined
then, if specified, its value should contain all the remaining command-line content. However, it seems that value does not include later '--' arguments.
For example, the following works as expected:
import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("--args", nargs=argparse.REMAINDER)
args = parser.parse_args("--args cmd --arg1 XX ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "ZZ"]
But the following fails with
'PROG: error: unrecognized arguments: -- ZZ':
import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("--args", nargs=argparse.REMAINDER)
args = parser.parse_args("--args cmd --arg1 XX -- ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "--", "ZZ"]
Note that the same code works as expected when using a
positional nargs=argparse.REMAINDER arguments:
import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("args", nargs=argparse.REMAINDER)
args = parser.parse_args("args cmd --arg1 XX ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "ZZ"]
args = parser.parse_args("args cmd --arg1 XX -- ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "--", "ZZ"]
But that, of course, does not allow us to have the args value
start with something like "--blah" that looks like an optional
argument.
Hope this helps.
Best regards,
Jurko Gospodnetić |
|
Date |
User |
Action |
Args |
2014-08-18 09:46:02 | Jurko.Gospodnetić | set | recipients:
+ Jurko.Gospodnetić |
2014-08-18 09:46:02 | Jurko.Gospodnetić | set | messageid: <1408355162.18.0.7039636355.issue22223@psf.upfronthosting.co.za> |
2014-08-18 09:46:02 | Jurko.Gospodnetić | link | issue22223 messages |
2014-08-18 09:46:01 | Jurko.Gospodnetić | create | |
|