Browse Source

fix flag parsing -- use flagSet after the command

pull/9/head
Jae Kwon 10 years ago
parent
commit
c6c465402e
2 changed files with 17 additions and 17 deletions
  1. +6
    -7
      cmd/main.go
  2. +11
    -10
      config/config.go

+ 6
- 7
cmd/main.go View File

@ -1,18 +1,15 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os"
"github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/config"
) )
func main() { func main() {
// Parse config flags
config.ParseFlags()
args := flag.Args()
args := os.Args[1:]
if len(args) == 0 { if len(args) == 0 {
fmt.Println(`Tendermint fmt.Println(`Tendermint
@ -21,13 +18,13 @@ Commands:
gen_account Generate new account keypair gen_account Generate new account keypair
gen_validator Generate new validator keypair gen_validator Generate new validator keypair
probe_upnp Test UPnP functionality probe_upnp Test UPnP functionality
tendermint --help for command options`)
`)
return return
} }
switch args[0] { switch args[0] {
case "daemon": case "daemon":
config.ParseFlags(args[1:])
daemon() daemon()
case "gen_account": case "gen_account":
gen_account() gen_account()
@ -35,5 +32,7 @@ tendermint --help for command options`)
gen_validator() gen_validator()
case "probe_upnp": case "probe_upnp":
probe_upnp() probe_upnp()
default:
fmt.Println("Unknown command %v", args[0])
} }
} }

+ 11
- 10
config/config.go View File

@ -127,16 +127,18 @@ func DataDir() string { return rootDir + "/data" }
var Config ConfigType 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() 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) configBytes, err := ioutil.ReadFile(configFile)
if err != nil { if err != nil {
defaultConfig.write(configFile) defaultConfig.write(configFile)
@ -157,11 +159,10 @@ func ParseFlags() {
} }
// try to parse arg flags, which can override file configuration. // 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 { if printHelp {
flag.PrintDefaults()
flags.PrintDefaults()
os.Exit(0) os.Exit(0)
} }
} }

Loading…
Cancel
Save