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.
This commit is contained in:
Andrew Slotin 2017-10-12 11:47:00 +07:00
parent 3c22c6227b
commit 46e67d5848
1 changed files with 12 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
@ -19,11 +20,19 @@ var memBolt *BoltDB
var currentFilename string var currentFilename string
func init() {
flag.Usage = func() {
fmt.Fprintf(os.Stdout, "Usage: %s <filename(s)>\n", ProgramName)
}
}
func main() { func main() {
var err error var err error
if len(os.Args) < 2 { flag.Parse()
fmt.Printf("Usage: %s <filename(s)>\n", ProgramName)
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1) os.Exit(1)
} }
@ -35,7 +44,7 @@ func main() {
style := defaultStyle() style := defaultStyle()
termbox.SetOutputMode(termbox.Output256) termbox.SetOutputMode(termbox.Output256)
databaseFiles := os.Args[1:] databaseFiles := flag.Args()
for _, databaseFile := range databaseFiles { for _, databaseFile := range databaseFiles {
currentFilename = databaseFile currentFilename = databaseFile
db, err = bolt.Open(databaseFile, 0600, nil) db, err = bolt.Open(databaseFile, 0600, nil)