diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-09-09 10:44:37 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-09-09 10:44:37 +0200 |
commit | 0407c133037d522283bc53aa611677f821b234e3 (patch) | |
tree | e88f96654dc5846b46a95b0d7d3b1efebe2b5e21 | |
parent | fc6d6410f16e56d39288deed424a642482e7ca2e (diff) |
Support reading from stdin and multiple inputs
-rw-r--r-- | main.go | 28 |
1 files changed, 21 insertions, 7 deletions
@@ -9,15 +9,29 @@ import ( ) func main() { - if len(os.Args) != 2 { - fmt.Fprintf(os.Stderr, "Usage: %s file\n", os.Args[0]) - os.Exit(1) + if len(os.Args) == 1 { + process("-") } - file, err := os.Open(os.Args[1]) - if err != nil { - die(err) + + for _, arg := range os.Args[1:] { + process(arg) + } +} + +func process(filename string) { + var file *os.File + var err error + + if filename == "-" { + file = os.Stdin + } else { + file, err = os.Open(filename) + if err != nil { + die(err) + } + defer file.Close() } - defer file.Close() + ast, err := parser.ParseFile(file) if err != nil { die(err) |