Optparse, the old version just ignores all unrecognised arguments and carries on. In most situations, this isn't ideal and was changed in argparse. But there are a few situations where you want to ignore any unrecognised arguments and parse the ones you've specified.
For example:
parser = argparse.ArgumentParser()
parser.add_argument('--foo', dest="foo")
parser.parse_args()
$python myscript.py --foo 1 --bar 2
error: unrecognized arguments: --bar
Is there anyway to overwrite this?
You can puts the remaining parts into a new argument with
parser.add_argument('args', nargs=argparse.REMAINDER)
if you want to use them.Actually argparse does still "ignore"
_unrecognized_args
. As long as these "unrecognized" arguments don't use the default prefix you will hear no complaints from the parser.Using @anutbu's configuration but with the standard
parse.parse_args()
, if we were to execute our program with the following arguments.We will have this Namespaced data collection to work with.
If we wanted the default prefix
-
ignored we could change the ArgumentParser and decide we are going to use a+
for our "recognized" arguments instead.The same command will produce
Put that in your pipe and smoke it =)
nJoy!
Replace
with
For example,