From f2a349608fc2eeb4a18c4aec6db39fcea0647e7a Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 17 Feb 2015 22:21:45 +0100 Subject: [PATCH] make help even nicer When calling --help, this change prints only the usage itself. Before it was calling the help, then continued parsing everything. --- flags.go | 13 +++++++++++-- 1 file 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: ¶mContainer{}, } + 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()