gogrep
gogrep copied to clipboard
Allow standard input too
Sometimes it's useful to be able to search an arbitrary portion of a Go program (for example to see where any identifiers named "a" are within a given function). It would be nice if gogrep could do this.
This occurred to me too, but I'm not sure how to go about it. The tool uses gotool
, which means that any arguments are taken as either package paths, directories or Go files making up a single package.
And, when you don't give any arguments, .
is assumed.
On the other hand, grep
tools tend to understand no arguments as standard input. Should we do the same? Or did you have another way of specifying stdin in mind?
We could always go with the slightly hacky -
argument meaning stdin.
I'm also not clear on how we could support arbitrary portions of Go code. Surely those can't always type-check properly. Are you saying that we would ignore typechecking in that scenario?
Yeah, maybe gogrep -i
to use stdin? I'm not generally keen on abusing -
to mean stdin, although the probability of collision is pretty low here.
As for type checking, I'd drop typechecking when reading stdin unless the input represents a complete Go package file. It could print an error if a type was specified in the pattern.
Well, -
is neither a valid Go package name nor a valid Go source file name, so I'd think it's a better option than adding a flag. I dislike that strategy when a -
argument could be valid, such as when one does cat -
expecting to print the file named -
.
Dropping typechecking sounds like something we have to do one way or another. For example, typechecking a single file out of an entire package could very easily not typecheck. I'll open a separate issue.
@rogpeppe feel free to work on this if you'd like, since I have little use for partial source code parsing (I assume you do because of Acme).
parse.go
already has the code you need, and quite funnily it's a more powerful version of what gofmt
uses when reading stdin. gofmt
allows files, top-level declarations and statements, while we allow those as well as value and type expressions.