Skip to content

Commit

Permalink
cmd/shfmt: document when --filename is needed for EditorConfig support
Browse files Browse the repository at this point in the history
EditorConfig files are found based on a script's absolute path,
and the EditorConfig patterns are usually filename-based as well.

When formatting standard input, there is no known filename or path.
Sometimes there's a need to format a stream of bytes in memory
without having to place them on a file, which is why we support stdin.

We added the --filename flag for this purpose a long time ago,
and the tests already verified this fact - we just hadn't documented it.

While here, also add a test case for absolute paths,
and make the man page flags consistently use double dashes.

Fixes #1055.
  • Loading branch information
mvdan committed Feb 13, 2024
1 parent 84baa08 commit df8fd14
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/shfmt/shfmt.1.scd
Expand Up @@ -25,7 +25,7 @@ predictable. Some aspects of the format can be configured via printer flags.

## Generic flags

*-version*
*--version*
Show version and exit.

*-l*, *--list*
Expand Down Expand Up @@ -70,9 +70,12 @@ predictable. Some aspects of the format can be configured via printer flags.
*-p*, *--posix*
Shorthand for *-ln=posix*.

*-filename* str
*--filename* str
Provide a name for the standard input file.

Use of this flag is necessary for EditorConfig support to work with stdin,
since EditorConfig files are found relative to the location of a script.

## Printer flags

*-i*, *--indent* <uint>
Expand Down
4 changes: 4 additions & 0 deletions cmd/shfmt/testdata/script/editorconfig.txtar
Expand Up @@ -14,6 +14,10 @@ stdin stdin-filename-bash
! exec shfmt -filename=foo_posix.sh
stderr '^foo_posix.sh:.* arrays are a bash'

stdin stdin-filename-bash
! exec shfmt -filename=${WORK}/foo_posix.sh
stderr ^${WORK@R}/'foo_posix.sh:.* arrays are a bash'

# Using a file path should use EditorConfig, including with the use of flags
# like -l.
exec shfmt input.sh
Expand Down

0 comments on commit df8fd14

Please sign in to comment.