From 0407c133037d522283bc53aa611677f821b234e3 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 9 Sep 2023 10:44:37 +0200 Subject: Support reading from stdin and multiple inputs --- main.go | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 1da9ba1..1c94efa 100644 --- a/main.go +++ b/main.go @@ -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) -- cgit v1.2.3