Codebase list golang-github-odeke-em-command / 3ce5162
Merge pull request #2 from odeke-em/outdesc-now-stdout stdout now outfile descriptor Emmanuel Odeke 8 years ago
1 changed file(s) with 13 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
2323 "strings"
2424 )
2525
26 var (
27 OutFileDesc = os.Stdout
28 )
29
2630 // A map of all of the registered sub-commands.
2731 var cmds map[string]*cmdCont = make(map[string]*cmdCont)
2832
6973 program := os.Args[0]
7074 if len(cmds) == 0 {
7175 // no subcommands
72 fmt.Fprintf(os.Stderr, "Usage of %s:\n", program)
76 fmt.Fprintf(OutFileDesc, "Usage of %s:\n", program)
7377 flag.PrintDefaults()
7478 return
7579 }
7680
77 fmt.Fprintf(os.Stderr, "Usage: %s <command>\n\n", program)
78 fmt.Fprintf(os.Stderr, "where <command> is one of:\n")
81 fmt.Fprintf(OutFileDesc, "Usage: %s <command>\n\n", program)
82 fmt.Fprintf(OutFileDesc, "where <command> is one of:\n")
7983 for name, cont := range cmds {
80 fmt.Fprintf(os.Stderr, " %-15s %s\n", name, cont.desc)
84 fmt.Fprintf(OutFileDesc, " %-15s %s\n", name, cont.desc)
8185 }
8286
8387 if numOfGlobalFlags() > 0 {
84 fmt.Fprintf(os.Stderr, "\navailable flags:\n")
88 fmt.Fprintf(OutFileDesc, "\navailable flags:\n")
8589 flag.PrintDefaults()
8690 }
87 fmt.Fprintf(os.Stderr, "\n%s <command> -h for subcommand help\n", program)
91 fmt.Fprintf(OutFileDesc, "\n%s <command> -h for subcommand help\n", program)
8892 }
8993
9094 func subcommandUsage(cont *cmdCont) {
91 fmt.Fprintf(os.Stderr, "Usage of %s %s:\n", os.Args[0], cont.name)
95 fmt.Fprintf(OutFileDesc, "Usage of %s %s:\n", os.Args[0], cont.name)
9296 // should only output sub command flags, ignore h flag.
9397 fs := matchingCmd.command.Flags(flag.NewFlagSet(cont.name, flag.ContinueOnError))
9498 fs.PrintDefaults()
9599 if len(cont.requiredFlags) > 0 {
96 fmt.Fprintf(os.Stderr, "\nrequired flags:\n")
97 fmt.Fprintf(os.Stderr, " %s\n\n", strings.Join(cont.requiredFlags, ", "))
100 fmt.Fprintf(OutFileDesc, "\nrequired flags:\n")
101 fmt.Fprintf(OutFileDesc, " %s\n\n", strings.Join(cont.requiredFlags, ", "))
98102 }
99103 }
100104