0
0
Fork 0

make help even nicer

When calling --help, this change prints only the usage itself. Before it
was calling the help, then continued parsing everything.
This commit is contained in:
Gibheer 2015-02-17 22:21:45 +01:00
parent a81c103572
commit f2a349608f
1 changed files with 11 additions and 2 deletions

View File

@ -78,13 +78,16 @@ type (
// create a new flag handler with the name of the subfunction // create a new flag handler with the name of the subfunction
func NewFlags(method_name string) *Flags { func NewFlags(method_name string) *Flags {
return &Flags{ flagset := flag.NewFlagSet(method_name, flag.ExitOnError)
flags := &Flags{
Name: method_name, Name: method_name,
Flags: &flagSet{}, Flags: &flagSet{},
flagset: flag.NewFlagSet(method_name, flag.ContinueOnError), flagset: flagset,
check_list: make([]flagCheck, 0), check_list: make([]flagCheck, 0),
flag_container: &paramContainer{}, flag_container: &paramContainer{},
} }
flagset.Usage = flags.Usage
return flags
} }
// check all parameters for validity // check all parameters for validity
@ -100,8 +103,14 @@ func (f *Flags) Parse(options []string) error {
return nil return nil
} }
// print a message with the usage part
func (f *Flags) Usagef(message string, args ...interface{}) { func (f *Flags) Usagef(message string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "error: " + message + "\n", args...) 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.Fprintf(os.Stderr, "usage: %s %s [options]\n", os.Args[0], f.Name)
fmt.Fprint(os.Stderr, "where options are:\n") fmt.Fprint(os.Stderr, "where options are:\n")
f.flagset.PrintDefaults() f.flagset.PrintDefaults()