I have this command, made with argparse, that can either submit or read an item.
Here is a dummy version of what I am trying to do:
if __name__ == "__main__":
# Create the CLI Arguments parser
parser = argparse.ArgumentParser(prog="dummy",
allow_abbrev=False,
description="Dummy command for StackOverflow")
# Create subparsers to add several commands
subparsers = parser.add_subparsers(help="CLI options")
# Command to submit
submit_parser = subparsers.add_parser("submit", help="Submit a new Job")
#submit_parser.set_defaults(func=sub)
submit_parser.add_argument("job_id", type=str, help="The Job ID")
submit_parser.add_argument("job_file", type=str, help="Path to the Job file")
# Command to read Job
status_parser = subparsers.add_parser("read", help="Read the Job Status")
#status_parser.set_defaults(func=status)
status_parser.add_argument("job_id", help="The Job ID")
# Parse the command
parse_res = parser.parse_args()
#parse_res.func(parse_res)
Now, if I type mycommand -h
, I get what I expect:
If I try to use the sub commands without their arguments, I get what I expect as well:
But if i put nothing, no argument, just the naked command, I get nothing. No output, no print, no help, no error. Worse: if I try to associate functions to the sub commands, the naked command raises this error:
How do I get ArgParse to print an help or an error when the command has no argument, just like the sub commands do ?