Merge pull request #2 from odeke-em/outdesc-now-stdout
stdout now outfile descriptor
Emmanuel Odeke
8 years ago
23 | 23 | "strings" |
24 | 24 | ) |
25 | 25 | |
26 | var ( | |
27 | OutFileDesc = os.Stdout | |
28 | ) | |
29 | ||
26 | 30 | // A map of all of the registered sub-commands. |
27 | 31 | var cmds map[string]*cmdCont = make(map[string]*cmdCont) |
28 | 32 | |
69 | 73 | program := os.Args[0] |
70 | 74 | if len(cmds) == 0 { |
71 | 75 | // no subcommands |
72 | fmt.Fprintf(os.Stderr, "Usage of %s:\n", program) | |
76 | fmt.Fprintf(OutFileDesc, "Usage of %s:\n", program) | |
73 | 77 | flag.PrintDefaults() |
74 | 78 | return |
75 | 79 | } |
76 | 80 | |
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") | |
79 | 83 | 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) | |
81 | 85 | } |
82 | 86 | |
83 | 87 | if numOfGlobalFlags() > 0 { |
84 | fmt.Fprintf(os.Stderr, "\navailable flags:\n") | |
88 | fmt.Fprintf(OutFileDesc, "\navailable flags:\n") | |
85 | 89 | flag.PrintDefaults() |
86 | 90 | } |
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) | |
88 | 92 | } |
89 | 93 | |
90 | 94 | 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) | |
92 | 96 | // should only output sub command flags, ignore h flag. |
93 | 97 | fs := matchingCmd.command.Flags(flag.NewFlagSet(cont.name, flag.ContinueOnError)) |
94 | 98 | fs.PrintDefaults() |
95 | 99 | 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, ", ")) | |
98 | 102 | } |
99 | 103 | } |
100 | 104 |