3

我有一个使用 GLib 的命令行选项解析器来处理命令行参数的应用程序(如此所述)。

我发现每个选项条目的描述必须非常简短 - 为了适合标准尺寸终端的宽度(当使用 --help 参数调用应用程序时)。如果一个选项的描述太长,它会环绕,这看起来很糟糕。有没有公​​认的方法来整理这个?

例如,下面是我的应用程序的帮助输出部分在 80 个字符宽的终端窗口中的样子:

Application Options:
  -i, --ip-addr                     Sets the IP address to which the video strea
ms will be sent. If this option is not used then the default IP address of 127.0
.0.1 is used.
  -p, --port                        Sets the port to send the video streams to. 
If not chosen this defaults to 1234.

理想情况下,它看起来像这样:

Application Options:
  -i, --ip-addr                     Sets the IP address to which the video
                                    streams will be sent. If this option is not
                                    used then the default IP address of
                                    127.0.0.1 is used.
  -p, --port                        Sets the port to send the video streams to.
                                    If not chosen this defaults to 1234.

通过计算出我的选项描述的每一行所需的长度,我可以手动获得上述结果。然后我可以手动在字符串中输入换行符和空格以获得正确的缩进。但这似乎是一种非常粗略的方法,我确信必须有一种更好、更省时的方式来格式化输出。

我敢肯定这个问题之前一定已经为其他人提出过,但我还没有找到解决方案,这里有没有人知道获得更好格式的更好方法?

4

1 回答 1

0

我有完全相同的问题。目前我正在使用添加空格的 ghetto 修复。然而,这对于参数描述是不可能的(而不仅仅是描述,这是最后打印的内容)。如果您添加换行符来打破参数描述,则后续参数的间距会被弄乱。

于 2013-08-05T01:49:05.967 回答