Expand globs in args#716
Conversation
|
For this... I think we need to guard it a bit better to avoid regression. if !strings.ContainsAny(arg, "*?[") {
filepaths = append(filepaths, arg)
continue
}
matches, err := filepath.Glob(arg)
if err != nil || len(matches) == 0 {the above guards, |
|
🤔 I think I'm realizing I didn't think this all the way through. Your code block (specifically the I think this will need to be enabled only if a flag like BTW I think we can drop the nil check on the error if we check the length of the matches, since matches should be nil on an error. But keeping the |
|
Yeah... globbing support is one of those things I tried to kick out to the OS due to all the edge cases it presents. |
|
I think that turning this on with If there's a flag, I don't think a "does this look like a glob" check would be necessary, since the user has explicitly opted into treating args as globs. Of course, only if you consider glob support worth the additional complexity. |
|
I think its fine in this case, its mostly for windows support? So long as thats mentioned in the -h I think its fine. |
That's my use case, though I imagine it could potentially be helpful in certain scripting scenarios, if scc gets called by another process and receives some filepath-like input. Unfortunately, since command prompt doesn't expand globs, and it's a bit cumbersome to do in PowerShell, expanding globs in the executable is necessary to make usage between Windows built-in shells and Unix the same. |
This is somewhat of a follow-up to #540.
Unfortunately, Windows command prompt and PowerShell do not expand globs by default. It is instead left up to the application to expand the globs. In PowerShell, this is doable with a call like
scc.exe (Get-ChildItem *.go), becauseGet-ChildItemknows how to expand globs. But a simple call toscc.exe *.gowill result infile or directory could not be read: *.go(unless you actually have a file called*.goof course).This makes globs in the CLI easier to use on Windows. It may also be helpful in certain scripting situations.
If a glob pattern is invalid (the only possible error return value), it falls back to treating the pattern as the actual filepath.
This could negatively impact anyone who has pattern-like syntax in actual filenames (e.g. if someone wanted to match exactly a file called
*.go), but I assume that's a very rare edge-case.I could make this Windows-specific behavior at either runtime via
runtime.GOOSor at compile-time via amain_windows.gofile.