import argparse parser = argparse.ArgumentParser(exit_on_error=False) subparsers = parser.add_subparsers(required=True) sub = subparsers.add_parser("sub", exit_on_error=False) sub.add_argument("arg", type=int) parser2 = argparse.ArgumentParser(exit_on_error=False) parser2.add_argument("arg", type=int) try: #parser.parse_args([]) # Doesn't work, the process just exits instead of ArgumentError getting caught #parser.parse_args(["invalid"]) # Invalid arguments work, ArgumentError gets caught #parser.parse_args(["sub"]) # Doesn't work, the process just exits instead of ArgumentError getting caught #parser2.parse_args([]) # Doesn't work, the process just exits instead of ArgumentError getting caught parser2.parse_args("a") # Invalid arguments work, ArgumentError gets caught print("okay") except argparse.ArgumentError as ae: print("ArgumentError", ae)