diff --git a/cmd/shfmt/main.go b/cmd/shfmt/main.go index e1e7a139..b377b933 100644 --- a/cmd/shfmt/main.go +++ b/cmd/shfmt/main.go @@ -41,6 +41,7 @@ var ( simplify = &multiFlag[bool]{"s", "simplify", false} minify = &multiFlag[bool]{"mn", "minify", false} find = &multiFlag[bool]{"f", "find", false} + find0 = &multiFlag[bool]{"f0", "find0", false} diff = &multiFlag[bool]{"d", "diff", false} applyIgnore = &multiFlag[bool]{"", "apply-ignore", false} @@ -71,7 +72,7 @@ var ( version = "(devel)" // to match the default from runtime/debug allFlags = []any{ - versionFlag, list, list0, write, simplify, minify, find, diff, applyIgnore, + versionFlag, list, list0, write, simplify, minify, find, find0, diff, applyIgnore, lang, posix, filename, indent, binNext, caseIndent, spaceRedirs, keepPadding, funcNext, toJSON, fromJSON, } @@ -159,7 +160,11 @@ Printer options: Utilities: - -f, --find recursively find all shell files and print the paths + -f, --find recursively find all shell files and print the paths; + paths are separated by a newline + -f0, --find0 recursively find all shell files and print the paths; + paths are separated by a null character + corresponding to the -0 option of xargs --to-json print syntax tree to stdout as a typed JSON --from-json read syntax tree from stdin as a typed JSON @@ -188,6 +193,10 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh. fmt.Fprintf(os.Stderr, "-l and -l0 cannot coexist\n") return 1 } + if find.val && find0.val { + fmt.Fprintf(os.Stderr, "-f and -f0 cannot coexist\n") + return 1 + } if minify.val { simplify.val = true } @@ -255,7 +264,7 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh. } status := 0 for _, path := range flag.Args() { - if info, err := os.Stat(path); err == nil && !info.IsDir() && !applyIgnore.val && !find.val { + if info, err := os.Stat(path); err == nil && !info.IsDir() && !applyIgnore.val && !find.val && !find0.val { // When given paths to files directly, always format them, // no matter their extension or shebang. // @@ -432,6 +441,10 @@ func formatPath(path string, checkShebang bool) error { if find.val { fmt.Println(path) return nil + } else if find0.val { + fmt.Print(path) + fmt.Print("\000") + return nil } if _, err := io.CopyBuffer(&readBuf, f, copyBuf); err != nil { return err diff --git a/cmd/shfmt/shfmt.1.scd b/cmd/shfmt/shfmt.1.scd index 4a395f92..8c090e91 100644 --- a/cmd/shfmt/shfmt.1.scd +++ b/cmd/shfmt/shfmt.1.scd @@ -105,7 +105,13 @@ predictable. Some aspects of the format can be configured via printer flags. ## Utility flags *-f*, *--find* - Recursively find all shell files and print the paths. + Recursively find all shell files and print the paths; + paths are separated by a newline. + +*-f0*, *--find0* + Recursively find all shell files and print the paths; + paths are separated by a null character + corresponding to the *-0* option of *xargs*. *--to-json* Print syntax tree to stdout as a typed JSON. diff --git a/cmd/shfmt/testdata/script/flags.txtar b/cmd/shfmt/testdata/script/flags.txtar index b52aa653..dba69fa0 100644 --- a/cmd/shfmt/testdata/script/flags.txtar +++ b/cmd/shfmt/testdata/script/flags.txtar @@ -19,6 +19,9 @@ stdout 'devel|v3' ! exec shfmt -l -l0 stderr 'cannot coexist' +! exec shfmt -f -f0 +stderr 'cannot coexist' + ! exec shfmt -ln=bash -p stderr 'cannot coexist'