Why use argparse rather than optparse?

As of python 2.7, optparse is deprecated, and will hopefully go away in the future. argparse is better for all the reasons listed on its original page (https://code.google.com/archive/p/argparse/): handling positional arguments supporting sub-commands allowing alternative option prefixes like + and / handling zero-or-more and one-or-more style arguments producing more informative usage messages providing a much … Read more

Python argparse ignore unrecognised arguments

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