aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGibheer <gibheer@gmail.com>2015-02-17 22:21:45 +0100
committerGibheer <gibheer@gmail.com>2015-02-17 22:21:45 +0100
commitf2a349608fc2eeb4a18c4aec6db39fcea0647e7a (patch)
tree982b3eb1c35d0c1c67d5490fcea29125bb838de1
parenta81c10357289a085170d6f3dadc2c6efd5794593 (diff)
make help even nicer
When calling --help, this change prints only the usage itself. Before it was calling the help, then continued parsing everything.
-rw-r--r--flags.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/flags.go b/flags.go
index c6118e7..6c0a76d 100644
--- a/flags.go
+++ b/flags.go
@@ -78,13 +78,16 @@ type (
// create a new flag handler with the name of the subfunction
func NewFlags(method_name string) *Flags {
- return &Flags{
+ flagset := flag.NewFlagSet(method_name, flag.ExitOnError)
+ flags := &Flags{
Name: method_name,
Flags: &flagSet{},
- flagset: flag.NewFlagSet(method_name, flag.ContinueOnError),
+ flagset: flagset,
check_list: make([]flagCheck, 0),
flag_container: &paramContainer{},
}
+ flagset.Usage = flags.Usage
+ return flags
}
// check all parameters for validity
@@ -100,8 +103,14 @@ func (f *Flags) Parse(options []string) error {
return nil
}
+// print a message with the usage part
func (f *Flags) Usagef(message string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "error: " + message + "\n", args...)
+ f.Usage()
+}
+
+// print the usage of the current flag set
+func (f *Flags) Usage() {
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()