【问题标题】:Linux terminal: nicely formatted -h outputLinux 终端:格式良好的 -h 输出
【发布时间】:2016-01-26 20:57:51
【问题描述】:

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

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

但以我的基本字符串和打印知识,我似乎无法获得正确的格式。如何实现多行描述是齐平的并且没有进入参数部分?

【问题讨论】:

    标签: python linux command-line terminal


    【解决方案1】:

    首先,请记住“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')
    

    【讨论】:

      【解决方案2】:

      我建议跳过自己和use the argparse Python module进行解析

      【讨论】:

      • 完美,我使用的是 getopt 模块,但 argparse 完全符合我的要求。
      • 我建议跳过 argparse 直接前往docopt
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2011-02-14
      • 2010-11-08
      • 1970-01-01
      相关资源
      最近更新 更多