cmd-help-sublime-syntax icon indicating copy to clipboard operation
cmd-help-sublime-syntax copied to clipboard

Wrong order of `stdout` and `stderr` in `file --help`

Open rusmux opened this issue 5 months ago • 2 comments

description

file \--help 2>&1 | bat -pl help gives:

  -M LIST                    use LIST as a colon-separated list of magic
                               number files in place of default
  -d                         use default magic file
  -i                         do not further classify regular files
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.

      --help                 display this help and exit
  -v, --version              output version information and exit
  -m, --magic-file LIST      use LIST as a colon-separated list of magic
                               number files
...

Because file writes some part of its help message to stderr and it goes first in the resulting text. Without 2>&1 everything is right:

file \--help | bat -pl help:

Usage: file [OPTION...] [FILE...]
Determine type of FILEs.

      --help                 display this help and exit
  -v, --version              output version information and exit
  -m, --magic-file LIST      use LIST as a colon-separated list of magic
                               number files
...

I think it's quite rare for most tools to write help to stderr, but I guess it's worth mentioning.

$ bat --version

bat 0.25.0

$ COMMAND --help

file \--help 2>&1 | bat -pl help

$ COMMAND --version

file-5.41; magic file from /usr/share/file/magic

rusmux avatar Jul 02 '25 13:07 rusmux

Hi @rusmux, thank you for reporting this!

Does it change anything if you do file \--help 1>&2 | bat -pl help ?

(I wouldn't expect it to, because I've read the redirect handles stdout & stderr concurrently, not as a concatenation. But still.)

If it's a concurrency issue, then the only way to fix this lies on the command's end. I don't think we're able to improve the situation here.

As a matter of fact, I can't reproduce this with a latter version of the command, file-5.45[^1]. That one prints a large error message and that's the end of the story.

For context, we recommend using that redirect because, while writing tests for this, I found that some commands write their help message to stderr, not stdout. So it makes sense to capture both before piping to bat + cmd-help.

For this case only, you may try skipping the redirection (you'll lose half the msg though, and I don't know which half 😅). Or forego bat + cmd-help entirely.

Is this "resolution" satisfactory enough for you?

[^1]: magic file from /etc/magic:/usr/share/misc/magic, on Ubuntu 24.04.

victor-gp avatar Aug 05 '25 11:08 victor-gp

FWIW, I can't reproduce it on a Docker container of Ubuntu 22.04 either... (file-5.41 like yours)

It prints the entire message, in the intended order, and without any ellipses.

Are you maybe on a BSD system?

In any case, is file the only command that gives this issue or are there others?

If it's the latter, I may want to look into improving our handling of this case (zero idea how though!)

victor-gp avatar Aug 05 '25 11:08 victor-gp