parse_known_args没有为空输入提供异常

时间:2015-08-05 09:26:15

标签: python

对于2个不同的调用,以下函数的行为方式不同。

第一次,_argv为空并且抛出异常

第二次,_argv为空但不抛出异常

在try块中调用函数run_command。

def run_command(cmd, cmd_parser, sub_argv):
    print ('Entering run_command')
    _argv = sub_argv
    index = -1
    values_specs = []
    if '--' in sub_argv:
        index = sub_argv.index('--')
        _argv = sub_argv[:index]
        values_specs = sub_argv[index:]
    print ('argv is ')
    print (_argv)
    try:
        known_args, _values_specs = cmd_parser.parse_known_args(_argv)
    except Exception as err:
        print('excpetion is')
        print(err)
    print ('known args')
    print (known_args)
    cmd.values_specs = (index == -1 and _values_specs or values_specs)
    print ('Exiting run_command')
    return cmd.run(known_args)

第一次运行时,我得到了正确的异常,被parse_known_arguments抛出。因此程序显示帮助菜单。

get parser full name:%s tacker vnf-create
DEBUG: tackerclient.tacker.v1_0.vm.vnf.CreateVNF Entering CreateCommand::get_parser
DEBUG: tackerclient.tacker.v1_0.vm.vnf.CreateVNF Entering TackerCommand::get_parser
DEBUG: tackerclient.tacker.v1_0.vm.vnf.CreateVNF Exiting TackerCommand::get_parser
Entering CreateVNF::add_known_arguments
Exiting CreateVNF::add_known_arguments
DEBUG: tackerclient.tacker.v1_0.vm.vnf.CreateVNF Exiting CreateCommand::get_parser
Exiting 1 run_subcommand
Entering run_command
before if condition
before parsing args
argv is
usage: tacker vnf-create [-h] [-f {html,json,shell,table,value,yaml}]
                         [-c COLUMN] [--max-width <integer>] [--prefix PREFIX]
                         [--request-format {json,xml}] [--tenant-id TENANT_ID]
                         [--name NAME] --vnfd-id VNFD_ID
                         [--config-file CONFIG_FILE] [--config CONFIG]
tacker vnf-create: error: argument --vnfd-id is required

但是当我运行不同的输入时,parse_known_args行不会抛出任何异常,尽管argv为空。

Entering run_command
before if condition
before parsing args
argv is
known args
Namespace(columns=[], description=None, formatter='table', max_width=0, name=None, prefix='', request_format='json', tenant_id=None, variables=[], vnfd=None, vnfd_file=None)
value specs
[]
Exiting run_command

请帮助!!!

0 个答案:

没有答案