how to let the parser print help message rather than error and exit
Posted
by
fluter
on Stack Overflow
See other posts from Stack Overflow
or by fluter
Published on 2010-12-30T09:44:51Z
Indexed on
2010/12/30
9:54 UTC
Read the original article
Hit count: 223
Hi, I am using argparse to handle cmd args, I wanna if there is no args specified, then print the help message, but now the parse will output a error, and then exit. my code is:
def main():
print "in abing/start/main"
parser = argparse.ArgumentParser(prog="abing")#, usage="%(prog)s <command> [args] [--help]")
parser.add_argument("-v", "--verbose", action="store_true", default=False, help="show verbose output")
subparsers = parser.add_subparsers(title="commands")
bkr_subparser = subparsers.add_parser("beaker", help="beaker inspection")
bkr_subparser.set_defaults(command=beaker_command)
bkr_subparser.add_argument("-m", "--max", action="store", default=3, type=int, help="max resubmit count")
bkr_subparser.add_argument("-g", "--grain", action="store", default="J", choices=["J", "RS", "R", "T", "job", "recipeset", "recipe", "task"], type=str, help="resubmit selection granularity")
bkr_subparser.add_argument("job_ids", nargs=1, action="store", help="list of job id to be monitored")
et_subparser = subparsers.add_parser("errata", help="errata inspection")
et_subparser.set_defaults(command=errata_command)
et_subparser.add_argument("-w", "--workflows", action="store_true", help="generate workflows for the erratum")
et_subparser.add_argument("-r", "--run", action="store_true", help="generate workflows, and run for the erratum")
et_subparser.add_argument("-s", "--start-monitor", action="store_true", help="start monitor the errata system")
et_subparser.add_argument("-d", "--daemon", action="store_true", help="run monitor into daemon mode")
et_subparser.add_argument("erratum", action="store", nargs=1, metavar="ERRATUM", help="erratum id")
if len(sys.argv) == 1:
parser.print_help()
return
args = parser.parse_args()
args.command(args)
return
how can I do that? thanks.
© Stack Overflow or respective owner