如果没有,是否存在一个事实上的标准?基本上,我正在编写命令行帮助文本,如下所示:

usage: app_name [options] required_input required_input2
  options:
    -a, --argument     Does something
    -b required     Does something with "required"
    -c, --command required     Something else
    -d [optlistitem1 optlistitem 2 ... ]     Something with list

我基本上只是阅读了各种工具的帮助文本,但是否有指南列表或其他内容?例如,我是用方括号还是圆括号?如何使用空格?如果参数是一个列表呢?谢谢!


当前回答

没有标准,但是http://docopt.org/已经创建了他们版本的命令行工具的帮助文本规范。

其他回答

我认为命令行使用没有标准的语法,但大多数人都使用这样的约定:

微软命令行语法,IBM有类似的命令行语法


没有括号或大括号的文本 必须按所示输入的项 <尖括号内的文本> 必须为其提供值的占位符 [方括号内的文本] 可选项目 {括号内的文本} 一套必需的项目;选择一个 竖条{a|b} 互斥项的分隔符;选择一个 <file>… 可以重复的项目

我们运行的是Linux,一个基本符合posix的操作系统。它应该是POSIX标准:实用程序参数语法。

An option is a hyphen followed by a single alphanumeric character, like this: -o. An option may require an argument (which must appear immediately after the option); for example, -o argument or -oargument. Options that do not require arguments can be grouped after a hyphen, so, for example, -lst is equivalent to -t -l -s. Options can appear in any order; thus -lst is equivalent to -tls. Options can appear multiple times. Options precede other nonoption arguments: -lst nonoption. The -- argument terminates options. The - option is typically used to represent one of the standard input streams.

我会以官方项目为例,比如tar。在我看来,帮助味精。需要尽可能的简单和描述性。使用的例子也很好。对“标准帮助”没有真正的需求。

这可能有点离题,但我曾经写过两个小工具,使创建和维护命令行工具帮助页面更有效:

MAIN DOCLET,它通过处理源代码中的Javadoc注释,为Java程序的主方法生成HTML文档 HTML2TXT工具,将HTML文档格式化为纯文本(这是我们想要的帮助文本)

我将这两个工具集成到程序的MAVEN构建过程中,以便它们在每次构建时自动执行。

例如:

我的ZZFIND工具的相关源文件 构建项目的POM文件(并运行上面提到的两个工具) 使用——help命令行选项运行ZZFIND时的示例输出

希望这对其他人有用!?

看一下docopt。它是用于记录(和自动解析)命令行参数的正式标准。

例如……

Usage:
  my_program command --option <argument>
  my_program [<optional-argument>]
  my_program --another-option=<with-argument>
  my_program (--either-that-option | <or-this-argument>)
  my_program <repeating-argument> <repeating-argument>...