命令行“用法”应该是打印在 stdout 或 stderr 上?
当打印应用程序的“用法”时,应该在 stdout 还是 stderr 上完成?
根据应用程序的不同,我见过几种情况,但似乎没有一个规则。也许我错了,有一种好的做法。既然如此,那又是什么呢?
When printing the "usage" of an application, should it be done on stdout or on stderr?
Depending on the application I've seen several cases, but there doesn't seem to be one rule. Maybe I'm mistaken and there is one good practice. In that case, what is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我会使用 STDERR,因为简单地将其放入 STDOUT 可能会导致管道输出出现问题,并且它将出现在 cronjobs 的日志中,以便您更容易注意到错误。
I'd use STDERR since simply putting it to STDOUT might cause problems with piped output and it will appear in the logs for cronjobs so you notice the mistake easier.
如果
--help
则为 stdout,否则为 stderr。以下是针对 Java 用户的 JCommander 代码:if
--help
then stdout, else stderr. Here's the JCommander code for Java users:在我看来,标准是信息的出现程度。如果它需要立即反应或注意,我会将其放入 stderr (因为它是无缓冲的)。如果它以某种方式提供信息并且不考虑任何错误,那么它是用于标准输出的。
According to me, the criteria is how emergence is the information. If it needs immediate reaction or attention, I put it into stderr (cause it's unbuffered). If it is somehow informative and do not regard any errors it is for stdout.
我认为这取决于组织的编码标准。 那些无休止争论的话题之一,比如哪个是最好的操作系统,哪个是最好的编辑器,哪个是正确的宗教,......
在组织之外,这可能是 oracle.com/technetwork/java/codeconventions-150003.pdf" rel="nofollow noreferrer">Java 代码约定(1997 年 9 月),Java 没有指定它。没有答案,人们会无休止地争论。
根据 GNU 的编码标准,它应该打印在标准输出上:
这是“版本”的相关主题。它也来自 GNU 编码指南,并且也写入标准输出:
I think it depends on the organization's coding standards. Outside an organization, its probably one of those topics that are endlessly debated, like which is the best operating system, which is the best editor, which is the right religion, ...
Browsing Java Code Conventions (SEPT 1997), Java does not specify it. There is no answer, and it will be endlessly debated.
According to GNU's coding standards, it should be printed on standard output:
Here's the related topic of "version". Its also from the GNU coding guide, and it also writes to standard output:
从来没有想过,但如果程序调用时没有参数或参数错误,为什么不将使用指令写入 stderr,而在使用
--help
(或类似)参数调用时将其写入 stdout?这样,如果由于错误而显示用法,则它将转到 stderr,如果由于用户请求而不是错误,则它将转到 stdout。不知怎的,似乎合乎逻辑。Never thought about it, but why not write the usage instructions to stderr if the program was called with no or wrong arguments, and write it to stdout when called with a
--help
(or similar) argument? This way, if the usage is shown because of an error, it goes to stderr, and if it's not an error because the user requested it, it goes to stdout. Seems logical, somehow.我同意明确请求的“用法”(通过 -h、-? 或 --help 选项)应该转到标准输出,而响应不正确的语法或其他错误而打印的“用法”应该转到标准输出到标准错误。
但是,请注意,日益流行的 popt 库(处理命令行解析;其名称代表“解析选项”)包含自动生成帮助的工具,并且它总是将其发送到 stderr。我引用 popt 手册页:
我相信这是一个 popt bug,但问题是 POSIX(或它所遵循的 ISO C)从未定义“诊断输出”的含义。只需阅读“man stderr”或POSIX.1-2008。
I agree that explicitly requested "usage" (through a -h, -? or --help option) should go to stdout while "usage" that is printed in response to incorrect syntax or other errors should go to stderr.
However, note that the increasingly popular popt library (which handles command line parsing; its name stands for "parse options") includes a facility for automatically generated help and that it always sends that to stderr. I quote the popt man page:
I believe this to be a popt bug, but the problem is that POSIX (or ISO C, to which it defers) never defined what was meant by "diagnostic output". Just read 'man stderr' or POSIX.1-2008.
这只能是意见,但我认为写入 stderr 是最好的做法。这样,即使正常输出已被重定向,如果用户犯了错误,也会显示使用消息。
This can only be opinion, but I think writing to stderr is the best thing to do. That way the usage message appears if the user makes a mistake even if the normal output has been re-directed.
我总是对有很多不适合屏幕的选项的程序感到困扰,但当作为
program --help | 运行时less
,我看不到任何内容,因为帮助实际上已发送到 stderr。我喜欢明确请求的用法(即
--help
选项)应该将输出发送到 stdout 的想法。如果选项无效,我认为没有必要显示详细的使用信息。肯定应该有一条错误消息,例如Invalid option "--some-option"。运行“program --help”以获取使用信息。
发送到stderr。如果程序决定默认在无效选项上或在没有选项的情况下调用时输出使用信息,我认为应该有一条简短的错误消息抱怨无效使用,但帮助本身可能会转到stdout。I'm always bothered by programs that have a lot of options that don't fit on screen, but when run as
program --help | less
, I can't see anything since the help was actually sent to stderr.I like the idea of explicitly requested usage (i.e.
--help
option) should send output to stdout. In case of invalid options I think it's not necessary to display detailed usage information. There definitely should be an error message likeInvalid option "--some-option". Run "program --help" for usage information.
sent to stderr. If the program decides to output usage information by default on invalid options or when invoked without options, I think there should be a short error message complaining about invalid usage, but the help itself may go to stdout.