Skip to content

Commit

Permalink
cmd/shfmt: support -l[=0] flag
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Davids <[email protected]>
  • Loading branch information
sdavids committed Oct 1, 2024
1 parent 9d2806b commit 4183cbc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
34 changes: 25 additions & 9 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type multiFlag[T any] struct {

var (
versionFlag = &multiFlag[bool]{"", "version", false}
list = &multiFlag[bool]{"l", "list", false}
list = &multiFlag[boolString]{"l", "list", "false"}

write = &multiFlag[bool]{"w", "write", false}
simplify = &multiFlag[bool]{"s", "simplify", false}
Expand Down Expand Up @@ -99,6 +99,13 @@ func init() {
if name := f.long; name != "" {
flag.BoolVar(&f.val, name, f.val, "")
}
case *multiFlag[boolString]:
if name := f.short; name != "" {
flag.Var(&f.val, name, "")
}
if name := f.long; name != "" {
flag.Var(&f.val, name, "")
}
case *multiFlag[string]:
if name := f.short; name != "" {
flag.StringVar(&f.val, name, f.val, "")
Expand Down Expand Up @@ -140,12 +147,13 @@ directory, all shell scripts found under that directory will be used.
--version show version and exit
-l, --list list files whose formatting differs from shfmt's
-w, --write write result to file instead of stdout
-d, --diff error with a diff when the formatting differs
-s, --simplify simplify the code
-mn, --minify minify the code to reduce its size (implies -s)
--apply-ignore always apply EditorConfig ignore rules
-l[=0], --list[=0] list files whose formatting differs from shfmt;
paths are separated by a newline or a null character if -l=0
-w, --write write result to file instead of stdout
-d, --diff error with a diff when the formatting differs
-s, --simplify simplify the code
-mn, --minify minify the code to reduce its size (implies -s)
--apply-ignore always apply EditorConfig ignore rules
Parser options:
Expand Down Expand Up @@ -189,6 +197,10 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh.
fmt.Fprintf(os.Stderr, "-p and -ln=lang cannot coexist\n")
return 1
}
if list.val != "true" && list.val != "false" && list.val != "0" {
fmt.Fprintf(os.Stderr, "only -l and -l=0 allowed\n")
return 1
}
if minify.val {
simplify.val = true
}
Expand Down Expand Up @@ -494,8 +506,12 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
printer.Print(&writeBuf, node)
res := writeBuf.Bytes()
if !bytes.Equal(src, res) {
if list.val {
switch list.val {
case "true":
fmt.Println(path)
case "0":
fmt.Print(path)
fmt.Print("\000")
}
if write.val {
info, err := os.Lstat(path)
Expand Down Expand Up @@ -539,7 +555,7 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
return errChangedWithDiff
}
}
if !list.val && !write.val && !diff.val {
if list.val == "false" && !write.val && !diff.val {
os.Stdout.Write(res)
}
return nil
Expand Down
5 changes: 3 additions & 2 deletions cmd/shfmt/shfmt.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ predictable. Some aspects of the format can be configured via printer flags.
*--version*
Show version and exit.

*-l*, *--list*
List files whose formatting differs from shfmt's.
*-l[=0]*, *--list[=0]*
List files whose formatting differs from shfmt's;
paths are separated by a newline or a null character if -l=0

*-w*, *--write*
Write result to file instead of stdout.
Expand Down

0 comments on commit 4183cbc

Please sign in to comment.