Skip to content

Commit 0ebe958

Browse files
committed
cli: add help and version global flags
1 parent f34e883 commit 0ebe958

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

cli/cli.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"fmt"
88
"os"
99
"path"
10+
"runtime"
11+
"slices"
1012
"strconv"
1113
"strings"
1214

@@ -53,8 +55,15 @@ func (a *Application) Run() {
5355
return
5456
} else if os.Args[1] == CliCommandName {
5557
// ok, handle here below
58+
} else if slices.Contains([]string{"-h", "--help"}, os.Args[1]) {
59+
a.printGlobalHelp()
60+
os.Exit(0)
61+
} else if slices.Contains([]string{"-v", "--version"}, os.Args[1]) {
62+
a.printVersion()
63+
os.Exit(0)
5664
} else {
57-
a.logger.Fatalf("Unknown command '%s', try '%s cli -h' for info on cli commands or '%s' to start awl server", os.Args[1], binaryName, binaryName)
65+
fmt.Printf("Unknown command '%s'. Use '%s -h' for help, or run '%s' to start the server\n", os.Args[1], binaryName, binaryName)
66+
os.Exit(-1)
5867
}
5968

6069
err := a.cliapp.Run(os.Args[1:])
@@ -65,6 +74,26 @@ func (a *Application) Run() {
6574
os.Exit(0)
6675
}
6776

77+
func (a *Application) printGlobalHelp() {
78+
fmt.Printf(`Usage: %s <command>
79+
80+
Run without a command to start the server.
81+
82+
Flags:
83+
-h, --help Show context-sensitive help.
84+
-v, --version Show version.
85+
86+
Commands:
87+
cli Command line interface for Anywherelan
88+
89+
Run "%s <command> --help" for more information on a command.
90+
`, binaryName, binaryName)
91+
}
92+
93+
func (a *Application) printVersion() {
94+
fmt.Printf("Anywherelan version %s (%s %s-%s)\n", config.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
95+
}
96+
6897
func (a *Application) init() {
6998
a.cliapp = &cli.App{
7099
Name: "awl",

0 commit comments

Comments
 (0)