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

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

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


GNU编码标准是一个很好的参考。本节处理——help的输出。在这种情况下,它不是很具体。打印一个表,显示短选项和长选项,并给出简洁的描述,可能不会出错。为了可读性,尽量让所有参数之间的间距合适。您可能希望为您的工具提供一个手册页(也可能是信息手册),以提供更详细的解释。


是的,你的思路是对的。

是的,方括号是可选项目的常用指示符。

通常,正如您所勾勒的那样,在顶部有一个命令行摘要,后面是详细信息,理想情况下每个选项都有示例。(您的示例显示了每个选项描述之间的行,但我假设这是一个编辑问题,并且您的实际程序输出的是缩进的选项列表,中间没有空行。这将是在任何情况下都要遵循的标准。)

一个新的趋势(也许有一个POSIX规范可以解决这个问题?)是消除文档的手册页系统,并将手册页中的所有信息作为程序的一部分—帮助输出包括在内。这个额外的内容将包括更长的描述,解释的概念,使用示例,已知的限制和错误,如何报告错误,以及可能的相关命令的“参见”部分。

我希望这能有所帮助。


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


通常,你的帮助输出应该包括:

Description of what the app does Usage syntax, which: Uses [options] to indicate where the options go arg_name for a required, singular arg [arg_name] for an optional, singular arg arg_name... for a required arg of which there can be many (this is rare) [arg_name...] for an arg for which any number can be supplied note that arg_name should be a descriptive, short name, in lower, snake case A nicely-formatted list of options, each: having a short description showing the default value, if there is one showing the possible values, if that applies Note that if an option can accept a short form (e.g. -l) or a long form (e.g. --list), include them together on the same line, as their descriptions will be the same Brief indicator of the location of config files or environment variables that might be the source of command line arguments, e.g. GREP_OPTS If there is a man page, indicate as such, otherwise, a brief indicator of where more detailed help can be found

进一步注意,同时接受-h和——help来触发这条消息是一种很好的形式,如果用户弄乱了命令行语法,例如遗漏了一个必需的参数,你应该显示这条消息。


微软有自己的命令行标准规范:

This document is focused at developers of command line utilities. Collectively, our goal is to present a consistent, composable command line user experience. Achieving that allows a user to learn a core set of concepts (syntax, naming, behaviors, etc) and then be able to translate that knowledge into working with a large set of commands. Those commands should be able to output standardized streams of data in a standardized format to allow easy composition without the burden of parsing streams of output text. This document is written to be independent of any specific implementation of a shell, set of utilities or command creation technologies; however, Appendix J - Using Windows Powershell to implement the Microsoft Command Line Standard shows how using Windows PowerShell will provide implementation of many of these guidelines for free.


看一下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>...

我们运行的是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.


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

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


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


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

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

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

例如:

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

希望这对其他人有用!?


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


我使用CSS形式符号表示。

Component values may be arranged into property values as follows: Several juxtaposed words mean that all of them must occur, in the given order. A bar (|) separates two or more alternatives: exactly one of them must occur. A double bar (||) separates two or more options: one or more of them must occur, in any order. A double ampersand (&&) separates two or more components, all of which must occur, in any order. Brackets ([ ]) are for grouping. Juxtaposition is stronger than the double ampersand, the double ampersand is stronger than the double bar, and the double bar is stronger than the bar. Thus, the following lines are equivalent: a b | c || d && e f [ a b ] | [ c || [ d && [ e f ]]] Every type, keyword, or bracketed group may be followed by one of the following modifiers: An asterisk (*) indicates that the preceding type, word, or group occurs zero or more times. A plus (+) indicates that the preceding type, word, or group occurs one or more times. A question mark (?) indicates that the preceding type, word, or group is optional. A pair of numbers in curly braces ({A,B}) indicates that the preceding type, word, or group occurs at least A and at most B times.

如果您需要示例,请参阅MDN上的正式定义部分;这里有一个字体:https://developer.mozilla.org/en-US/docs/Web/CSS/font#formal_syntax。

下面是我自己Pandoc小抄中的一个简单例子:

$ pandoc <input_file>.md --from [markdown|commonmark_x][-smart]? --to html --standalone --table-of-contents? --number-sections? [--css <style_sheet>.css]? --output <output_file>.html