You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/shfmt: support null byte separators in --list and --find #1096
Thanks. How often does this realistically come up?
If we add this, I would rather not add new flags. We can tweak --find and --list so that they also accept an optional zero parameter like --find=0 and --list=0. I adapted an existing boolean flag to take optional string values too in a different project, you're free to copy paste the idea:
-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 (corresponding to the -0 option of xargs)-f[=0], --find[=0] recursively find all shell files and print the paths; paths are separated by a newline or a null character if -l=0 (corresponding to the -0 option of xargs)
⇑
Your preference?
I am not sure, if that is more readable/discoverable than:
-l, --list list files whose formatting differs from shfmt's; paths are separated by a newline-l0, --list0 list files whose formatting differs from shfmt's;
paths are separated by a null character
corresponding to the -0 option of xargs
-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
-l, --list list files whose formatting differs from shfmt's; paths are separated by a newline-f, --find recursively find all shell files and print the paths; paths are separated by a newline-z \0 line termination on -l and -f output
-z
Treat the input as a set of lines, each terminated by a zero byte (the ASCII ‘NUL’ character) instead of a newline. This option can be used with commands like ‘sort -z’ and ‘find -print0’ to process arbitrary file names.
Every time one wants to use --find/--list with xargs or any other tool like sort.
You only need to null-separate arguments when they could contain newlines though. I have never in my life encountered any filenames containing newlines, so I would assume that shfmt -find . | xargs whatever works just fine with newline separators. I still don't oppose the feature, for the sake of better compatibility with other tools.
I am not sure, if that is more readable/discoverable than:
Yes, I would rather add the optional value to the two flags rather than duplicating the flags or adding a new flag which can only be combined with either of the other two. You could drop the mention of xargs in the help text for brevity.
Note that the -z flag you mention from other tools affects the entire tool. Here, our -z flag would not work on its own, so it would be a rather awkward flag.
Share the same type. FWIW a single string value is still enough as long as you set the default to "false" - then the only allowed defaults can be "false", "true", and "0". Note that -list calls Set("true") as it's a short alias for -list=true.
Activity
[-]-z or -0 flag for shfmt -f[/-][+]-z or -0 flag for shfmt -f and -l[/+][-]-z or -0 flag for shfmt -f and -l[/-][+]Add --find0 and --list0 flags[/+]mvdan commentedon Sep 26, 2024
Thanks. How often does this realistically come up?
If we add this, I would rather not add new flags. We can tweak
--findand--listso that they also accept an optional zero parameter like--find=0and--list=0. I adapted an existing boolean flag to take optional string values too in a different project, you're free to copy paste the idea:https://github.com/mvdan/xurls/blob/c5b6e994e5db07c3f691bbb8c044b34dd20720f8/cmd/xurls/main.go#L36-L44
sdavids commentedon Sep 26, 2024
Every time one wants to use
--find/--listwithxargsor any other tool likesort.sdavids commentedon Sep 26, 2024
⇑
Your preference?
I am not sure, if that is more readable/discoverable than:
Precedent
https://www.man7.org/linux/man-pages/man1/find.1.html
https://git-scm.com/docs/git-ls-files#Documentation/git-ls-files.txt--z
https://www.gnu.org/software/sed/manual/sed.html
mvdan commentedon Sep 29, 2024
You only need to null-separate arguments when they could contain newlines though. I have never in my life encountered any filenames containing newlines, so I would assume that
shfmt -find . | xargs whateverworks just fine with newline separators. I still don't oppose the feature, for the sake of better compatibility with other tools.Yes, I would rather add the optional value to the two flags rather than duplicating the flags or adding a new flag which can only be combined with either of the other two. You could drop the mention of xargs in the help text for brevity.
Note that the
-zflag you mention from other tools affects the entire tool. Here, our-zflag would not work on its own, so it would be a rather awkward flag.[-]Add --find0 and --list0 flags[/-][+]cmd/shfmt: support null byte separators in --list and --find[/+]sdavids commentedon Sep 29, 2024
Maybe you wanted to say “spaces”?
That is because the file systems I know of do not allow it.
File names with spaces are quite common on the other hand.
macOS:
~/Library/Application Supportmvdan commentedon Sep 29, 2024
Ah of course. For some reason I misremembered
xargsto only split on newlines, but it does split on spaces too.sdavids commentedon Oct 1, 2024
As far as I can tell the flag package does not differentiate between
--flag=xand--flag x.If you have a String value then you must have a value, i.e.
--flagis not allowed anymore.This is important because if
lis String instead of Bool:shfmt -l .is the same asshfmt -l=.With the above we would have
list.val.val = "."forshfmt -l .mvdan commentedon Oct 1, 2024
See the snippet I shared earlier:
https://github.com/mvdan/xurls/blob/c5b6e994e5db07c3f691bbb8c044b34dd20720f8/cmd/xurls/main.go#L36-L44
In particular the presence of
IsBoolFlag.sdavids commentedon Oct 1, 2024
Adding
does not change a thing.
shfmt -l .⇒list.val.val = "."mvdan commentedon Oct 1, 2024
Because you're using flag.StringVar rather than flag.Var; that takes Value, which is where IsBoolFlag may be present.
If you get stuck it's not a problem, I'll get to it at some point.
sdavids commentedon Oct 1, 2024
It have it working now, but one side effect is:
./shfmt2 -l=true .is the same as./shfmt2 -l ..Should that be documented?
mvdan commentedon Oct 1, 2024
That's already the case with all boolean flags in Go:
It's just not a very well known fact because it's not very useful in general.
sdavids commentedon Oct 1, 2024
Do you want just one
boolFlagor onelistFlagand onefindFlag.Considering now both have the same allowed values, but that might change in the future.
vs.
mvdan commentedon Oct 1, 2024
Share the same type. FWIW a single string value is still enough as long as you set the default to
"false"- then the only allowed defaults can be"false","true", and"0". Note that-listcallsSet("true")as it's a short alias for-list=true.sdavids commentedon Oct 1, 2024
I have updated the PR.
cmd/shfmt: support -l=0 and -f=0 flags
sdavids commentedon Mar 18, 2025
Verified:
chore(deps): update module mvdan.cc/sh/v3/cmd/shfmt to v3.12.0 (hetzn…
interp+lower: reconcile typed shell scalar diagnostics
interp: decode exact float carrier before shell scalar fallback