diff options
author | Alex Crawford <wally@code.acrawford.com> | 2021-04-07 15:54:13 -0700 |
---|---|---|
committer | Alex Crawford <wally@code.acrawford.com> | 2021-04-07 16:13:01 -0700 |
commit | 98a3959eeedc2e4fdb38acf8de9a9543badb825d (patch) | |
tree | 9b5733cdf463a20107eadb961c3296fea76b01e5 | |
parent | c579dc8aaf592804f381a35796ec1a30aeb20459 (diff) |
main: use standard flag parsing
I had to read the code to find out the correct form of the version flag.
Moving to the standard library's flag implementation provides a better
usage message out of the box and allows for better extensibility for
future flags.
-rw-r--r-- | main.go | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "github.com/caarlos0/spin" "gopkg.in/cheggaaa/pb.v1" @@ -18,21 +19,24 @@ type state struct { } func main() { - var args = os.Args[1:] - s := state{step: 0, total: 0, sent: 0} - - if len(args) != 1 { - fmt.Println("Usage: wally-cli <firmware file>") - os.Exit(2) + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s: [flags] <firmware file>\n", os.Args[0]) + flag.PrintDefaults() } + version := flag.Bool("version", false, "print the version and exit") + flag.Parse() - if args[0] == "--version" { - appVersion := fmt.Sprintf("wally-cli v%s", appVersion) - fmt.Println(appVersion) + if *version { + fmt.Println(fmt.Sprintf("wally-cli v%s", appVersion)) os.Exit(0) } - path := args[0] + if flag.NArg() != 1 { + flag.Usage() + os.Exit(2) + } + + path := flag.Arg(0) extension := filepath.Ext(path) if extension != ".bin" && extension != ".hex" { fmt.Println("Please provide a valid firmware file: a .hex file (ErgoDox EZ) or a .bin file (Moonlander / Planck EZ)") @@ -51,6 +55,7 @@ func main() { var progress *pb.ProgressBar progressStarted := false + s := state{step: 0, total: 0, sent: 0} if extension == ".bin" { go dfuFlash(path, &s) } |