-
-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/shfmt: support null byte separators in --list and --find #1096
Comments
Thanks. How often does this realistically come up? If we add this, I would rather not add new flags. We can tweak |
Every time one wants to use |
-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 Precedenthttps://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
|
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
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 |
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: |
Ah of course. For some reason I misremembered |
As far as I can tell the flag package does not differentiate between
If you have a String value then you must have a value, i.e. This is important because if
type stringFlag struct {
set bool
val string
}
func (sf *stringFlag) Set(s string) error {
sf.val = s
// TODO validate s
sf.set = true
return nil
}
func (sf *stringFlag) String() string {
return sf.val
}
list = &multiFlag[stringFlag]{"l", "list", stringFlag{false, ""}}
case *multiFlag[stringFlag]:
if name := f.short; name != "" {
flag.StringVar(&f.val.val, name, f.val.val, "")
}
if name := f.long; name != "" {
flag.StringVar(&f.val.val, name, f.val.val, "")
} With the above we would have |
See the snippet I shared earlier: In particular the presence of |
Adding func (*stringFlag) IsBoolFlag() bool { return true } does not change a thing.
|
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. |
It have it working now, but one side effect is:
Should that be documented? |
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. |
Do you want just one Considering now both have the same allowed values, but that might change in the future. func (s * boolFlag) Set(val string) error {
if val != "true" && val != "0" {
return fmt.Errorf("only \"0\" supported\n")
}
s.val = val
s.set = true
return nil
}
list = &multiFlag[boolFlag]{"l", "list", boolFlag{false, ""}}
find = &multiFlag[boolFlag]{"f", "find", boolFlag{false, ""}} vs. list = &multiFlag[listFlag]{"l", "list", listFlag{false, ""}}
find = &multiFlag[findFlag]{"f", "find", findFlag{false, ""}} |
Share the same type. FWIW a single string value is still enough as long as you set the default to |
I have updated the PR. |
To separate output filenames with null bytes rather than newlines, matching the format expected by tools such as `xargs -0`. Fixes mvdan#1096. Signed-off-by: Sebastian Davids <sdavids@gmx.de>
@msanders
#288 (comment)
How can I find and safely handle file names containing newlines, spaces or both?
Also for
shfmt -l
.Adding
--find0
and--list0
would not lead to ambiguous/erroneous cases likeshfmt -w -0 .
.It also mirrors
find
's-print
/-print0
and-fprint
/fprint0
.The text was updated successfully, but these errors were encountered: