aboutsummaryrefslogtreecommitdiff
path: root/flags.go
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-17 22:15:21 +0100
committerGibheer <gibheer@gmail.com>2015-02-17 22:15:21 +0100
commita81c10357289a085170d6f3dadc2c6efd5794593 (patch)
treedd269d0ec299356a0b4be886501b3bd210a4d6cc /flags.go
parent470d21409b5f26d49fefd34d75b82197045d4811 (diff)
make error messages nicer
This changes the error message so that not the program help is printed but instead the specific submenu help. This should result in much faster and better understanding of the problematic situation.
Diffstat (limited to 'flags.go')
-rw-r--r--flags.go20
1 files changed, 16 insertions, 4 deletions
diff --git a/flags.go b/flags.go
index 157dd04..c6118e7 100644
--- a/flags.go
+++ b/flags.go
@@ -65,11 +65,12 @@ type (
}
Flags struct {
+ Name string // name of the sub function
flagset *flag.FlagSet // the flagset reference for printing the help
flag_container *paramContainer
- Flags *flagSet // the end result of the flag setting
+ Flags *flagSet // the end result of the flag setting
- check_list []flagCheck // list of all checks
+ check_list []flagCheck // list of all checks
}
flagCheck func()(error)
@@ -78,8 +79,9 @@ type (
// create a new flag handler with the name of the subfunction
func NewFlags(method_name string) *Flags {
return &Flags{
+ Name: method_name,
Flags: &flagSet{},
- flagset: flag.NewFlagSet(method_name, flag.ExitOnError),
+ flagset: flag.NewFlagSet(method_name, flag.ContinueOnError),
check_list: make([]flagCheck, 0),
flag_container: &paramContainer{},
}
@@ -90,11 +92,21 @@ func (f *Flags) Parse(options []string) error {
f.flagset.Parse(options)
for _, check := range f.check_list {
// TODO handle error in a betetr way (output specific help, not command help)
- if err := check(); err != nil { return err }
+ if err := check(); err != nil {
+ f.Usagef("%s", err)
+ return err
+ }
}
return nil
}
+func (f *Flags) Usagef(message string, args ...interface{}) {
+ fmt.Fprintf(os.Stderr, "error: " + message + "\n", args...)
+ fmt.Fprintf(os.Stderr, "usage: %s %s [options]\n", os.Args[0], f.Name)
+ fmt.Fprint(os.Stderr, "where options are:\n")
+ f.flagset.PrintDefaults()
+}
+
// add the private key option to the requested flags
func (f *Flags) AddPrivateKey() {
f.check_list = append(f.check_list, f.parsePrivateKey)