From a64169d6248441527fd573d3ccd6d43fa55cb49c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 27 Jun 2024 20:30:33 +0200 Subject: [PATCH] Improved CLI --- src/cli/Build.go | 11 ++++++----- src/cli/Help.go | 18 +++++++++++------- src/cli/Main.go | 9 +++++++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/cli/Build.go b/src/cli/Build.go index 2553996..cd5ac11 100644 --- a/src/cli/Build.go +++ b/src/cli/Build.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "os" "strings" "git.akyoto.dev/cli/q/src/build" @@ -11,12 +12,12 @@ import ( // Build builds an executable. func Build(args []string) int { b := build.New() - writeExecutable := true + dry := false for i := 0; i < len(args); i++ { switch args[i] { case "--dry": - writeExecutable = false + dry = true case "--verbose", "-v": config.Verbose = true @@ -38,7 +39,7 @@ func Build(args []string) int { result, err := b.Run() if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) return 1 } @@ -48,7 +49,7 @@ func Build(args []string) int { } } - if !writeExecutable { + if dry { return 0 } @@ -57,7 +58,7 @@ func Build(args []string) int { err = build.Write(path, code, data) if err != nil { - fmt.Println(err) + fmt.Fprintln(os.Stderr, err) return 1 } diff --git a/src/cli/Help.go b/src/cli/Help.go index ace2380..cf7c00f 100644 --- a/src/cli/Help.go +++ b/src/cli/Help.go @@ -1,12 +1,16 @@ package cli -import "fmt" +import ( + "fmt" + "io" +) // Help shows the command line argument usage. -func Help(args []string) int { - fmt.Println("Usage: q [command] [options]") - fmt.Println("") - fmt.Println(" build [directory] [file]") - fmt.Println(" system") - return 2 +func Help(w io.Writer, code int) int { + fmt.Fprintln(w, "Usage: q [command] [options]") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " build [directory] [file]") + fmt.Fprintln(w, " help") + fmt.Fprintln(w, " system") + return code } diff --git a/src/cli/Main.go b/src/cli/Main.go index e681239..2b90bda 100644 --- a/src/cli/Main.go +++ b/src/cli/Main.go @@ -1,11 +1,13 @@ package cli +import "os" + // Main is the entry point for the CLI frontend. // It returns the exit code of the compiler. // We never call os.Exit directly here because it's bad for testing. func Main(args []string) int { if len(args) == 0 { - return Help(nil) + return Help(os.Stderr, 2) } switch args[0] { @@ -15,7 +17,10 @@ func Main(args []string) int { case "system": return System(args[1:]) + case "help": + return Help(os.Stdout, 0) + default: - return Help(args[1:]) + return Help(os.Stderr, 2) } }