【问题标题】:How to align horizontally a multiline CLI help text with OptionParser如何使用 OptionParser 水平对齐多行 CLI 帮助文本
【发布时间】:2016-10-03 16:23:34
【问题描述】:

我正在尝试使我的 CLI Ruby gem my_command --help 的输出更干净。

有一些 CLI 选项和标志需要几句话来解释它们。当解释太长而无法容纳在常规终端宽度视图中时,我没有找到一种方法来正确对齐其列中的文本。

我想要这样的东西:

以 ab --help 为例,注意一些标志如何具有多行解释并正确对齐。

现在,我正在 OptionParser 中执行类似的操作,以保持文本在其列中对齐,以防我们需要多行来解释某些内容:

opts.on("-d", "--directory PATH", String, "Directory to save the downloaded files into\n\t\t\t\t     Default is ./websites/ plus the domain name") do |t|
  options[:directory] = t
end

它正在工作,但在任何地方都有\t 来强制格式化似乎既不理想也不干净。另外,我可以看到在其他终端配置中格式不正确的情况。

如何使用 OptionParser 以干净的方式水平对齐多行 CLI 帮助文本?

【问题讨论】:

标签: ruby rubygems command-line-interface


【解决方案1】:

您可以通过向opts.on添加更多参数来强制换行而无需添加制表符:

opts.on("-d", "--directory PATH", String, 
        "Directory to save the downloaded files into",
        "Default is ./websites/ plus the domain name") do |t|
  options[:directory] = t
end

这在官方文档中并没有很清楚地记录,但您可以在complete example 中看到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2023-03-18
    • 2014-02-13
    • 2013-08-14
    相关资源
    最近更新 更多