Linux 命令行开关和参数有标准吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8957222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are there standards for Linux command line switches and arguments?
提问by DigitalMan
This is more about the invocation of a program, than any language or parser (though I'm sure choice of parser library can depend on this). See, I've used a lot of Linux command-line utilities. And there are some obvious patterns; '-' precedes a single letter for short options, multiple options that don't take arguments can be combined, '--' precedes long versions of options, and so on.
这更多是关于程序的调用,而不是任何语言或解析器(尽管我确信解析器库的选择取决于这一点)。看,我使用了很多 Linux 命令行实用程序。并且有一些明显的模式;对于短选项,'-' 位于单个字母之前,可以组合多个不带参数的选项,'--' 在选项的长版本之前,依此类推。
However, in some cases, capitalization is used to invertan option. So, '-d' might mean to run as a daemon, but '-D' would be to notrun as a daemon. (Why not just omit the option if you don't want it? That's never been clear, but it's actually rather common, so I figure there must be some reason.) But in some programs, a capital is a completely unrelated option; if '-d' is run as daemon, '-D' might be to enable debug mode. Is there some kind of overarching principal behind this, and which is the best to choose? Or are we just dealing with "whatever works"?
但是,在某些情况下,大写用于反转选项。因此,'-d' 可能意味着作为守护进程运行,但 '-D' 将不作为守护进程运行。(如果你不想要它,为什么不直接省略这个选项?这一直不清楚,但它实际上相当普遍,所以我认为一定有某种原因。)但在某些程序中,大写是一个完全不相关的选项;如果“-d”作为守护进程运行,“-D”可能是启用调试模式。这背后是否有某种总体原则,哪个是最好的选择?或者我们只是在处理“任何有效的东西”?
There are also some commands that, in addition to (or instead of) options with arguments, just take lone arguments. cp is a good example of this; aside from a couple rarely used toggles, the last argument it receives is presumed to be the destination, and any arguments between the option list and the destination are presumed to be sources. Is there a rule of thumb when it's "okay" to rely on order like that, instead of using explicit option flags with arguments?
还有一些命令,除了(或代替)带参数的选项之外,只接受单独的参数。cp 就是一个很好的例子;除了几个很少使用的切换之外,它接收到的最后一个参数被假定为目标,并且选项列表和目标之间的任何参数都被假定为源。当“可以”依赖这样的顺序而不是使用带有参数的显式选项标志时,是否有经验法则?
采纳答案by ApriOri
回答by DigitalMan
ESR has collected a lot of information about this in his book "The Art of UNIX Programming". Here's a snippet.
ESR 在他的《UNIX 编程艺术》一书中收集了很多关于这方面的信息。这是一个片段。
-a
All (without argument). If there is a GNU-style --all option, for -a to be anything but a synonym for it would be quite surprising. Examples: fuser(1), fetchmail(1).Append, as in tar(1). This is often paired with -d for delete.
-b
Buffer or block size (with argument). Set a critical buffer size, or (in a program having to do with archiving or managing storage media) set a block size. Examples: du(1), df(1), tar(1).Batch. If the program is naturally interactive, -b may be used to suppress prompts or set other options appropriate to accepting input from a file rather than a human operator. Example: flex(1).
-c
Command (with argument). If the program is an interpreter that normally takes commands from standard input, it is expected that the option of a -c argument will be passed to it as a single line of input. This convention is particularly strong for shells and shell-like interpreters. Examples: sh(1), ash(1), bsh(1), ksh(1), python(1). Compare -e below.Check (without argument). Check the correctness of the file argument(s) to the command, but don't actually perform normal processing. Frequently used as a syntax-check option by programs that do interpretation of command files. Examples: getty(1), perl(1).
-a
全部(不带参数)。如果有一个 GNU 风格的 --all 选项,那么 -a 不是它的同义词,这将是非常令人惊讶的。示例:fuser(1)、fetchmail(1)。附加,如 tar(1)。这通常与 -d 配对用于删除。
-b
缓冲区或块大小(带参数)。设置临界缓冲区大小,或(在与归档或管理存储介质有关的程序中)设置块大小。示例:du(1)、df(1)、tar(1)。批。如果程序是自然交互的,可以使用 -b 来抑制提示或设置其他适合接受来自文件而不是人工操作员的输入的选项。示例:flex(1)。
-c
命令(带参数)。如果程序是一个通常从标准输入中获取命令的解释器,则期望 -c 参数的选项将作为单行输入传递给它。这种约定对于 shell 和类似 shell 的解释器特别有效。示例:sh(1)、ash(1)、bsh(1)、ksh(1)、python(1)。比较下面的 -e。检查(不带参数)。检查命令的文件参数的正确性,但实际上不执行正常处理。经常被解释命令文件的程序用作语法检查选项。示例:getty(1)、perl(1)。
See the full list at http://catb.org/~esr/writings/taoup/html/ch10s05.html
在http://catb.org/~esr/writings/taoup/html/ch10s05.html查看完整列表
回答by showMeYourPython
The Linux/GNU command line interface follows the POSIX standard. This is noted by GNU in their standards: http://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html.
Linux/GNU 命令行界面遵循 POSIX 标准。GNU 在他们的标准中指出了这一点:http: //www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html。
Command line syntax is also part of the Single Unix Specification, though --long-options are a GNU innovation IIRC.
命令行语法也是单一 Unix 规范的一部分,尽管 --long-options 是 GNU 的创新 IIRC。
See here: http://pubs.opengroup.org/onlinepubs/7908799/xbd/utilconv.html
请参阅此处:http: //pubs.opengroup.org/onlinepubs/7908799/xbd/utilconv.html
But yes, this standard is implemented as getopt.
但是是的,这个标准是作为 getopt 实现的。
回答by JCB
A quick summary of the thread:
线程的快速摘要:
You CLI should display help when missing or incorrect parameters in addition to the error message if any.
You should use
-
for a single letter flag or option and--
for a long option, for instance-a
and--all
All programs should support two standard options:
-v
--version
and-h
--help
.-h
and--help
=> Give usage message and exit-v
and--version
=> Show program version and exit
除了错误消息(如果有)之外,当缺少或不正确的参数时,您的 CLI 应该显示帮助。
您应该使用
-
单字母标志或选项以及--
长选项,例如-a
和--all
所有程序都应支持两个标准选项:
-v
--version
和-h
--help
.-h
and--help
=> 给出使用信息并退出-v
和--version
=> 显示程序版本并退出
See the links (IEEE and GNU getopt) provided on this answer https://stackoverflow.com/a/8957246
请参阅此答案中提供的链接(IEEE 和 GNU getopt)https://stackoverflow.com/a/8957246