Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package cmdline |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "flag" |
| 7 | "fmt" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | var ( |
| 14 | errEcho = errors.New("echo error") |
| 15 | flagExtra bool |
| 16 | optNoNewline bool |
| 17 | flagTopLevelExtra bool |
| 18 | globalFlag1 string |
| 19 | globalFlag2 *int64 |
| 20 | ) |
| 21 | |
| 22 | // runEcho is used to implement commands for our tests. |
| 23 | func runEcho(cmd *Command, args []string) error { |
| 24 | if len(args) == 1 { |
| 25 | if args[0] == "error" { |
| 26 | return errEcho |
| 27 | } else if args[0] == "bad_arg" { |
Todd Wang | a615e4d | 2014-09-29 16:56:05 -0700 | [diff] [blame] | 28 | return cmd.UsageErrorf("Invalid argument %v", args[0]) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | if flagExtra { |
| 32 | args = append(args, "extra") |
| 33 | } |
| 34 | if flagTopLevelExtra { |
| 35 | args = append(args, "tlextra") |
| 36 | } |
| 37 | if optNoNewline { |
| 38 | fmt.Fprint(cmd.Stdout(), args) |
| 39 | } else { |
| 40 | fmt.Fprintln(cmd.Stdout(), args) |
| 41 | } |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | // runHello is another function for test commands. |
| 46 | func runHello(cmd *Command, args []string) error { |
| 47 | if flagTopLevelExtra { |
| 48 | args = append(args, "tlextra") |
| 49 | } |
| 50 | fmt.Fprintln(cmd.Stdout(), strings.Join(append([]string{"Hello"}, args...), " ")) |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | type testCase struct { |
| 55 | Args []string |
| 56 | Err error |
| 57 | Stdout string |
| 58 | Stderr string |
| 59 | GlobalFlag1 string |
| 60 | GlobalFlag2 int64 |
| 61 | } |
| 62 | |
| 63 | func init() { |
| 64 | flag.StringVar(&globalFlag1, "global1", "", "global test flag 1") |
| 65 | globalFlag2 = flag.Int64("global2", 0, "global test flag 2") |
| 66 | } |
| 67 | |
| 68 | func matchOutput(actual, expect string) bool { |
| 69 | // The global flags include the flags from the testing package, so strip them |
| 70 | // out before the comparison. |
| 71 | re := regexp.MustCompile(" -test.*\n") |
| 72 | return re.ReplaceAllLiteralString(actual, "") == expect |
| 73 | } |
| 74 | |
| 75 | func runTestCases(t *testing.T, cmd *Command, tests []testCase) { |
| 76 | for _, test := range tests { |
| 77 | // Reset global variables before running each test case. |
| 78 | var stdout bytes.Buffer |
| 79 | var stderr bytes.Buffer |
| 80 | flagExtra = false |
| 81 | flagTopLevelExtra = false |
| 82 | optNoNewline = false |
| 83 | globalFlag1 = "" |
| 84 | *globalFlag2 = 0 |
| 85 | |
| 86 | // Run the execute function and check against expected results. |
| 87 | cmd.Init(nil, &stdout, &stderr) |
| 88 | if err := cmd.Execute(test.Args); err != test.Err { |
| 89 | t.Errorf("Ran with args %q\nEXPECTED error:\n%q\nACTUAL error:\n%q", test.Args, test.Err, err) |
| 90 | } |
| 91 | if !matchOutput(stdout.String(), test.Stdout) { |
| 92 | t.Errorf("Ran with args %q\nEXPECTED stdout:\n%q\nACTUAL stdout:\n%q", test.Args, test.Stdout, stdout.String()) |
| 93 | } |
| 94 | if !matchOutput(stderr.String(), test.Stderr) { |
| 95 | t.Errorf("Ran with args %q\nEXPECTED stderr:\n%q\nACTUAL stderr:\n%q", test.Args, test.Stderr, stderr.String()) |
| 96 | } |
| 97 | if globalFlag1 != test.GlobalFlag1 { |
| 98 | t.Errorf("Value for global1 flag %q\nEXPECTED %q", globalFlag1, test.GlobalFlag1) |
| 99 | } |
| 100 | if *globalFlag2 != test.GlobalFlag2 { |
| 101 | t.Errorf("Value for global2 flag %q\nEXPECTED %q", globalFlag2, test.GlobalFlag2) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | func TestNoCommands(t *testing.T) { |
| 107 | cmd := &Command{ |
| 108 | Name: "nocmds", |
| 109 | Short: "Nocmds is invalid.", |
| 110 | Long: "Nocmds has no commands and no run function.", |
| 111 | } |
| 112 | |
| 113 | var tests = []testCase{ |
| 114 | { |
| 115 | Args: []string{}, |
| 116 | Err: ErrUsage, |
| 117 | Stderr: `ERROR: nocmds: neither Children nor Run is specified |
| 118 | |
| 119 | Nocmds has no commands and no run function. |
| 120 | |
| 121 | Usage: |
| 122 | nocmds [ERROR: neither Children nor Run is specified] |
| 123 | |
| 124 | The global flags are: |
| 125 | -global1=: global test flag 1 |
| 126 | -global2=0: global test flag 2 |
| 127 | `, |
| 128 | }, |
| 129 | { |
| 130 | Args: []string{"foo"}, |
| 131 | Err: ErrUsage, |
| 132 | Stderr: `ERROR: nocmds: neither Children nor Run is specified |
| 133 | |
| 134 | Nocmds has no commands and no run function. |
| 135 | |
| 136 | Usage: |
| 137 | nocmds [ERROR: neither Children nor Run is specified] |
| 138 | |
| 139 | The global flags are: |
| 140 | -global1=: global test flag 1 |
| 141 | -global2=0: global test flag 2 |
| 142 | `, |
| 143 | }, |
| 144 | } |
| 145 | runTestCases(t, cmd, tests) |
| 146 | } |
| 147 | |
| 148 | func TestOneCommand(t *testing.T) { |
| 149 | cmdEcho := &Command{ |
| 150 | Name: "echo", |
| 151 | Short: "Print strings on stdout", |
| 152 | Long: ` |
| 153 | Echo prints any strings passed in to stdout. |
| 154 | `, |
| 155 | Run: runEcho, |
| 156 | ArgsName: "[strings]", |
| 157 | ArgsLong: "[strings] are arbitrary strings that will be echoed.", |
| 158 | } |
| 159 | |
| 160 | prog := &Command{ |
| 161 | Name: "onecmd", |
| 162 | Short: "Onecmd program.", |
| 163 | Long: "Onecmd only has the echo command.", |
| 164 | Children: []*Command{cmdEcho}, |
| 165 | } |
| 166 | |
| 167 | var tests = []testCase{ |
| 168 | { |
| 169 | Args: []string{}, |
| 170 | Err: ErrUsage, |
| 171 | Stderr: `ERROR: onecmd: no command specified |
| 172 | |
| 173 | Onecmd only has the echo command. |
| 174 | |
| 175 | Usage: |
| 176 | onecmd <command> |
| 177 | |
| 178 | The onecmd commands are: |
| 179 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 180 | help Display help for commands or topics |
| 181 | Run "onecmd help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 182 | |
| 183 | The global flags are: |
| 184 | -global1=: global test flag 1 |
| 185 | -global2=0: global test flag 2 |
| 186 | `, |
| 187 | }, |
| 188 | { |
| 189 | Args: []string{"foo"}, |
| 190 | Err: ErrUsage, |
| 191 | Stderr: `ERROR: onecmd: unknown command "foo" |
| 192 | |
| 193 | Onecmd only has the echo command. |
| 194 | |
| 195 | Usage: |
| 196 | onecmd <command> |
| 197 | |
| 198 | The onecmd commands are: |
| 199 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 200 | help Display help for commands or topics |
| 201 | Run "onecmd help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 202 | |
| 203 | The global flags are: |
| 204 | -global1=: global test flag 1 |
| 205 | -global2=0: global test flag 2 |
| 206 | `, |
| 207 | }, |
| 208 | { |
| 209 | Args: []string{"help"}, |
| 210 | Stdout: `Onecmd only has the echo command. |
| 211 | |
| 212 | Usage: |
| 213 | onecmd <command> |
| 214 | |
| 215 | The onecmd commands are: |
| 216 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 217 | help Display help for commands or topics |
| 218 | Run "onecmd help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 219 | |
| 220 | The global flags are: |
| 221 | -global1=: global test flag 1 |
| 222 | -global2=0: global test flag 2 |
| 223 | `, |
| 224 | }, |
| 225 | { |
| 226 | Args: []string{"help", "echo"}, |
| 227 | Stdout: `Echo prints any strings passed in to stdout. |
| 228 | |
| 229 | Usage: |
| 230 | onecmd echo [strings] |
| 231 | |
| 232 | [strings] are arbitrary strings that will be echoed. |
| 233 | |
| 234 | The global flags are: |
| 235 | -global1=: global test flag 1 |
| 236 | -global2=0: global test flag 2 |
| 237 | `, |
| 238 | }, |
| 239 | { |
| 240 | Args: []string{"help", "help"}, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 241 | Stdout: `Help with no args displays the usage of the parent command. |
| 242 | Help with args displays the usage of the specified sub-command or help topic. |
| 243 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 244 | |
| 245 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 246 | onecmd help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 247 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 248 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 249 | |
| 250 | The help flags are: |
| 251 | -style=text: The formatting style for help output, either "text" or "godoc". |
| 252 | |
| 253 | The global flags are: |
| 254 | -global1=: global test flag 1 |
| 255 | -global2=0: global test flag 2 |
| 256 | `, |
| 257 | }, |
| 258 | { |
| 259 | Args: []string{"help", "..."}, |
| 260 | Stdout: `Onecmd only has the echo command. |
| 261 | |
| 262 | Usage: |
| 263 | onecmd <command> |
| 264 | |
| 265 | The onecmd commands are: |
| 266 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 267 | help Display help for commands or topics |
| 268 | Run "onecmd help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 269 | |
| 270 | The global flags are: |
| 271 | -global1=: global test flag 1 |
| 272 | -global2=0: global test flag 2 |
| 273 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 274 | Onecmd Echo |
| 275 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 276 | Echo prints any strings passed in to stdout. |
| 277 | |
| 278 | Usage: |
| 279 | onecmd echo [strings] |
| 280 | |
| 281 | [strings] are arbitrary strings that will be echoed. |
| 282 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 283 | Onecmd Help |
| 284 | |
| 285 | Help with no args displays the usage of the parent command. |
| 286 | Help with args displays the usage of the specified sub-command or help topic. |
| 287 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 288 | |
| 289 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 290 | onecmd help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 291 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 292 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 293 | |
| 294 | The help flags are: |
| 295 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 296 | `, |
| 297 | }, |
| 298 | { |
| 299 | Args: []string{"help", "foo"}, |
| 300 | Err: ErrUsage, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 301 | Stderr: `ERROR: onecmd: unknown command or topic "foo" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 302 | |
| 303 | Onecmd only has the echo command. |
| 304 | |
| 305 | Usage: |
| 306 | onecmd <command> |
| 307 | |
| 308 | The onecmd commands are: |
| 309 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 310 | help Display help for commands or topics |
| 311 | Run "onecmd help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 312 | |
| 313 | The global flags are: |
| 314 | -global1=: global test flag 1 |
| 315 | -global2=0: global test flag 2 |
| 316 | `, |
| 317 | }, |
| 318 | { |
| 319 | Args: []string{"echo", "foo", "bar"}, |
| 320 | Stdout: "[foo bar]\n", |
| 321 | }, |
| 322 | { |
| 323 | Args: []string{"echo", "error"}, |
| 324 | Err: errEcho, |
| 325 | }, |
| 326 | { |
| 327 | Args: []string{"echo", "bad_arg"}, |
| 328 | Err: ErrUsage, |
| 329 | Stderr: `ERROR: Invalid argument bad_arg |
| 330 | |
| 331 | Echo prints any strings passed in to stdout. |
| 332 | |
| 333 | Usage: |
| 334 | onecmd echo [strings] |
| 335 | |
| 336 | [strings] are arbitrary strings that will be echoed. |
| 337 | |
| 338 | The global flags are: |
| 339 | -global1=: global test flag 1 |
| 340 | -global2=0: global test flag 2 |
| 341 | `, |
| 342 | }, |
| 343 | } |
| 344 | runTestCases(t, prog, tests) |
| 345 | } |
| 346 | |
| 347 | func TestMultiCommands(t *testing.T) { |
| 348 | cmdEcho := &Command{ |
| 349 | Run: runEcho, |
| 350 | Name: "echo", |
| 351 | Short: "Print strings on stdout", |
| 352 | Long: ` |
| 353 | Echo prints any strings passed in to stdout. |
| 354 | `, |
| 355 | ArgsName: "[strings]", |
| 356 | ArgsLong: "[strings] are arbitrary strings that will be echoed.", |
| 357 | } |
| 358 | var cmdEchoOpt = &Command{ |
| 359 | Run: runEcho, |
| 360 | Name: "echoopt", |
| 361 | Short: "Print strings on stdout, with opts", |
| 362 | // Try varying number of header/trailer newlines around the long description. |
| 363 | Long: `Echoopt prints any args passed in to stdout. |
| 364 | |
| 365 | |
| 366 | `, |
| 367 | ArgsName: "[args]", |
| 368 | ArgsLong: "[args] are arbitrary strings that will be echoed.", |
| 369 | } |
| 370 | cmdEchoOpt.Flags.BoolVar(&optNoNewline, "n", false, "Do not output trailing newline") |
| 371 | |
| 372 | prog := &Command{ |
| 373 | Name: "multi", |
| 374 | Short: "Multi test command", |
| 375 | Long: "Multi has two variants of echo.", |
| 376 | Children: []*Command{cmdEcho, cmdEchoOpt}, |
| 377 | } |
| 378 | prog.Flags.BoolVar(&flagExtra, "extra", false, "Print an extra arg") |
| 379 | |
| 380 | var tests = []testCase{ |
| 381 | { |
| 382 | Args: []string{}, |
| 383 | Err: ErrUsage, |
| 384 | Stderr: `ERROR: multi: no command specified |
| 385 | |
| 386 | Multi has two variants of echo. |
| 387 | |
| 388 | Usage: |
| 389 | multi [flags] <command> |
| 390 | |
| 391 | The multi commands are: |
| 392 | echo Print strings on stdout |
| 393 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 394 | help Display help for commands or topics |
| 395 | Run "multi help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 396 | |
| 397 | The multi flags are: |
| 398 | -extra=false: Print an extra arg |
| 399 | |
| 400 | The global flags are: |
| 401 | -global1=: global test flag 1 |
| 402 | -global2=0: global test flag 2 |
| 403 | `, |
| 404 | }, |
| 405 | { |
| 406 | Args: []string{"help"}, |
| 407 | Stdout: `Multi has two variants of echo. |
| 408 | |
| 409 | Usage: |
| 410 | multi [flags] <command> |
| 411 | |
| 412 | The multi commands are: |
| 413 | echo Print strings on stdout |
| 414 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 415 | help Display help for commands or topics |
| 416 | Run "multi help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 417 | |
| 418 | The multi flags are: |
| 419 | -extra=false: Print an extra arg |
| 420 | |
| 421 | The global flags are: |
| 422 | -global1=: global test flag 1 |
| 423 | -global2=0: global test flag 2 |
| 424 | `, |
| 425 | }, |
| 426 | { |
| 427 | Args: []string{"help", "..."}, |
| 428 | Stdout: `Multi has two variants of echo. |
| 429 | |
| 430 | Usage: |
| 431 | multi [flags] <command> |
| 432 | |
| 433 | The multi commands are: |
| 434 | echo Print strings on stdout |
| 435 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 436 | help Display help for commands or topics |
| 437 | Run "multi help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 438 | |
| 439 | The multi flags are: |
| 440 | -extra=false: Print an extra arg |
| 441 | |
| 442 | The global flags are: |
| 443 | -global1=: global test flag 1 |
| 444 | -global2=0: global test flag 2 |
| 445 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 446 | Multi Echo |
| 447 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 448 | Echo prints any strings passed in to stdout. |
| 449 | |
| 450 | Usage: |
| 451 | multi echo [strings] |
| 452 | |
| 453 | [strings] are arbitrary strings that will be echoed. |
| 454 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 455 | Multi Echoopt |
| 456 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 457 | Echoopt prints any args passed in to stdout. |
| 458 | |
| 459 | Usage: |
| 460 | multi echoopt [flags] [args] |
| 461 | |
| 462 | [args] are arbitrary strings that will be echoed. |
| 463 | |
| 464 | The echoopt flags are: |
| 465 | -n=false: Do not output trailing newline |
| 466 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 467 | Multi Help |
| 468 | |
| 469 | Help with no args displays the usage of the parent command. |
| 470 | Help with args displays the usage of the specified sub-command or help topic. |
| 471 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 472 | |
| 473 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 474 | multi help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 475 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 476 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 477 | |
| 478 | The help flags are: |
| 479 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 480 | `, |
| 481 | }, |
| 482 | { |
| 483 | Args: []string{"help", "echo"}, |
| 484 | Stdout: `Echo prints any strings passed in to stdout. |
| 485 | |
| 486 | Usage: |
| 487 | multi echo [strings] |
| 488 | |
| 489 | [strings] are arbitrary strings that will be echoed. |
| 490 | |
| 491 | The global flags are: |
| 492 | -global1=: global test flag 1 |
| 493 | -global2=0: global test flag 2 |
| 494 | `, |
| 495 | }, |
| 496 | { |
| 497 | Args: []string{"help", "echoopt"}, |
| 498 | Stdout: `Echoopt prints any args passed in to stdout. |
| 499 | |
| 500 | Usage: |
| 501 | multi echoopt [flags] [args] |
| 502 | |
| 503 | [args] are arbitrary strings that will be echoed. |
| 504 | |
| 505 | The echoopt flags are: |
| 506 | -n=false: Do not output trailing newline |
| 507 | |
| 508 | The global flags are: |
| 509 | -global1=: global test flag 1 |
| 510 | -global2=0: global test flag 2 |
| 511 | `, |
| 512 | }, |
| 513 | { |
| 514 | Args: []string{"help", "foo"}, |
| 515 | Err: ErrUsage, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 516 | Stderr: `ERROR: multi: unknown command or topic "foo" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 517 | |
| 518 | Multi has two variants of echo. |
| 519 | |
| 520 | Usage: |
| 521 | multi [flags] <command> |
| 522 | |
| 523 | The multi commands are: |
| 524 | echo Print strings on stdout |
| 525 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 526 | help Display help for commands or topics |
| 527 | Run "multi help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 528 | |
| 529 | The multi flags are: |
| 530 | -extra=false: Print an extra arg |
| 531 | |
| 532 | The global flags are: |
| 533 | -global1=: global test flag 1 |
| 534 | -global2=0: global test flag 2 |
| 535 | `, |
| 536 | }, |
| 537 | { |
| 538 | Args: []string{"echo", "foo", "bar"}, |
| 539 | Stdout: "[foo bar]\n", |
| 540 | }, |
| 541 | { |
| 542 | Args: []string{"-extra", "echo", "foo", "bar"}, |
| 543 | Stdout: "[foo bar extra]\n", |
| 544 | }, |
| 545 | { |
| 546 | Args: []string{"echo", "error"}, |
| 547 | Err: errEcho, |
| 548 | }, |
| 549 | { |
| 550 | Args: []string{"echoopt", "foo", "bar"}, |
| 551 | Stdout: "[foo bar]\n", |
| 552 | }, |
| 553 | { |
| 554 | Args: []string{"-extra", "echoopt", "foo", "bar"}, |
| 555 | Stdout: "[foo bar extra]\n", |
| 556 | }, |
| 557 | { |
| 558 | Args: []string{"echoopt", "-n", "foo", "bar"}, |
| 559 | Stdout: "[foo bar]", |
| 560 | }, |
| 561 | { |
| 562 | Args: []string{"-extra", "echoopt", "-n", "foo", "bar"}, |
| 563 | Stdout: "[foo bar extra]", |
| 564 | }, |
| 565 | { |
| 566 | Args: []string{"-global1=globalStringValue", "-extra", "echoopt", "-n", "foo", "bar"}, |
| 567 | Stdout: "[foo bar extra]", |
| 568 | GlobalFlag1: "globalStringValue", |
| 569 | }, |
| 570 | { |
| 571 | Args: []string{"-global2=42", "echoopt", "-n", "foo", "bar"}, |
| 572 | Stdout: "[foo bar]", |
| 573 | GlobalFlag2: 42, |
| 574 | }, |
| 575 | { |
| 576 | Args: []string{"-global1=globalStringOtherValue", "-global2=43", "-extra", "echoopt", "-n", "foo", "bar"}, |
| 577 | Stdout: "[foo bar extra]", |
| 578 | GlobalFlag1: "globalStringOtherValue", |
| 579 | GlobalFlag2: 43, |
| 580 | }, |
| 581 | { |
| 582 | Args: []string{"echoopt", "error"}, |
| 583 | Err: errEcho, |
| 584 | }, |
| 585 | { |
| 586 | Args: []string{"echo", "-n", "foo", "bar"}, |
| 587 | Err: ErrUsage, |
| 588 | Stderr: `ERROR: flag provided but not defined: -n |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 589 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 590 | Echo prints any strings passed in to stdout. |
| 591 | |
| 592 | Usage: |
| 593 | multi echo [strings] |
| 594 | |
| 595 | [strings] are arbitrary strings that will be echoed. |
| 596 | |
| 597 | The global flags are: |
| 598 | -global1=: global test flag 1 |
| 599 | -global2=0: global test flag 2 |
| 600 | `, |
| 601 | }, |
| 602 | { |
| 603 | Args: []string{"-nosuchflag", "echo", "foo", "bar"}, |
| 604 | Err: ErrUsage, |
| 605 | Stderr: `ERROR: flag provided but not defined: -nosuchflag |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 606 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 607 | Multi has two variants of echo. |
| 608 | |
| 609 | Usage: |
| 610 | multi [flags] <command> |
| 611 | |
| 612 | The multi commands are: |
| 613 | echo Print strings on stdout |
| 614 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 615 | help Display help for commands or topics |
| 616 | Run "multi help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 617 | |
| 618 | The multi flags are: |
| 619 | -extra=false: Print an extra arg |
| 620 | |
| 621 | The global flags are: |
| 622 | -global1=: global test flag 1 |
| 623 | -global2=0: global test flag 2 |
| 624 | `, |
| 625 | }, |
| 626 | } |
| 627 | runTestCases(t, prog, tests) |
| 628 | } |
| 629 | |
| 630 | func TestMultiLevelCommands(t *testing.T) { |
| 631 | cmdEcho := &Command{ |
| 632 | Run: runEcho, |
| 633 | Name: "echo", |
| 634 | Short: "Print strings on stdout", |
| 635 | Long: ` |
| 636 | Echo prints any strings passed in to stdout. |
| 637 | `, |
| 638 | ArgsName: "[strings]", |
| 639 | ArgsLong: "[strings] are arbitrary strings that will be echoed.", |
| 640 | } |
| 641 | cmdEchoOpt := &Command{ |
| 642 | Run: runEcho, |
| 643 | Name: "echoopt", |
| 644 | Short: "Print strings on stdout, with opts", |
| 645 | // Try varying number of header/trailer newlines around the long description. |
| 646 | Long: `Echoopt prints any args passed in to stdout. |
| 647 | |
| 648 | |
| 649 | `, |
| 650 | ArgsName: "[args]", |
| 651 | ArgsLong: "[args] are arbitrary strings that will be echoed.", |
| 652 | } |
| 653 | cmdEchoOpt.Flags.BoolVar(&optNoNewline, "n", false, "Do not output trailing newline") |
| 654 | cmdHello := &Command{ |
| 655 | Run: runHello, |
| 656 | Name: "hello", |
| 657 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 658 | Long: ` |
| 659 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 660 | `, |
| 661 | ArgsName: "[strings]", |
| 662 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 663 | } |
| 664 | echoProg := &Command{ |
| 665 | Name: "echoprog", |
| 666 | Short: "Set of echo commands", |
| 667 | Long: "Echoprog has two variants of echo.", |
| 668 | Children: []*Command{cmdEcho, cmdEchoOpt}, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 669 | Topics: []Topic{ |
| 670 | {Name: "topic3", Short: "Help topic 3 short", Long: "Help topic 3 long."}, |
| 671 | }, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 672 | } |
| 673 | echoProg.Flags.BoolVar(&flagExtra, "extra", false, "Print an extra arg") |
| 674 | prog := &Command{ |
| 675 | Name: "toplevelprog", |
| 676 | Short: "Top level prog", |
| 677 | Long: "Toplevelprog has the echo subprogram and the hello command.", |
| 678 | Children: []*Command{echoProg, cmdHello}, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 679 | Topics: []Topic{ |
| 680 | {Name: "topic1", Short: "Help topic 1 short", Long: "Help topic 1 long."}, |
| 681 | {Name: "topic2", Short: "Help topic 2 short", Long: "Help topic 2 long."}, |
| 682 | }, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 683 | } |
| 684 | prog.Flags.BoolVar(&flagTopLevelExtra, "tlextra", false, "Print an extra arg for all commands") |
| 685 | |
| 686 | var tests = []testCase{ |
| 687 | { |
| 688 | Args: []string{}, |
| 689 | Err: ErrUsage, |
| 690 | Stderr: `ERROR: toplevelprog: no command specified |
| 691 | |
| 692 | Toplevelprog has the echo subprogram and the hello command. |
| 693 | |
| 694 | Usage: |
| 695 | toplevelprog [flags] <command> |
| 696 | |
| 697 | The toplevelprog commands are: |
| 698 | echoprog Set of echo commands |
| 699 | hello Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 700 | help Display help for commands or topics |
| 701 | Run "toplevelprog help [command]" for command usage. |
| 702 | |
| 703 | The toplevelprog additional help topics are: |
| 704 | topic1 Help topic 1 short |
| 705 | topic2 Help topic 2 short |
| 706 | Run "toplevelprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 707 | |
| 708 | The toplevelprog flags are: |
| 709 | -tlextra=false: Print an extra arg for all commands |
| 710 | |
| 711 | The global flags are: |
| 712 | -global1=: global test flag 1 |
| 713 | -global2=0: global test flag 2 |
| 714 | `, |
| 715 | }, |
| 716 | { |
| 717 | Args: []string{"help"}, |
| 718 | Stdout: `Toplevelprog has the echo subprogram and the hello command. |
| 719 | |
| 720 | Usage: |
| 721 | toplevelprog [flags] <command> |
| 722 | |
| 723 | The toplevelprog commands are: |
| 724 | echoprog Set of echo commands |
| 725 | hello Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 726 | help Display help for commands or topics |
| 727 | Run "toplevelprog help [command]" for command usage. |
| 728 | |
| 729 | The toplevelprog additional help topics are: |
| 730 | topic1 Help topic 1 short |
| 731 | topic2 Help topic 2 short |
| 732 | Run "toplevelprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 733 | |
| 734 | The toplevelprog flags are: |
| 735 | -tlextra=false: Print an extra arg for all commands |
| 736 | |
| 737 | The global flags are: |
| 738 | -global1=: global test flag 1 |
| 739 | -global2=0: global test flag 2 |
| 740 | `, |
| 741 | }, |
| 742 | { |
| 743 | Args: []string{"help", "..."}, |
| 744 | Stdout: `Toplevelprog has the echo subprogram and the hello command. |
| 745 | |
| 746 | Usage: |
| 747 | toplevelprog [flags] <command> |
| 748 | |
| 749 | The toplevelprog commands are: |
| 750 | echoprog Set of echo commands |
| 751 | hello Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 752 | help Display help for commands or topics |
| 753 | Run "toplevelprog help [command]" for command usage. |
| 754 | |
| 755 | The toplevelprog additional help topics are: |
| 756 | topic1 Help topic 1 short |
| 757 | topic2 Help topic 2 short |
| 758 | Run "toplevelprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 759 | |
| 760 | The toplevelprog flags are: |
| 761 | -tlextra=false: Print an extra arg for all commands |
| 762 | |
| 763 | The global flags are: |
| 764 | -global1=: global test flag 1 |
| 765 | -global2=0: global test flag 2 |
| 766 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 767 | Toplevelprog Echoprog |
| 768 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 769 | Echoprog has two variants of echo. |
| 770 | |
| 771 | Usage: |
| 772 | toplevelprog echoprog [flags] <command> |
| 773 | |
| 774 | The echoprog commands are: |
| 775 | echo Print strings on stdout |
| 776 | echoopt Print strings on stdout, with opts |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 777 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 778 | The echoprog additional help topics are: |
| 779 | topic3 Help topic 3 short |
| 780 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 781 | The echoprog flags are: |
| 782 | -extra=false: Print an extra arg |
| 783 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 784 | Toplevelprog Echoprog Echo |
| 785 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 786 | Echo prints any strings passed in to stdout. |
| 787 | |
| 788 | Usage: |
| 789 | toplevelprog echoprog echo [strings] |
| 790 | |
| 791 | [strings] are arbitrary strings that will be echoed. |
| 792 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 793 | Toplevelprog Echoprog Echoopt |
| 794 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 795 | Echoopt prints any args passed in to stdout. |
| 796 | |
| 797 | Usage: |
| 798 | toplevelprog echoprog echoopt [flags] [args] |
| 799 | |
| 800 | [args] are arbitrary strings that will be echoed. |
| 801 | |
| 802 | The echoopt flags are: |
| 803 | -n=false: Do not output trailing newline |
| 804 | ================================================================================ |
Todd Wang | 0d12d71 | 2014-10-06 17:25:41 -0700 | [diff] [blame] | 805 | Toplevelprog Echoprog Topic3 - help topic |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 806 | |
| 807 | Help topic 3 long. |
| 808 | ================================================================================ |
| 809 | Toplevelprog Hello |
| 810 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 811 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 812 | |
| 813 | Usage: |
| 814 | toplevelprog hello [strings] |
| 815 | |
| 816 | [strings] are arbitrary strings that will be printed. |
| 817 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 818 | Toplevelprog Help |
| 819 | |
| 820 | Help with no args displays the usage of the parent command. |
| 821 | Help with args displays the usage of the specified sub-command or help topic. |
| 822 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 823 | |
| 824 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 825 | toplevelprog help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 826 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 827 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 828 | |
| 829 | The help flags are: |
| 830 | -style=text: The formatting style for help output, either "text" or "godoc". |
| 831 | ================================================================================ |
Todd Wang | 0d12d71 | 2014-10-06 17:25:41 -0700 | [diff] [blame] | 832 | Toplevelprog Topic1 - help topic |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 833 | |
| 834 | Help topic 1 long. |
| 835 | ================================================================================ |
Todd Wang | 0d12d71 | 2014-10-06 17:25:41 -0700 | [diff] [blame] | 836 | Toplevelprog Topic2 - help topic |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 837 | |
| 838 | Help topic 2 long. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 839 | `, |
| 840 | }, |
| 841 | { |
| 842 | Args: []string{"help", "echoprog"}, |
| 843 | Stdout: `Echoprog has two variants of echo. |
| 844 | |
| 845 | Usage: |
| 846 | toplevelprog echoprog [flags] <command> |
| 847 | |
| 848 | The echoprog commands are: |
| 849 | echo Print strings on stdout |
| 850 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 851 | help Display help for commands or topics |
| 852 | Run "toplevelprog echoprog help [command]" for command usage. |
| 853 | |
| 854 | The echoprog additional help topics are: |
| 855 | topic3 Help topic 3 short |
| 856 | Run "toplevelprog echoprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 857 | |
| 858 | The echoprog flags are: |
| 859 | -extra=false: Print an extra arg |
| 860 | |
| 861 | The global flags are: |
| 862 | -global1=: global test flag 1 |
| 863 | -global2=0: global test flag 2 |
| 864 | `, |
| 865 | }, |
| 866 | { |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 867 | Args: []string{"help", "topic1"}, |
| 868 | Stdout: `Help topic 1 long. |
| 869 | `, |
| 870 | }, |
| 871 | { |
| 872 | Args: []string{"help", "topic2"}, |
| 873 | Stdout: `Help topic 2 long. |
| 874 | `, |
| 875 | }, |
| 876 | { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 877 | Args: []string{"echoprog", "help", "..."}, |
| 878 | Stdout: `Echoprog has two variants of echo. |
| 879 | |
| 880 | Usage: |
| 881 | toplevelprog echoprog [flags] <command> |
| 882 | |
| 883 | The echoprog commands are: |
| 884 | echo Print strings on stdout |
| 885 | echoopt Print strings on stdout, with opts |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 886 | help Display help for commands or topics |
| 887 | Run "toplevelprog echoprog help [command]" for command usage. |
| 888 | |
| 889 | The echoprog additional help topics are: |
| 890 | topic3 Help topic 3 short |
| 891 | Run "toplevelprog echoprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 892 | |
| 893 | The echoprog flags are: |
| 894 | -extra=false: Print an extra arg |
| 895 | |
| 896 | The global flags are: |
| 897 | -global1=: global test flag 1 |
| 898 | -global2=0: global test flag 2 |
| 899 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 900 | Toplevelprog Echoprog Echo |
| 901 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 902 | Echo prints any strings passed in to stdout. |
| 903 | |
| 904 | Usage: |
| 905 | toplevelprog echoprog echo [strings] |
| 906 | |
| 907 | [strings] are arbitrary strings that will be echoed. |
| 908 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 909 | Toplevelprog Echoprog Echoopt |
| 910 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 911 | Echoopt prints any args passed in to stdout. |
| 912 | |
| 913 | Usage: |
| 914 | toplevelprog echoprog echoopt [flags] [args] |
| 915 | |
| 916 | [args] are arbitrary strings that will be echoed. |
| 917 | |
| 918 | The echoopt flags are: |
| 919 | -n=false: Do not output trailing newline |
| 920 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 921 | Toplevelprog Echoprog Help |
| 922 | |
| 923 | Help with no args displays the usage of the parent command. |
| 924 | Help with args displays the usage of the specified sub-command or help topic. |
| 925 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 926 | |
| 927 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 928 | toplevelprog echoprog help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 929 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 930 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 931 | |
| 932 | The help flags are: |
| 933 | -style=text: The formatting style for help output, either "text" or "godoc". |
| 934 | ================================================================================ |
Todd Wang | 0d12d71 | 2014-10-06 17:25:41 -0700 | [diff] [blame] | 935 | Toplevelprog Echoprog Topic3 - help topic |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 936 | |
| 937 | Help topic 3 long. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 938 | `, |
| 939 | }, |
| 940 | { |
| 941 | Args: []string{"echoprog", "help", "echoopt"}, |
| 942 | Stdout: `Echoopt prints any args passed in to stdout. |
| 943 | |
| 944 | Usage: |
| 945 | toplevelprog echoprog echoopt [flags] [args] |
| 946 | |
| 947 | [args] are arbitrary strings that will be echoed. |
| 948 | |
| 949 | The echoopt flags are: |
| 950 | -n=false: Do not output trailing newline |
| 951 | |
| 952 | The global flags are: |
| 953 | -global1=: global test flag 1 |
| 954 | -global2=0: global test flag 2 |
| 955 | `, |
| 956 | }, |
| 957 | { |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 958 | Args: []string{"help", "echoprog", "topic3"}, |
| 959 | Stdout: `Help topic 3 long. |
| 960 | `, |
| 961 | }, |
| 962 | { |
| 963 | Args: []string{"echoprog", "help", "topic3"}, |
| 964 | Stdout: `Help topic 3 long. |
| 965 | `, |
| 966 | }, |
| 967 | { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 968 | Args: []string{"help", "hello"}, |
| 969 | Stdout: `Hello prints any strings passed in to stdout preceded by "Hello". |
| 970 | |
| 971 | Usage: |
| 972 | toplevelprog hello [strings] |
| 973 | |
| 974 | [strings] are arbitrary strings that will be printed. |
| 975 | |
| 976 | The global flags are: |
| 977 | -global1=: global test flag 1 |
| 978 | -global2=0: global test flag 2 |
| 979 | `, |
| 980 | }, |
| 981 | { |
| 982 | Args: []string{"help", "foo"}, |
| 983 | Err: ErrUsage, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 984 | Stderr: `ERROR: toplevelprog: unknown command or topic "foo" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 985 | |
| 986 | Toplevelprog has the echo subprogram and the hello command. |
| 987 | |
| 988 | Usage: |
| 989 | toplevelprog [flags] <command> |
| 990 | |
| 991 | The toplevelprog commands are: |
| 992 | echoprog Set of echo commands |
| 993 | hello Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 994 | help Display help for commands or topics |
| 995 | Run "toplevelprog help [command]" for command usage. |
| 996 | |
| 997 | The toplevelprog additional help topics are: |
| 998 | topic1 Help topic 1 short |
| 999 | topic2 Help topic 2 short |
| 1000 | Run "toplevelprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1001 | |
| 1002 | The toplevelprog flags are: |
| 1003 | -tlextra=false: Print an extra arg for all commands |
| 1004 | |
| 1005 | The global flags are: |
| 1006 | -global1=: global test flag 1 |
| 1007 | -global2=0: global test flag 2 |
| 1008 | `, |
| 1009 | }, |
| 1010 | { |
| 1011 | Args: []string{"echoprog", "echo", "foo", "bar"}, |
| 1012 | Stdout: "[foo bar]\n", |
| 1013 | }, |
| 1014 | { |
| 1015 | Args: []string{"echoprog", "-extra", "echo", "foo", "bar"}, |
| 1016 | Stdout: "[foo bar extra]\n", |
| 1017 | }, |
| 1018 | { |
| 1019 | Args: []string{"echoprog", "echo", "error"}, |
| 1020 | Err: errEcho, |
| 1021 | }, |
| 1022 | { |
| 1023 | Args: []string{"echoprog", "echoopt", "foo", "bar"}, |
| 1024 | Stdout: "[foo bar]\n", |
| 1025 | }, |
| 1026 | { |
| 1027 | Args: []string{"echoprog", "-extra", "echoopt", "foo", "bar"}, |
| 1028 | Stdout: "[foo bar extra]\n", |
| 1029 | }, |
| 1030 | { |
| 1031 | Args: []string{"echoprog", "echoopt", "-n", "foo", "bar"}, |
| 1032 | Stdout: "[foo bar]", |
| 1033 | }, |
| 1034 | { |
| 1035 | Args: []string{"echoprog", "-extra", "echoopt", "-n", "foo", "bar"}, |
| 1036 | Stdout: "[foo bar extra]", |
| 1037 | }, |
| 1038 | { |
| 1039 | Args: []string{"echoprog", "echoopt", "error"}, |
| 1040 | Err: errEcho, |
| 1041 | }, |
| 1042 | { |
| 1043 | Args: []string{"--tlextra", "echoprog", "-extra", "echoopt", "foo", "bar"}, |
| 1044 | Stdout: "[foo bar extra tlextra]\n", |
| 1045 | }, |
| 1046 | { |
| 1047 | Args: []string{"hello", "foo", "bar"}, |
| 1048 | Stdout: "Hello foo bar\n", |
| 1049 | }, |
| 1050 | { |
| 1051 | Args: []string{"--tlextra", "hello", "foo", "bar"}, |
| 1052 | Stdout: "Hello foo bar tlextra\n", |
| 1053 | }, |
| 1054 | { |
| 1055 | Args: []string{"hello", "--extra", "foo", "bar"}, |
| 1056 | Err: ErrUsage, |
| 1057 | Stderr: `ERROR: flag provided but not defined: -extra |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1058 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1059 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1060 | |
| 1061 | Usage: |
| 1062 | toplevelprog hello [strings] |
| 1063 | |
| 1064 | [strings] are arbitrary strings that will be printed. |
| 1065 | |
| 1066 | The global flags are: |
| 1067 | -global1=: global test flag 1 |
| 1068 | -global2=0: global test flag 2 |
| 1069 | `, |
| 1070 | }, |
| 1071 | { |
| 1072 | Args: []string{"-extra", "echoprog", "echoopt", "foo", "bar"}, |
| 1073 | Err: ErrUsage, |
| 1074 | Stderr: `ERROR: flag provided but not defined: -extra |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1075 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1076 | Toplevelprog has the echo subprogram and the hello command. |
| 1077 | |
| 1078 | Usage: |
| 1079 | toplevelprog [flags] <command> |
| 1080 | |
| 1081 | The toplevelprog commands are: |
| 1082 | echoprog Set of echo commands |
| 1083 | hello Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1084 | help Display help for commands or topics |
| 1085 | Run "toplevelprog help [command]" for command usage. |
| 1086 | |
| 1087 | The toplevelprog additional help topics are: |
| 1088 | topic1 Help topic 1 short |
| 1089 | topic2 Help topic 2 short |
| 1090 | Run "toplevelprog help [topic]" for topic details. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1091 | |
| 1092 | The toplevelprog flags are: |
| 1093 | -tlextra=false: Print an extra arg for all commands |
| 1094 | |
| 1095 | The global flags are: |
| 1096 | -global1=: global test flag 1 |
| 1097 | -global2=0: global test flag 2 |
| 1098 | `, |
| 1099 | }, |
| 1100 | } |
| 1101 | runTestCases(t, prog, tests) |
| 1102 | } |
| 1103 | |
| 1104 | func TestMultiLevelCommandsOrdering(t *testing.T) { |
| 1105 | cmdHello11 := &Command{ |
| 1106 | Name: "hello11", |
| 1107 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 1108 | Long: ` |
| 1109 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1110 | `, |
| 1111 | ArgsName: "[strings]", |
| 1112 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1113 | Run: runHello, |
| 1114 | } |
| 1115 | cmdHello12 := &Command{ |
| 1116 | Name: "hello12", |
| 1117 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 1118 | Long: ` |
| 1119 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1120 | `, |
| 1121 | ArgsName: "[strings]", |
| 1122 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1123 | Run: runHello, |
| 1124 | } |
| 1125 | cmdHello21 := &Command{ |
| 1126 | Name: "hello21", |
| 1127 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 1128 | Long: ` |
| 1129 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1130 | `, |
| 1131 | ArgsName: "[strings]", |
| 1132 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1133 | Run: runHello, |
| 1134 | } |
| 1135 | cmdHello22 := &Command{ |
| 1136 | Name: "hello22", |
| 1137 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 1138 | Long: ` |
| 1139 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1140 | `, |
| 1141 | ArgsName: "[strings]", |
| 1142 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1143 | Run: runHello, |
| 1144 | } |
| 1145 | cmdHello31 := &Command{ |
| 1146 | Name: "hello31", |
| 1147 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 1148 | Long: ` |
| 1149 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1150 | `, |
| 1151 | ArgsName: "[strings]", |
| 1152 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1153 | Run: runHello, |
| 1154 | } |
| 1155 | cmdHello32 := &Command{ |
| 1156 | Name: "hello32", |
| 1157 | Short: "Print strings on stdout preceded by \"Hello\"", |
| 1158 | Long: ` |
| 1159 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1160 | `, |
| 1161 | ArgsName: "[strings]", |
| 1162 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1163 | Run: runHello, |
| 1164 | } |
| 1165 | progHello3 := &Command{ |
| 1166 | Name: "prog3", |
| 1167 | Short: "Set of hello commands", |
| 1168 | Long: "Prog3 has two variants of hello.", |
| 1169 | Children: []*Command{cmdHello31, cmdHello32}, |
| 1170 | } |
| 1171 | progHello2 := &Command{ |
| 1172 | Name: "prog2", |
| 1173 | Short: "Set of hello commands", |
| 1174 | Long: "Prog2 has two variants of hello and a subprogram prog3.", |
| 1175 | Children: []*Command{cmdHello21, progHello3, cmdHello22}, |
| 1176 | } |
| 1177 | progHello1 := &Command{ |
| 1178 | Name: "prog1", |
| 1179 | Short: "Set of hello commands", |
| 1180 | Long: "Prog1 has two variants of hello and a subprogram prog2.", |
| 1181 | Children: []*Command{cmdHello11, cmdHello12, progHello2}, |
| 1182 | } |
| 1183 | |
| 1184 | var tests = []testCase{ |
| 1185 | { |
| 1186 | Args: []string{}, |
| 1187 | Err: ErrUsage, |
| 1188 | Stderr: `ERROR: prog1: no command specified |
| 1189 | |
| 1190 | Prog1 has two variants of hello and a subprogram prog2. |
| 1191 | |
| 1192 | Usage: |
| 1193 | prog1 <command> |
| 1194 | |
| 1195 | The prog1 commands are: |
| 1196 | hello11 Print strings on stdout preceded by "Hello" |
| 1197 | hello12 Print strings on stdout preceded by "Hello" |
| 1198 | prog2 Set of hello commands |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1199 | help Display help for commands or topics |
| 1200 | Run "prog1 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1201 | |
| 1202 | The global flags are: |
| 1203 | -global1=: global test flag 1 |
| 1204 | -global2=0: global test flag 2 |
| 1205 | `, |
| 1206 | }, |
| 1207 | { |
| 1208 | Args: []string{"help"}, |
| 1209 | Stdout: `Prog1 has two variants of hello and a subprogram prog2. |
| 1210 | |
| 1211 | Usage: |
| 1212 | prog1 <command> |
| 1213 | |
| 1214 | The prog1 commands are: |
| 1215 | hello11 Print strings on stdout preceded by "Hello" |
| 1216 | hello12 Print strings on stdout preceded by "Hello" |
| 1217 | prog2 Set of hello commands |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1218 | help Display help for commands or topics |
| 1219 | Run "prog1 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1220 | |
| 1221 | The global flags are: |
| 1222 | -global1=: global test flag 1 |
| 1223 | -global2=0: global test flag 2 |
| 1224 | `, |
| 1225 | }, |
| 1226 | { |
| 1227 | Args: []string{"help", "..."}, |
| 1228 | Stdout: `Prog1 has two variants of hello and a subprogram prog2. |
| 1229 | |
| 1230 | Usage: |
| 1231 | prog1 <command> |
| 1232 | |
| 1233 | The prog1 commands are: |
| 1234 | hello11 Print strings on stdout preceded by "Hello" |
| 1235 | hello12 Print strings on stdout preceded by "Hello" |
| 1236 | prog2 Set of hello commands |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1237 | help Display help for commands or topics |
| 1238 | Run "prog1 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1239 | |
| 1240 | The global flags are: |
| 1241 | -global1=: global test flag 1 |
| 1242 | -global2=0: global test flag 2 |
| 1243 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1244 | Prog1 Hello11 |
| 1245 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1246 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1247 | |
| 1248 | Usage: |
| 1249 | prog1 hello11 [strings] |
| 1250 | |
| 1251 | [strings] are arbitrary strings that will be printed. |
| 1252 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1253 | Prog1 Hello12 |
| 1254 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1255 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1256 | |
| 1257 | Usage: |
| 1258 | prog1 hello12 [strings] |
| 1259 | |
| 1260 | [strings] are arbitrary strings that will be printed. |
| 1261 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1262 | Prog1 Prog2 |
| 1263 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1264 | Prog2 has two variants of hello and a subprogram prog3. |
| 1265 | |
| 1266 | Usage: |
| 1267 | prog1 prog2 <command> |
| 1268 | |
| 1269 | The prog2 commands are: |
| 1270 | hello21 Print strings on stdout preceded by "Hello" |
| 1271 | prog3 Set of hello commands |
| 1272 | hello22 Print strings on stdout preceded by "Hello" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1273 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1274 | Prog1 Prog2 Hello21 |
| 1275 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1276 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1277 | |
| 1278 | Usage: |
| 1279 | prog1 prog2 hello21 [strings] |
| 1280 | |
| 1281 | [strings] are arbitrary strings that will be printed. |
| 1282 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1283 | Prog1 Prog2 Prog3 |
| 1284 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1285 | Prog3 has two variants of hello. |
| 1286 | |
| 1287 | Usage: |
| 1288 | prog1 prog2 prog3 <command> |
| 1289 | |
| 1290 | The prog3 commands are: |
| 1291 | hello31 Print strings on stdout preceded by "Hello" |
| 1292 | hello32 Print strings on stdout preceded by "Hello" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1293 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1294 | Prog1 Prog2 Prog3 Hello31 |
| 1295 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1296 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1297 | |
| 1298 | Usage: |
| 1299 | prog1 prog2 prog3 hello31 [strings] |
| 1300 | |
| 1301 | [strings] are arbitrary strings that will be printed. |
| 1302 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1303 | Prog1 Prog2 Prog3 Hello32 |
| 1304 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1305 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1306 | |
| 1307 | Usage: |
| 1308 | prog1 prog2 prog3 hello32 [strings] |
| 1309 | |
| 1310 | [strings] are arbitrary strings that will be printed. |
| 1311 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1312 | Prog1 Prog2 Hello22 |
| 1313 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1314 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1315 | |
| 1316 | Usage: |
| 1317 | prog1 prog2 hello22 [strings] |
| 1318 | |
| 1319 | [strings] are arbitrary strings that will be printed. |
| 1320 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1321 | Prog1 Help |
| 1322 | |
| 1323 | Help with no args displays the usage of the parent command. |
| 1324 | Help with args displays the usage of the specified sub-command or help topic. |
| 1325 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1326 | |
| 1327 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1328 | prog1 help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1329 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1330 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1331 | |
| 1332 | The help flags are: |
| 1333 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1334 | `, |
| 1335 | }, |
| 1336 | { |
| 1337 | Args: []string{"prog2", "help", "..."}, |
| 1338 | Stdout: `Prog2 has two variants of hello and a subprogram prog3. |
| 1339 | |
| 1340 | Usage: |
| 1341 | prog1 prog2 <command> |
| 1342 | |
| 1343 | The prog2 commands are: |
| 1344 | hello21 Print strings on stdout preceded by "Hello" |
| 1345 | prog3 Set of hello commands |
| 1346 | hello22 Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1347 | help Display help for commands or topics |
| 1348 | Run "prog1 prog2 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1349 | |
| 1350 | The global flags are: |
| 1351 | -global1=: global test flag 1 |
| 1352 | -global2=0: global test flag 2 |
| 1353 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1354 | Prog1 Prog2 Hello21 |
| 1355 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1356 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1357 | |
| 1358 | Usage: |
| 1359 | prog1 prog2 hello21 [strings] |
| 1360 | |
| 1361 | [strings] are arbitrary strings that will be printed. |
| 1362 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1363 | Prog1 Prog2 Prog3 |
| 1364 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1365 | Prog3 has two variants of hello. |
| 1366 | |
| 1367 | Usage: |
| 1368 | prog1 prog2 prog3 <command> |
| 1369 | |
| 1370 | The prog3 commands are: |
| 1371 | hello31 Print strings on stdout preceded by "Hello" |
| 1372 | hello32 Print strings on stdout preceded by "Hello" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1373 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1374 | Prog1 Prog2 Prog3 Hello31 |
| 1375 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1376 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1377 | |
| 1378 | Usage: |
| 1379 | prog1 prog2 prog3 hello31 [strings] |
| 1380 | |
| 1381 | [strings] are arbitrary strings that will be printed. |
| 1382 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1383 | Prog1 Prog2 Prog3 Hello32 |
| 1384 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1385 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1386 | |
| 1387 | Usage: |
| 1388 | prog1 prog2 prog3 hello32 [strings] |
| 1389 | |
| 1390 | [strings] are arbitrary strings that will be printed. |
| 1391 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1392 | Prog1 Prog2 Hello22 |
| 1393 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1394 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1395 | |
| 1396 | Usage: |
| 1397 | prog1 prog2 hello22 [strings] |
| 1398 | |
| 1399 | [strings] are arbitrary strings that will be printed. |
| 1400 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1401 | Prog1 Prog2 Help |
| 1402 | |
| 1403 | Help with no args displays the usage of the parent command. |
| 1404 | Help with args displays the usage of the specified sub-command or help topic. |
| 1405 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1406 | |
| 1407 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1408 | prog1 prog2 help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1409 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1410 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1411 | |
| 1412 | The help flags are: |
| 1413 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1414 | `, |
| 1415 | }, |
| 1416 | { |
| 1417 | Args: []string{"prog2", "prog3", "help", "..."}, |
| 1418 | Stdout: `Prog3 has two variants of hello. |
| 1419 | |
| 1420 | Usage: |
| 1421 | prog1 prog2 prog3 <command> |
| 1422 | |
| 1423 | The prog3 commands are: |
| 1424 | hello31 Print strings on stdout preceded by "Hello" |
| 1425 | hello32 Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1426 | help Display help for commands or topics |
| 1427 | Run "prog1 prog2 prog3 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1428 | |
| 1429 | The global flags are: |
| 1430 | -global1=: global test flag 1 |
| 1431 | -global2=0: global test flag 2 |
| 1432 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1433 | Prog1 Prog2 Prog3 Hello31 |
| 1434 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1435 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1436 | |
| 1437 | Usage: |
| 1438 | prog1 prog2 prog3 hello31 [strings] |
| 1439 | |
| 1440 | [strings] are arbitrary strings that will be printed. |
| 1441 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1442 | Prog1 Prog2 Prog3 Hello32 |
| 1443 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1444 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1445 | |
| 1446 | Usage: |
| 1447 | prog1 prog2 prog3 hello32 [strings] |
| 1448 | |
| 1449 | [strings] are arbitrary strings that will be printed. |
| 1450 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1451 | Prog1 Prog2 Prog3 Help |
| 1452 | |
| 1453 | Help with no args displays the usage of the parent command. |
| 1454 | Help with args displays the usage of the specified sub-command or help topic. |
| 1455 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1456 | |
| 1457 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1458 | prog1 prog2 prog3 help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1459 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1460 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1461 | |
| 1462 | The help flags are: |
| 1463 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1464 | `, |
| 1465 | }, |
| 1466 | { |
| 1467 | Args: []string{"help", "prog2", "prog3", "..."}, |
| 1468 | Stdout: `Prog3 has two variants of hello. |
| 1469 | |
| 1470 | Usage: |
| 1471 | prog1 prog2 prog3 <command> |
| 1472 | |
| 1473 | The prog3 commands are: |
| 1474 | hello31 Print strings on stdout preceded by "Hello" |
| 1475 | hello32 Print strings on stdout preceded by "Hello" |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1476 | help Display help for commands or topics |
| 1477 | Run "prog1 prog2 prog3 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1478 | |
| 1479 | The global flags are: |
| 1480 | -global1=: global test flag 1 |
| 1481 | -global2=0: global test flag 2 |
| 1482 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1483 | Prog1 Prog2 Prog3 Hello31 |
| 1484 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1485 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1486 | |
| 1487 | Usage: |
| 1488 | prog1 prog2 prog3 hello31 [strings] |
| 1489 | |
| 1490 | [strings] are arbitrary strings that will be printed. |
| 1491 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1492 | Prog1 Prog2 Prog3 Hello32 |
| 1493 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1494 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1495 | |
| 1496 | Usage: |
| 1497 | prog1 prog2 prog3 hello32 [strings] |
| 1498 | |
| 1499 | [strings] are arbitrary strings that will be printed. |
| 1500 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1501 | Prog1 Prog2 Prog3 Help |
| 1502 | |
| 1503 | Help with no args displays the usage of the parent command. |
| 1504 | Help with args displays the usage of the specified sub-command or help topic. |
| 1505 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1506 | |
| 1507 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1508 | prog1 prog2 prog3 help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1509 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1510 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1511 | |
| 1512 | The help flags are: |
| 1513 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1514 | `, |
| 1515 | }, |
| 1516 | { |
| 1517 | Args: []string{"help", "-style=godoc", "..."}, |
| 1518 | Stdout: `Prog1 has two variants of hello and a subprogram prog2. |
| 1519 | |
| 1520 | Usage: |
| 1521 | prog1 <command> |
| 1522 | |
| 1523 | The prog1 commands are: |
| 1524 | hello11 Print strings on stdout preceded by "Hello" |
| 1525 | hello12 Print strings on stdout preceded by "Hello" |
| 1526 | prog2 Set of hello commands |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1527 | help Display help for commands or topics |
| 1528 | Run "prog1 help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1529 | |
| 1530 | The global flags are: |
| 1531 | -global1=: global test flag 1 |
| 1532 | -global2=0: global test flag 2 |
| 1533 | |
| 1534 | Prog1 Hello11 |
| 1535 | |
| 1536 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1537 | |
| 1538 | Usage: |
| 1539 | prog1 hello11 [strings] |
| 1540 | |
| 1541 | [strings] are arbitrary strings that will be printed. |
| 1542 | |
| 1543 | Prog1 Hello12 |
| 1544 | |
| 1545 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1546 | |
| 1547 | Usage: |
| 1548 | prog1 hello12 [strings] |
| 1549 | |
| 1550 | [strings] are arbitrary strings that will be printed. |
| 1551 | |
| 1552 | Prog1 Prog2 |
| 1553 | |
| 1554 | Prog2 has two variants of hello and a subprogram prog3. |
| 1555 | |
| 1556 | Usage: |
| 1557 | prog1 prog2 <command> |
| 1558 | |
| 1559 | The prog2 commands are: |
| 1560 | hello21 Print strings on stdout preceded by "Hello" |
| 1561 | prog3 Set of hello commands |
| 1562 | hello22 Print strings on stdout preceded by "Hello" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1563 | |
| 1564 | Prog1 Prog2 Hello21 |
| 1565 | |
| 1566 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1567 | |
| 1568 | Usage: |
| 1569 | prog1 prog2 hello21 [strings] |
| 1570 | |
| 1571 | [strings] are arbitrary strings that will be printed. |
| 1572 | |
| 1573 | Prog1 Prog2 Prog3 |
| 1574 | |
| 1575 | Prog3 has two variants of hello. |
| 1576 | |
| 1577 | Usage: |
| 1578 | prog1 prog2 prog3 <command> |
| 1579 | |
| 1580 | The prog3 commands are: |
| 1581 | hello31 Print strings on stdout preceded by "Hello" |
| 1582 | hello32 Print strings on stdout preceded by "Hello" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1583 | |
| 1584 | Prog1 Prog2 Prog3 Hello31 |
| 1585 | |
| 1586 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1587 | |
| 1588 | Usage: |
| 1589 | prog1 prog2 prog3 hello31 [strings] |
| 1590 | |
| 1591 | [strings] are arbitrary strings that will be printed. |
| 1592 | |
| 1593 | Prog1 Prog2 Prog3 Hello32 |
| 1594 | |
| 1595 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1596 | |
| 1597 | Usage: |
| 1598 | prog1 prog2 prog3 hello32 [strings] |
| 1599 | |
| 1600 | [strings] are arbitrary strings that will be printed. |
| 1601 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1602 | Prog1 Prog2 Hello22 |
| 1603 | |
| 1604 | Hello prints any strings passed in to stdout preceded by "Hello". |
| 1605 | |
| 1606 | Usage: |
| 1607 | prog1 prog2 hello22 [strings] |
| 1608 | |
| 1609 | [strings] are arbitrary strings that will be printed. |
| 1610 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1611 | Prog1 Help |
| 1612 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1613 | Help with no args displays the usage of the parent command. |
| 1614 | Help with args displays the usage of the specified sub-command or help topic. |
| 1615 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1616 | |
| 1617 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1618 | prog1 help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1619 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1620 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1621 | |
| 1622 | The help flags are: |
| 1623 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1624 | `, |
| 1625 | }, |
| 1626 | } |
| 1627 | |
| 1628 | runTestCases(t, progHello1, tests) |
| 1629 | } |
| 1630 | |
| 1631 | func TestCommandAndArgs(t *testing.T) { |
| 1632 | cmdEcho := &Command{ |
| 1633 | Name: "echo", |
| 1634 | Short: "Print strings on stdout", |
| 1635 | Long: ` |
| 1636 | Echo prints any strings passed in to stdout. |
| 1637 | `, |
| 1638 | Run: runEcho, |
| 1639 | ArgsName: "[strings]", |
| 1640 | ArgsLong: "[strings] are arbitrary strings that will be echoed.", |
| 1641 | } |
| 1642 | |
| 1643 | prog := &Command{ |
| 1644 | Name: "cmdargs", |
| 1645 | Short: "Cmdargs program.", |
| 1646 | Long: "Cmdargs has the echo command and a Run function with args.", |
| 1647 | Children: []*Command{cmdEcho}, |
| 1648 | Run: runHello, |
| 1649 | ArgsName: "[strings]", |
| 1650 | ArgsLong: "[strings] are arbitrary strings that will be printed.", |
| 1651 | } |
| 1652 | |
| 1653 | var tests = []testCase{ |
| 1654 | { |
| 1655 | Args: []string{}, |
| 1656 | Stdout: "Hello\n", |
| 1657 | }, |
| 1658 | { |
| 1659 | Args: []string{"foo"}, |
| 1660 | Stdout: "Hello foo\n", |
| 1661 | }, |
| 1662 | { |
| 1663 | Args: []string{"help"}, |
| 1664 | Stdout: `Cmdargs has the echo command and a Run function with args. |
| 1665 | |
| 1666 | Usage: |
| 1667 | cmdargs <command> |
| 1668 | cmdargs [strings] |
| 1669 | |
| 1670 | The cmdargs commands are: |
| 1671 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1672 | help Display help for commands or topics |
| 1673 | Run "cmdargs help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1674 | |
| 1675 | [strings] are arbitrary strings that will be printed. |
| 1676 | |
| 1677 | The global flags are: |
| 1678 | -global1=: global test flag 1 |
| 1679 | -global2=0: global test flag 2 |
| 1680 | `, |
| 1681 | }, |
| 1682 | { |
| 1683 | Args: []string{"help", "echo"}, |
| 1684 | Stdout: `Echo prints any strings passed in to stdout. |
| 1685 | |
| 1686 | Usage: |
| 1687 | cmdargs echo [strings] |
| 1688 | |
| 1689 | [strings] are arbitrary strings that will be echoed. |
| 1690 | |
| 1691 | The global flags are: |
| 1692 | -global1=: global test flag 1 |
| 1693 | -global2=0: global test flag 2 |
| 1694 | `, |
| 1695 | }, |
| 1696 | { |
| 1697 | Args: []string{"help", "..."}, |
| 1698 | Stdout: `Cmdargs has the echo command and a Run function with args. |
| 1699 | |
| 1700 | Usage: |
| 1701 | cmdargs <command> |
| 1702 | cmdargs [strings] |
| 1703 | |
| 1704 | The cmdargs commands are: |
| 1705 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1706 | help Display help for commands or topics |
| 1707 | Run "cmdargs help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1708 | |
| 1709 | [strings] are arbitrary strings that will be printed. |
| 1710 | |
| 1711 | The global flags are: |
| 1712 | -global1=: global test flag 1 |
| 1713 | -global2=0: global test flag 2 |
| 1714 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1715 | Cmdargs Echo |
| 1716 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1717 | Echo prints any strings passed in to stdout. |
| 1718 | |
| 1719 | Usage: |
| 1720 | cmdargs echo [strings] |
| 1721 | |
| 1722 | [strings] are arbitrary strings that will be echoed. |
| 1723 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1724 | Cmdargs Help |
| 1725 | |
| 1726 | Help with no args displays the usage of the parent command. |
| 1727 | Help with args displays the usage of the specified sub-command or help topic. |
| 1728 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1729 | |
| 1730 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1731 | cmdargs help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1732 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1733 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1734 | |
| 1735 | The help flags are: |
| 1736 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1737 | `, |
| 1738 | }, |
| 1739 | { |
| 1740 | Args: []string{"help", "foo"}, |
| 1741 | Err: ErrUsage, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1742 | Stderr: `ERROR: cmdargs: unknown command or topic "foo" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1743 | |
| 1744 | Cmdargs has the echo command and a Run function with args. |
| 1745 | |
| 1746 | Usage: |
| 1747 | cmdargs <command> |
| 1748 | cmdargs [strings] |
| 1749 | |
| 1750 | The cmdargs commands are: |
| 1751 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1752 | help Display help for commands or topics |
| 1753 | Run "cmdargs help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1754 | |
| 1755 | [strings] are arbitrary strings that will be printed. |
| 1756 | |
| 1757 | The global flags are: |
| 1758 | -global1=: global test flag 1 |
| 1759 | -global2=0: global test flag 2 |
| 1760 | `, |
| 1761 | }, |
| 1762 | { |
| 1763 | Args: []string{"echo", "foo", "bar"}, |
| 1764 | Stdout: "[foo bar]\n", |
| 1765 | }, |
| 1766 | { |
| 1767 | Args: []string{"echo", "error"}, |
| 1768 | Err: errEcho, |
| 1769 | }, |
| 1770 | { |
| 1771 | Args: []string{"echo", "bad_arg"}, |
| 1772 | Err: ErrUsage, |
| 1773 | Stderr: `ERROR: Invalid argument bad_arg |
| 1774 | |
| 1775 | Echo prints any strings passed in to stdout. |
| 1776 | |
| 1777 | Usage: |
| 1778 | cmdargs echo [strings] |
| 1779 | |
| 1780 | [strings] are arbitrary strings that will be echoed. |
| 1781 | |
| 1782 | The global flags are: |
| 1783 | -global1=: global test flag 1 |
| 1784 | -global2=0: global test flag 2 |
| 1785 | `, |
| 1786 | }, |
| 1787 | } |
| 1788 | runTestCases(t, prog, tests) |
| 1789 | } |
| 1790 | |
| 1791 | func TestCommandAndRunNoArgs(t *testing.T) { |
| 1792 | cmdEcho := &Command{ |
| 1793 | Name: "echo", |
| 1794 | Short: "Print strings on stdout", |
| 1795 | Long: ` |
| 1796 | Echo prints any strings passed in to stdout. |
| 1797 | `, |
| 1798 | Run: runEcho, |
| 1799 | ArgsName: "[strings]", |
| 1800 | ArgsLong: "[strings] are arbitrary strings that will be echoed.", |
| 1801 | } |
| 1802 | |
| 1803 | prog := &Command{ |
| 1804 | Name: "cmdrun", |
| 1805 | Short: "Cmdrun program.", |
| 1806 | Long: "Cmdrun has the echo command and a Run function with no args.", |
| 1807 | Children: []*Command{cmdEcho}, |
| 1808 | Run: runHello, |
| 1809 | } |
| 1810 | |
| 1811 | var tests = []testCase{ |
| 1812 | { |
| 1813 | Args: []string{}, |
| 1814 | Stdout: "Hello\n", |
| 1815 | }, |
| 1816 | { |
| 1817 | Args: []string{"foo"}, |
| 1818 | Err: ErrUsage, |
| 1819 | Stderr: `ERROR: cmdrun: unknown command "foo" |
| 1820 | |
| 1821 | Cmdrun has the echo command and a Run function with no args. |
| 1822 | |
| 1823 | Usage: |
| 1824 | cmdrun <command> |
| 1825 | cmdrun |
| 1826 | |
| 1827 | The cmdrun commands are: |
| 1828 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1829 | help Display help for commands or topics |
| 1830 | Run "cmdrun help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1831 | |
| 1832 | The global flags are: |
| 1833 | -global1=: global test flag 1 |
| 1834 | -global2=0: global test flag 2 |
| 1835 | `, |
| 1836 | }, |
| 1837 | { |
| 1838 | Args: []string{"help"}, |
| 1839 | Stdout: `Cmdrun has the echo command and a Run function with no args. |
| 1840 | |
| 1841 | Usage: |
| 1842 | cmdrun <command> |
| 1843 | cmdrun |
| 1844 | |
| 1845 | The cmdrun commands are: |
| 1846 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1847 | help Display help for commands or topics |
| 1848 | Run "cmdrun help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1849 | |
| 1850 | The global flags are: |
| 1851 | -global1=: global test flag 1 |
| 1852 | -global2=0: global test flag 2 |
| 1853 | `, |
| 1854 | }, |
| 1855 | { |
| 1856 | Args: []string{"help", "echo"}, |
| 1857 | Stdout: `Echo prints any strings passed in to stdout. |
| 1858 | |
| 1859 | Usage: |
| 1860 | cmdrun echo [strings] |
| 1861 | |
| 1862 | [strings] are arbitrary strings that will be echoed. |
| 1863 | |
| 1864 | The global flags are: |
| 1865 | -global1=: global test flag 1 |
| 1866 | -global2=0: global test flag 2 |
| 1867 | `, |
| 1868 | }, |
| 1869 | { |
| 1870 | Args: []string{"help", "..."}, |
| 1871 | Stdout: `Cmdrun has the echo command and a Run function with no args. |
| 1872 | |
| 1873 | Usage: |
| 1874 | cmdrun <command> |
| 1875 | cmdrun |
| 1876 | |
| 1877 | The cmdrun commands are: |
| 1878 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1879 | help Display help for commands or topics |
| 1880 | Run "cmdrun help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1881 | |
| 1882 | The global flags are: |
| 1883 | -global1=: global test flag 1 |
| 1884 | -global2=0: global test flag 2 |
| 1885 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1886 | Cmdrun Echo |
| 1887 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1888 | Echo prints any strings passed in to stdout. |
| 1889 | |
| 1890 | Usage: |
| 1891 | cmdrun echo [strings] |
| 1892 | |
| 1893 | [strings] are arbitrary strings that will be echoed. |
| 1894 | ================================================================================ |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1895 | Cmdrun Help |
| 1896 | |
| 1897 | Help with no args displays the usage of the parent command. |
| 1898 | Help with args displays the usage of the specified sub-command or help topic. |
| 1899 | "help ..." recursively displays help for all commands and topics. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1900 | |
| 1901 | Usage: |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1902 | cmdrun help [flags] [command/topic ...] |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1903 | |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1904 | [command/topic ...] optionally identifies a specific sub-command or help topic. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1905 | |
| 1906 | The help flags are: |
| 1907 | -style=text: The formatting style for help output, either "text" or "godoc". |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1908 | `, |
| 1909 | }, |
| 1910 | { |
| 1911 | Args: []string{"help", "foo"}, |
| 1912 | Err: ErrUsage, |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1913 | Stderr: `ERROR: cmdrun: unknown command or topic "foo" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1914 | |
| 1915 | Cmdrun has the echo command and a Run function with no args. |
| 1916 | |
| 1917 | Usage: |
| 1918 | cmdrun <command> |
| 1919 | cmdrun |
| 1920 | |
| 1921 | The cmdrun commands are: |
| 1922 | echo Print strings on stdout |
Todd Wang | a3ce2be | 2014-10-05 12:10:15 -0700 | [diff] [blame] | 1923 | help Display help for commands or topics |
| 1924 | Run "cmdrun help [command]" for command usage. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1925 | |
| 1926 | The global flags are: |
| 1927 | -global1=: global test flag 1 |
| 1928 | -global2=0: global test flag 2 |
| 1929 | `, |
| 1930 | }, |
| 1931 | { |
| 1932 | Args: []string{"echo", "foo", "bar"}, |
| 1933 | Stdout: "[foo bar]\n", |
| 1934 | }, |
| 1935 | { |
| 1936 | Args: []string{"echo", "error"}, |
| 1937 | Err: errEcho, |
| 1938 | }, |
| 1939 | { |
| 1940 | Args: []string{"echo", "bad_arg"}, |
| 1941 | Err: ErrUsage, |
| 1942 | Stderr: `ERROR: Invalid argument bad_arg |
| 1943 | |
| 1944 | Echo prints any strings passed in to stdout. |
| 1945 | |
| 1946 | Usage: |
| 1947 | cmdrun echo [strings] |
| 1948 | |
| 1949 | [strings] are arbitrary strings that will be echoed. |
| 1950 | |
| 1951 | The global flags are: |
| 1952 | -global1=: global test flag 1 |
| 1953 | -global2=0: global test flag 2 |
| 1954 | `, |
| 1955 | }, |
| 1956 | } |
| 1957 | runTestCases(t, prog, tests) |
| 1958 | } |
Asim Shankar | fb5f1c1 | 2014-10-15 03:37:38 -0700 | [diff] [blame] | 1959 | |
| 1960 | func TestLongCommandsHelp(t *testing.T) { |
| 1961 | cmdLong := &Command{ |
| 1962 | Name: "thisisaverylongcommand", |
| 1963 | Short: "description of very long command.", |
| 1964 | Long: "blah blah blah.", |
| 1965 | Run: runEcho, |
| 1966 | } |
| 1967 | cmdShort := &Command{ |
| 1968 | Name: "x", |
| 1969 | Short: "description of short command.", |
| 1970 | Long: "blah blah blah", |
| 1971 | Run: runEcho, |
| 1972 | } |
| 1973 | prog := &Command{ |
| 1974 | Name: "program", |
| 1975 | Short: "Test help strings when there are long commands.", |
| 1976 | Long: "Test help strings when there are long commands.", |
| 1977 | Children: []*Command{cmdShort, cmdLong}, |
| 1978 | } |
| 1979 | var tests = []testCase{ |
| 1980 | { |
| 1981 | Args: []string{"help"}, |
| 1982 | Stdout: `Test help strings when there are long commands. |
| 1983 | |
| 1984 | Usage: |
| 1985 | program <command> |
| 1986 | |
| 1987 | The program commands are: |
| 1988 | x description of short command. |
| 1989 | thisisaverylongcommand description of very long command. |
| 1990 | help Display help for commands or topics |
| 1991 | Run "program help [command]" for command usage. |
| 1992 | |
| 1993 | The global flags are: |
| 1994 | -global1=: global test flag 1 |
| 1995 | -global2=0: global test flag 2 |
| 1996 | `, |
| 1997 | }, |
| 1998 | } |
| 1999 | runTestCases(t, prog, tests) |
| 2000 | } |