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']
This post is so helpfull and informative.keep updating with more information...
ReplyDeleteAngular Software
Advantages Of Angular JS