From c6c465402e6bd800c565217f0de5260b05cd9612 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Sun, 28 Dec 2014 15:08:07 -0800 Subject: [PATCH] fix flag parsing -- use flagSet after the command --- cmd/main.go | 13 ++++++------- config/config.go | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index cb5d57ebf..35a8446d4 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,18 +1,15 @@ package main import ( - "flag" "fmt" + "os" "github.com/tendermint/tendermint/config" ) func main() { - // Parse config flags - config.ParseFlags() - - args := flag.Args() + args := os.Args[1:] if len(args) == 0 { fmt.Println(`Tendermint @@ -21,13 +18,13 @@ Commands: gen_account Generate new account keypair gen_validator Generate new validator keypair probe_upnp Test UPnP functionality - -tendermint --help for command options`) +`) return } switch args[0] { case "daemon": + config.ParseFlags(args[1:]) daemon() case "gen_account": gen_account() @@ -35,5 +32,7 @@ tendermint --help for command options`) gen_validator() case "probe_upnp": probe_upnp() + default: + fmt.Println("Unknown command %v", args[0]) } } diff --git a/config/config.go b/config/config.go index 8131276c2..2972eebd8 100644 --- a/config/config.go +++ b/config/config.go @@ -127,16 +127,18 @@ func DataDir() string { return rootDir + "/data" } var Config ConfigType -func setFlags(printHelp *bool) { - flag.BoolVar(printHelp, "help", false, "Print this help message.") - flag.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)") - flag.StringVar(&Config.SeedNode, "seed", Config.SeedNode, "Address of seed node") +func parseFlags(flags *flag.FlagSet, args []string) (printHelp bool) { + flags.BoolVar(&printHelp, "help", false, "Print this help message.") + flags.StringVar(&Config.LAddr, "laddr", Config.LAddr, "Listen address. (0.0.0.0:0 means any interface, any port)") + flags.StringVar(&Config.SeedNode, "seed", Config.SeedNode, "Address of seed node") + flags.Parse(args) + return } -func ParseFlags() { +func ParseFlags(args []string) { configFile := ConfigFile() - // try to read configuration. if missing, write default + // try to read configuration from file. if missing, write default configBytes, err := ioutil.ReadFile(configFile) if err != nil { defaultConfig.write(configFile) @@ -157,11 +159,10 @@ func ParseFlags() { } // try to parse arg flags, which can override file configuration. - var printHelp bool - setFlags(&printHelp) - flag.Parse() + flags := flag.NewFlagSet("main", flag.ExitOnError) + printHelp := parseFlags(flags, args) if printHelp { - flag.PrintDefaults() + flags.PrintDefaults() os.Exit(0) } }