帮助标签中的文本未显示

时间:2018-08-10 06:03:15

标签: python

我有以下代码示例:

commandParser = argparse.ArgumentParser(description = 'Applications Management Operation', add_help = True)
commandParser.add_argument('-ssl', required = False, default = False, dest = 'sslEnabled', help = 'SSL Enabled')
args = commandParser.parse_known_args()[0]
commandParser.add_argument('-t', required = False, default = getDefaultTEAServerURL(args.sslEnabled), dest = 'serverURL', help = 'TEA Server URL')
commandParser.add_argument('-u', required = True, dest = 'userName', help = 'TEA User Name')
commandParser.add_argument('-p', required = True, dest = 'userPwd', help = 'TEA User Password')
commandParser.add_argument('-sc', required = False, default = '', dest = 'serverCert', help = 'Server certificate Path')
commandParser.add_argument('-cc', required = False, default = '', dest = 'clientCert', help = 'Client certificate Path')

当我像

一样奔跑时
 python applicationsMgmt.py --help

我得到了

usage: applicationsMgmt.py [-h] [-ssl SSLENABLED]

Applications Management Operations CLI.

optional arguments:
 -h, --help       show this help message and exit
 -ssl SSLENABLED  SSL Enabled

为什么其他帮助文本不显示?

2 个答案:

答案 0 :(得分:0)

将代码args = commandParser.parse_known_args()[0]移至底部。

commandParser = argparse.ArgumentParser(description = 'Applications Management Operation', add_help = True)
commandParser.add_argument('-ssl', required = False, default = False, dest = 'sslEnabled', help = 'SSL Enabled')
commandParser.add_argument('-t', required = False, default = getDefaultTEAServerURL(args.sslEnabled), dest = 'serverURL', help = 'TEA Server URL')
commandParser.add_argument('-u', required = True, dest = 'userName', help = 'TEA User Name')
commandParser.add_argument('-p', required = True, dest = 'userPwd', help = 'TEA User Password')
commandParser.add_argument('-sc', required = False, default = '', dest = 'serverCert', help = 'Server certificate Path')
commandParser.add_argument('-cc', required = False, default = '', dest = 'clientCert', help = 'Client certificate Path')
args = commandParser.parse_known_args()[0]
# or
# args = commandParser.parse_args()

答案 1 :(得分:0)

这是因为执行commandParser.parse_known_args()[0]时无法添加更多参数。由于您正在使用在下一个解析器参数中是否启用ssl的事实,因此建议您将默认值放入getDefaultTEAServerURL(args.sslEnabled)中,因为您无法访问它。另一种选择是做这样的事情

if args.serverURL == "":
   server_url = getDefaultTEAServerURL(args.sslEnabled)

然后将server_url的默认值更改为""