From 46e67d58481dc14277e701947c462dbca08d7549 Mon Sep 17 00:00:00 2001 From: Andrew Slotin Date: Thu, 12 Oct 2017 11:47:00 +0700 Subject: [PATCH] Add --help flag support Currently executing `boltbrowser --help` creates a file with name `--help` in the current working directory, which is counterintuitive. This commit adds command-line flags support to boltbrowser and changes it to print out the usage message instead. --- boltbrowser.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/boltbrowser.go b/boltbrowser.go index 3fc886e..0dcd2e1 100644 --- a/boltbrowser.go +++ b/boltbrowser.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "os" @@ -19,11 +20,19 @@ var memBolt *BoltDB var currentFilename string +func init() { + flag.Usage = func() { + fmt.Fprintf(os.Stdout, "Usage: %s \n", ProgramName) + } +} + func main() { var err error - if len(os.Args) < 2 { - fmt.Printf("Usage: %s \n", ProgramName) + flag.Parse() + + if flag.NArg() == 0 { + flag.Usage() os.Exit(1) } @@ -35,7 +44,7 @@ func main() { style := defaultStyle() termbox.SetOutputMode(termbox.Output256) - databaseFiles := os.Args[1:] + databaseFiles := flag.Args() for _, databaseFile := range databaseFiles { currentFilename = databaseFile db, err = bolt.Open(databaseFile, 0600, nil)