305

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 override this?

1
  • 25
    Very 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! Commented Jan 4, 2017 at 3:55

3 Answers 3

549

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']
Sign up to request clarification or add additional context in comments.

9 Comments

+1 - didn't knew there was some thing like parse_known_args
Nor did I! I even missed it in the docs docs.python.org/library/…. Thanks
This 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 than parser.parse_args([]) in my tests.
FWIW, using parse_known_args rather than parse_args enables the use of ArgumentParser in code within the scope of if __name__ == 'main': (a condition that is True 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)
This doesn't seem to work with optional args that are "known" not being passed in.
|
32

You can puts the remaining parts into a new argument with parser.add_argument('args', nargs=argparse.REMAINDER) if you want to use them.

3 Comments

This works with parse_args() and doesn't require parse_known_args() (on Python 2.7).
Using 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.
Just ran into a long-standing REMAINDER bug today: bugs.python.org/issue17050
10

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.