| 160 | } |
| 161 | |
| 162 | func usage(msg string) { |
| 163 | cmdName := filepath.Base(os.Args[0]) |
| 164 | if msg != "" { |
| 165 | Errorf("Error: %v\n", msg) |
| 166 | } |
| 167 | var modesQualifer string |
| 168 | if !*FlagHelp { |
| 169 | modesQualifer = " (use --help to see all modes)" |
| 170 | } |
| 171 | Errorf(` |
| 172 | Usage: `+cmdName+` [globalopts] <mode> [commandopts] [commandargs] |
| 173 | |
| 174 | Modes:%s |
| 175 | |
| 176 | `, modesQualifer) |
| 177 | var modes []string |
| 178 | for mode, cmd := range modeCommand { |
| 179 | if des, ok := cmd.(describer); ok && (*FlagHelp || !demote(cmd)) { |
| 180 | modes = append(modes, fmt.Sprintf(" %s: %s\n", mode, des.Describe())) |
| 181 | } |
| 182 | } |
| 183 | sort.Strings(modes) |
| 184 | for i := range modes { |
| 185 | Errorf("%s", modes[i]) |
| 186 | } |
| 187 | |
| 188 | Errorf("\nExamples:\n") |
| 189 | modes = nil |
| 190 | for mode, cmd := range modeCommand { |
| 191 | if ex, ok := cmd.(exampler); ok && (*FlagHelp || !demote(cmd)) { |
| 192 | line := "" |
| 193 | exs := ex.Examples() |
| 194 | if len(exs) > 0 { |
| 195 | line = "\n" |
| 196 | } |
| 197 | for _, example := range exs { |
| 198 | line += fmt.Sprintf(" %s %s %s\n", cmdName, mode, example) |
| 199 | } |
| 200 | modes = append(modes, line) |
| 201 | } |
| 202 | } |
| 203 | sort.Strings(modes) |
| 204 | for i := range modes { |
| 205 | Errorf("%s", modes[i]) |
| 206 | } |
| 207 | |
| 208 | Errorf("\nFor mode-specific help:\n\n ") |
| 209 | Errorf("%s <mode> -help\n", cmdName) |
| 210 | |
| 211 | Errorf("\nGlobal options:\n") |
| 212 | flag.PrintDefaults() |
| 213 | Exit(1) |
| 214 | } |
| 215 | |
| 216 | func help(mode string) { |
| 217 | cmdName := os.Args[0] |