Improved CLI

This commit is contained in:
Eduard Urbach 2024-06-27 20:30:33 +02:00
parent 526b92aa09
commit a64169d624
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 24 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package cli
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"git.akyoto.dev/cli/q/src/build" "git.akyoto.dev/cli/q/src/build"
@ -11,12 +12,12 @@ import (
// Build builds an executable. // Build builds an executable.
func Build(args []string) int { func Build(args []string) int {
b := build.New() b := build.New()
writeExecutable := true dry := false
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
switch args[i] { switch args[i] {
case "--dry": case "--dry":
writeExecutable = false dry = true
case "--verbose", "-v": case "--verbose", "-v":
config.Verbose = true config.Verbose = true
@ -38,7 +39,7 @@ func Build(args []string) int {
result, err := b.Run() result, err := b.Run()
if err != nil { if err != nil {
fmt.Println(err) fmt.Fprintln(os.Stderr, err)
return 1 return 1
} }
@ -48,7 +49,7 @@ func Build(args []string) int {
} }
} }
if !writeExecutable { if dry {
return 0 return 0
} }
@ -57,7 +58,7 @@ func Build(args []string) int {
err = build.Write(path, code, data) err = build.Write(path, code, data)
if err != nil { if err != nil {
fmt.Println(err) fmt.Fprintln(os.Stderr, err)
return 1 return 1
} }

View File

@ -1,12 +1,16 @@
package cli package cli
import "fmt" import (
"fmt"
"io"
)
// Help shows the command line argument usage. // Help shows the command line argument usage.
func Help(args []string) int { func Help(w io.Writer, code int) int {
fmt.Println("Usage: q [command] [options]") fmt.Fprintln(w, "Usage: q [command] [options]")
fmt.Println("") fmt.Fprintln(w, "")
fmt.Println(" build [directory] [file]") fmt.Fprintln(w, " build [directory] [file]")
fmt.Println(" system") fmt.Fprintln(w, " help")
return 2 fmt.Fprintln(w, " system")
return code
} }

View File

@ -1,11 +1,13 @@
package cli package cli
import "os"
// Main is the entry point for the CLI frontend. // Main is the entry point for the CLI frontend.
// It returns the exit code of the compiler. // It returns the exit code of the compiler.
// We never call os.Exit directly here because it's bad for testing. // We never call os.Exit directly here because it's bad for testing.
func Main(args []string) int { func Main(args []string) int {
if len(args) == 0 { if len(args) == 0 {
return Help(nil) return Help(os.Stderr, 2)
} }
switch args[0] { switch args[0] {
@ -15,7 +17,10 @@ func Main(args []string) int {
case "system": case "system":
return System(args[1:]) return System(args[1:])
case "help":
return Help(os.Stdout, 0)
default: default:
return Help(args[1:]) return Help(os.Stderr, 2)
} }
} }