Linux终端:格式良好的-h输出

时间:2015-10-27 12:34:05

标签: python linux command-line terminal

所以我用命令行参数支持写了我的第一个python程序。问题是我希望我的-h帮助消息显示在(Linux)终端上的格式为:

-i    Description for input
-o    Description for output
-x    Longer description that needs
      more lines than one

但是凭借我的基本字符串和印刷知识,我似乎无法使格式正确。如何实现多行描述是齐声并且不会进入参数部分?

2 个答案:

答案 0 :(得分:2)

首先,请记住" batteries are included。"具体来说,我建议您使用提供的argparse模块。

其次,您的特定问题的答案是使用width specifier of the format spec,如下所示:

fmt_string = '{:7s}{:s}'
print fmt_string.format('-i', 'Description for input')
print fmt_string.format('-o', 'Description for output')
print fmt_string.format('-x', 'Longer description that needs')
print fmt_string.format('', 'more lines than one')

答案 1 :(得分:1)

我建议您自己跳过解析并use the argparse Python module

相关问题