I need to use a python script having the following usages:
script.py ( commands ) ( options )
My problem is how i add arguments for "commands" and "options"?
What i did now is this:
parser = argparse.ArgumentParser()
parser._optionals.title = "Options"
parser.add_argument('-help','--help', action="store_true", dest="help", help='help')
subparsers = parser.add_subparsers(help="All available commands", title="Commands")
parser_start = subparsers.add_parser('start', help='Starts the script', add_help=False)
parser_start._optionals.title = "Options"
parser_start.add_argument('--help', action="store_true", dest="help_start")
parser_start.add_argument('-f', type=str, dest="file", help='simulation file to start')
parser_ls = subparsers.add_parser('ls', help='Lists running simulations', add_help=False)
parser_ls._optionals.title = "Options"
parser_ls.add_argument('--help', action="store_true", dest="help_ls")
parser_ls.add_argument('--all', action="store_true", help='Display all simulations')
parser_stop = subparsers.add_parser('stop', help='Stops simulation', add_help=False)
parser_stop._optionals.title = "Options"
parser_stop.add_argument('--help', action="store_true", dest="help_down")
parser_stop.add_argument('--sim-name', type=str, dest="sim_name")
args = parser.parse_args()
If i try to access args.help_up i receive: AttributeError: 'Namespace' object has no attribute 'help_start'
How do i pass the parser_up, parser_stop and parser_ls to the parse_args? And how do i access them afterwards?
Objective is to have custom help messages ( which i have atm that is why i disabled the help ) and to run the script like this:
script.py start -f (name of file)
script.py stop --sim-name (name of simulation)
EDIT:
If i add args2 = parser_start.parse_args() i am able to get a read on args2.help_start, but i am not able to find any of the start, ls or down arguments!