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 (
"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
}

View File

@ -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
}

View File

@ -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)
}
}