代码之家  ›  专栏  ›  技术社区  ›  Arian

向命令中添加子命令

  •  0
  • Arian  · 技术社区  · 5 年前

    我有一个python脚本,它从CLI获取输入,对其进行解析并运行相应的函数:

    command subcommand subsubcommand1 --arg1 <value1>

    当用户不提供subsubcommand时,我希望脚本返回一个错误,表示至少必须提供一个受支持的子命令,相反,它返回:

    Namespace object has no attribute func

    代码如下:

    parser = argparse.ArgumentParser(description='The highlevel command')
    parser.add_argument("-v", help="some help text", action='store_true', default=False)
    subparsers = parser.add_subparsers(dest='action')
    subparsers.required = True
    
    subcommand_parser = subparsers.add_parser('subcommand', help='some help text for the subcommand')
    subsub_subparser = subcommand_parser.add_subparsers()
    subsubparser1 = subsub_subparser.add_parser('subsubcommand1', help='some help text for the subsubcommand1')
    subsubparser1.set_defaults(action='subsubcommand_action', func=mylib.subsub1)
    subsubparser1.add_argument('--arg1', required=True, help='arg1')
    subsubparser1.add_argument('--arg2', required=False, default='hello', help='arg2')
    

    如何修复当前错误并显示新错误?

    0 回复  |  直到 5 年前
        1
  •  0
  •   hpaulj    5 年前

    更正我在评论中提到的遗漏:

    import argparse
    
    parser = argparse.ArgumentParser(description='The highlevel command')
    parser.add_argument("-v", help="some help text", action='store_true', default=False)
    subparsers = parser.add_subparsers(dest='action')
    subparsers.required = True
    
    subcommand_parser = subparsers.add_parser('subcommand', help='some help text for the subcommand')
    subsub_subparser = subcommand_parser.add_subparsers(dest='subaction')   # EDIT
    subsub_subparser.required = True             # EDIT
    subsubparser1 = subsub_subparser.add_parser('subsubcommand1', help='some help text for the subsubcommand1')
    subsubparser1.set_defaults(func="mylib.subsub1")          # EDIT
    subsubparser1.add_argument('--arg1', required=True, help='arg1')
    subsubparser1.add_argument('--arg2', required=False, default='hello', help='arg2')
    
    subsubparser2 = subsub_subparser.add_parser('subsubcommand2')  # EDIT
    
    args = parser.parse_args()     
    print(args)               # EDIT
    if hasattr(args, 'func'):
        print('func: ',args.func)
    else:
        print('func not defined')
    

    运行示例:

    2020:~/mypy$ python3 stack56435945.py 
    usage: stack56435945.py [-h] [-v] {subcommand} ...
    stack56435945.py: error: the following arguments are required: action
    
    2021:~/mypy$ python3 stack56435945.py subcommand
    usage: stack56435945.py subcommand [-h] {subsubcommand1,subsubcommand2} ...
    stack56435945.py subcommand: error: the following arguments are required: subaction
    
    2021:~/mypy$ python3 stack56435945.py subcommand subsubcommand1 --arg1 foobar
    Namespace(action='subcommand', arg1='foobar', arg2='hello', func='mylib.subsub1', subaction='subsubcommand1', v=False)
    func:  mylib.subsub1
    
    2022:~/mypy$ python3 stack56435945.py subcommand subsubcommand2
    Namespace(action='subcommand', subaction='subsubcommand2', v=False)
    func not defined