Python argparse ignore unrecognised arguments
Solution 1
Replace
args = parser.parse_args()
with
args, unknown = parser.parse_known_args()
For example,
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--foo')
args, unknown = parser.parse_known_args(['--foo', 'BAR', 'spam'])
print(args)
# Namespace(foo='BAR')
print(unknown)
# ['spam']
Solution 2
You can puts the remaining parts into a new argument with parser.add_argument('args', nargs=argparse.REMAINDER)
if you want to use them.
Solution 3
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.
$ program --foo BAR a b +cd e
We will have this Namespaced data collection to work with.
Namespace(_unrecognized_args=['a', 'b', '+cd', 'e'], foo='BAR')
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.
parser = argparse.ArgumentParser(prefix_chars='+')
parser.add_argument('+cd')
The same command will produce
Namespace(_unrecognized_args=['--foo', 'BAR', 'a', 'b'], cd='e')
Put that in your pipe and smoke it =)
nJoy!
Related videos on Youtube
joedborg
Updated on September 22, 2020Comments
-
joedborg over 3 years
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?
-
Alan De Smet over 7 yearsVery handy if you're writing a wrapper to another program, and you want to capture and modify a few arguments, but pass the rest on!
-
dwanderson over 7 yearsExactly why I ended up here @AlanDeSmet ! Glad I'm not trying to do something crazy :)
-
-
avasal over 11 years+1 - didn't knew there was some thing like
parse_known_args
-
joedborg over 11 yearsNor did I! I even missed it in the docs docs.python.org/library/…. Thanks
-
Andy Hayden about 10 yearsThis came up when trying to use nosetest with parseargs (it refused to allow nosetest args to be used) the reason was because I was doing
parser.parse_args(None)
rather thanparser.parse_args([])
in my tests. -
Nikolay Fominyh over 9 yearsYou saved my day. Thx.
-
gumption about 9 yearsFWIW, using
parse_known_args
rather thanparse_args
enables the use ofArgumentParser
in code within the scope ofif __name__ == 'main':
(a condition that isTrue
for all cells in an IPython Notebook ... which I find greatly aids the development and testing code that I want to eventually migrate to a script invoked from a command line) -
gumption about 9 yearsOops: s/main/__main__/
-
Sharud about 7 yearsThis doesn't seem to work with optional args that are "known" not being passed in.
-
OozeMeister almost 7 yearsThis works with
parse_args()
and doesn't requireparse_known_args()
(on Python 2.7). -
Matt almost 7 yearsUsing argparse.REMAINDER seems to be fraught with long-standing bugs. I certainly can't get it to work. parse_known_args() is a good alternative.
-
Scott Carpenter about 6 yearsJust ran into a long-standing REMAINDER bug today: bugs.python.org/issue17050
-
Shital Shah over 4 yearsUnfortunately
unknown
is simply array and there is no built-in way to convert to nice dictionary (as we don't know types). Alternative: stackoverflow.com/a/37367814/207661. -
yoyo about 2 yearsNote that the
unknown
args can then be passed to another argparse instance, for example a subparser.