Python argparse - 长参数名称的帮助文本
Python argparse - help text for long argument name
Python 'argparse' 为长命名参数在新行显示帮助文本:
**#script.py -h
Select one of the options given
optional arguments:
-h, --help show this help message and exit
-bs , --business_service
choose the service << New line
-so , --service_offering
-ci , --cmdb_ci provide configuration item
-desc , --description
write description << New line
下面是我使用的代码:
self.parser = argparse.ArgumentParser(
description='Select one of the options given',
epilog=self._get_usage(),
formatter_class=argparse.RawTextHelpFormatter
)
self.parser.add_argument('-bs','--business_service',type=str,help='choose the service',metavar='')
self.parser.add_argument('-so','--service_offering',type=str,metavar='')
self.parser.add_argument('-ci','--mdcb_ci',type=str,help='provide configuration item',metavar='')
self.parser.add_argument('-desc','--description',type=str,help='write description',metavar='')
我希望帮助字符串与参数位于同一行:
-bs , --business_service choose the service << Same line
我该如何解决?
HelpFormatter
class 接受一个 max_help_position
参数。默认值为 24,这就是您在帮助中看到的值。
可以使用新的 Formatter subclass 或修改调用参数来更改它。
In [23]: parser.formatter_class =
lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=30)
我在此之后修改 formatter_class
属性,但您可以使用此 lambda
行代替
formatter_class=argparse.RawTextHelpFormatter
结果是:
In [24]: parser.print_help()
usage: ipython3 [-h] [-bs] [-so] [-ci] [-desc]
Select one of the options given
optional arguments:
-h, --help show this help message and exit
-bs , --business_service choose the service
-so , --service_offering
-ci , --mdcb_ci provide configuration item
-desc , --description write description
选择这样的参数是为了平衡显示器的整体宽度和易读性。可以像这样调整它们,但这确实需要一些 Python 编程知识。我已经有一段时间没有这样做了,所以我的建议可能不是最简单的。但大体上是正确的方向。
另一种使用 partial
包装器设置它的方法:
In [42]: from functools import partial
In [43]: newformatter=partial(argparse.HelpFormatter, max_help_position=30)
In [44]: parser.formatter_class=newformatter
Python 'argparse' 为长命名参数在新行显示帮助文本:
**#script.py -h
Select one of the options given
optional arguments:
-h, --help show this help message and exit
-bs , --business_service
choose the service << New line
-so , --service_offering
-ci , --cmdb_ci provide configuration item
-desc , --description
write description << New line
下面是我使用的代码:
self.parser = argparse.ArgumentParser(
description='Select one of the options given',
epilog=self._get_usage(),
formatter_class=argparse.RawTextHelpFormatter
)
self.parser.add_argument('-bs','--business_service',type=str,help='choose the service',metavar='')
self.parser.add_argument('-so','--service_offering',type=str,metavar='')
self.parser.add_argument('-ci','--mdcb_ci',type=str,help='provide configuration item',metavar='')
self.parser.add_argument('-desc','--description',type=str,help='write description',metavar='')
我希望帮助字符串与参数位于同一行:
-bs , --business_service choose the service << Same line
我该如何解决?
HelpFormatter
class 接受一个 max_help_position
参数。默认值为 24,这就是您在帮助中看到的值。
可以使用新的 Formatter subclass 或修改调用参数来更改它。
In [23]: parser.formatter_class =
lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=30)
我在此之后修改 formatter_class
属性,但您可以使用此 lambda
行代替
formatter_class=argparse.RawTextHelpFormatter
结果是:
In [24]: parser.print_help()
usage: ipython3 [-h] [-bs] [-so] [-ci] [-desc]
Select one of the options given
optional arguments:
-h, --help show this help message and exit
-bs , --business_service choose the service
-so , --service_offering
-ci , --mdcb_ci provide configuration item
-desc , --description write description
选择这样的参数是为了平衡显示器的整体宽度和易读性。可以像这样调整它们,但这确实需要一些 Python 编程知识。我已经有一段时间没有这样做了,所以我的建议可能不是最简单的。但大体上是正确的方向。
另一种使用 partial
包装器设置它的方法:
In [42]: from functools import partial
In [43]: newformatter=partial(argparse.HelpFormatter, max_help_position=30)
In [44]: parser.formatter_class=newformatter