How to parse multiple nested sub-commands using python argparse?

后端 未结 11 1119
执念已碎
执念已碎 2020-11-28 20:52

I am implementing a command line program which has interface like this:

cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...]
<         


        
11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 21:42

    I came up with the same qustion, and it seems i have got a better answer.

    The solution is we shall not simply nest subparser with another subparser, but we can add subparser following with a parser following another subparser.

    Code tell you how:

    parent_parser = argparse.ArgumentParser(add_help=False)                                                                                                  
    parent_parser.add_argument('--user', '-u',                                                                                                               
                        default=getpass.getuser(),                                                                                                           
                        help='username')                                                                                                                     
    parent_parser.add_argument('--debug', default=False, required=False,                                                                                     
                               action='store_true', dest="debug", help='debug flag')                                                                         
    main_parser = argparse.ArgumentParser()                                                                                                                  
    service_subparsers = main_parser.add_subparsers(title="service",                                                                                         
                        dest="service_command")                                                                                                              
    service_parser = service_subparsers.add_parser("first", help="first",                                                                                    
                        parents=[parent_parser])                                                                                                             
    action_subparser = service_parser.add_subparsers(title="action",                                                                                         
                        dest="action_command")                                                                                                               
    action_parser = action_subparser.add_parser("second", help="second",                                                                                     
                        parents=[parent_parser])                                                                                                             
    
    args = main_parser.parse_args()   
    

提交回复
热议问题