Python argparse ignore unrecognised arguments -


optparse, old version ignores unrecognised arguments , carries on. in situations, isn't ideal , changed in argparse. there few situations want ignore unrecognised arguments , parse 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 overwrite this?

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'] 

Comments

  1. This post is so helpfull and informative.keep updating with more information...
    Angular Software
    Advantages Of Angular JS

    ReplyDelete

Post a Comment

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Socket.connect doesn't throw exception in Android -

SPSS keyboard combination alters encoding -