Handle invalid arguments with argparse in Python
问题 I am using argparse to parse command line arguments and by default on receiving invalid arguments it prints help message and exit. Is it possible to customize the behavior of argparse when it receives invalid arguments? Generally I want to catch all invalid arguments and do stuff with them. I am looking for something like: parser = argparse.ArgumentParser() # add some arguments here try: parser.parse_args() except InvalidArgvsError, iae: print "handle this invalid argument '{arg}' my way!"