不输入可选项的时候不能识别命令,请各位大大帮我看看怎么回事。
代码如下:
"""Naval Fate. Usage: access-list <access-list-number> [dynamic <dynamic-name> [timeout <minutes>]] (deny | permit) <protocol> <source> <source-wildcard> <destination> <destination-wildcard> [precedence <precedence> | dscp <dscp> | tos <tos> | time-range <time-range-name> | fragments | log [<word>] | log-input [<word>]] Options: """ from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__, version='Naval Fate 2.0') print(" {0:<40}{1:<20}{2:<20}".format("kye", "value", "type")) print(" {0:-<80}".format("")) for k,v in arguments.items(): print(" {0:<40}{1:<20}{2:<20}".format(str(k), str(v), str(type(v))))
[dynamic <dynamic-name> [timeout <minutes>]]是可选项,如果不输入这个可选项的话docopt不能识别这个命令:
C:\share>py access-list.py 1 deny ip any any any any
Usage:
access-list <access-list-number> [dynamic <dynamic-name> [timeout <minutes>]] (deny | permit) <protocol> <source> <source-wildcard> <destination> <destination-wildcard> [precedence <precedence> | dscp <dscp>
| tos <tos> | time-range <time-range-name> | fragments | log [<word>] | log-input [<word>]]
把这个可选项加到命令之后才能识别:
C:\share>py access-list.py 1 dynamic dynamic-name timeout 2 deny ip any any any any
-------------------------------------------------------------------------------- <access-list-number> 132 <class 'str'> dynamic True <class 'bool'> <dynamic-name> dynamic-name <class 'str'> timeout True <class 'bool'> <minutes> 2 <class 'str'> deny True <class 'bool'> permit False <class 'bool'> <protocol> ip <class 'str'> <source> any <class 'str'> <source-wildcard> any <class 'str'> <destination> any <class 'str'> <destination-wildcard> any <class 'str'> precedence False <class 'bool'> <precedence> None <class 'NoneType'> dscp False <class 'bool'> <dscp> None <class 'NoneType'> tos False <class 'bool'> <tos> None <class 'NoneType'> time-range False <class 'bool'> <time-range-name> None <class 'NoneType'> fragments False <class 'bool'> log False <class 'bool'> <word> None <class 'NoneType'> log-input False <class 'bool'>
可是用中括号[]括起来的是可选项啊,应该是可输入也可不输入的吧。
上面的输出结果里,最后的<word>没有被识别出来,希望这个信息可以给各位大大参考。