blob: e0de532e1a89e1ab989167e77626f77ec090e900 [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package cmdline
2
3import (
4 "bytes"
5 "errors"
6 "flag"
7 "fmt"
8 "regexp"
9 "strings"
10 "testing"
11)
12
13var (
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.
23func 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 Wanga615e4d2014-09-29 16:56:05 -070028 return cmd.UsageErrorf("Invalid argument %v", args[0])
Jiri Simsa5293dcb2014-05-10 09:56:38 -070029 }
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.
46func 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
54type testCase struct {
55 Args []string
56 Err error
57 Stdout string
58 Stderr string
59 GlobalFlag1 string
60 GlobalFlag2 int64
61}
62
63func init() {
64 flag.StringVar(&globalFlag1, "global1", "", "global test flag 1")
65 globalFlag2 = flag.Int64("global2", 0, "global test flag 2")
66}
67
68func 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
75func 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
106func 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
119Nocmds has no commands and no run function.
120
121Usage:
122 nocmds [ERROR: neither Children nor Run is specified]
123
124The 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
134Nocmds has no commands and no run function.
135
136Usage:
137 nocmds [ERROR: neither Children nor Run is specified]
138
139The 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
148func TestOneCommand(t *testing.T) {
149 cmdEcho := &Command{
150 Name: "echo",
151 Short: "Print strings on stdout",
152 Long: `
153Echo 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
173Onecmd only has the echo command.
174
175Usage:
176 onecmd <command>
177
178The onecmd commands are:
179 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -0700180 help Display help for commands or topics
181Run "onecmd help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700182
183The 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
193Onecmd only has the echo command.
194
195Usage:
196 onecmd <command>
197
198The onecmd commands are:
199 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -0700200 help Display help for commands or topics
201Run "onecmd help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700202
203The 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
212Usage:
213 onecmd <command>
214
215The onecmd commands are:
216 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -0700217 help Display help for commands or topics
218Run "onecmd help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700219
220The 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
229Usage:
230 onecmd echo [strings]
231
232[strings] are arbitrary strings that will be echoed.
233
234The 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 Wanga3ce2be2014-10-05 12:10:15 -0700241 Stdout: `Help with no args displays the usage of the parent command.
242Help with args displays the usage of the specified sub-command or help topic.
243"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700244
245Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -0700246 onecmd help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700247
Todd Wanga3ce2be2014-10-05 12:10:15 -0700248[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700249
250The help flags are:
251 -style=text: The formatting style for help output, either "text" or "godoc".
252
253The 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
262Usage:
263 onecmd <command>
264
265The onecmd commands are:
266 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -0700267 help Display help for commands or topics
268Run "onecmd help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700269
270The global flags are:
271 -global1=: global test flag 1
272 -global2=0: global test flag 2
273================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700274Onecmd Echo
275
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700276Echo prints any strings passed in to stdout.
277
278Usage:
279 onecmd echo [strings]
280
281[strings] are arbitrary strings that will be echoed.
282================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700283Onecmd Help
284
285Help with no args displays the usage of the parent command.
286Help with args displays the usage of the specified sub-command or help topic.
287"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700288
289Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -0700290 onecmd help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700291
Todd Wanga3ce2be2014-10-05 12:10:15 -0700292[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700293
294The help flags are:
295 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700296`,
297 },
298 {
299 Args: []string{"help", "foo"},
300 Err: ErrUsage,
Todd Wanga3ce2be2014-10-05 12:10:15 -0700301 Stderr: `ERROR: onecmd: unknown command or topic "foo"
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700302
303Onecmd only has the echo command.
304
305Usage:
306 onecmd <command>
307
308The onecmd commands are:
309 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -0700310 help Display help for commands or topics
311Run "onecmd help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700312
313The 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
331Echo prints any strings passed in to stdout.
332
333Usage:
334 onecmd echo [strings]
335
336[strings] are arbitrary strings that will be echoed.
337
338The 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
347func TestMultiCommands(t *testing.T) {
348 cmdEcho := &Command{
349 Run: runEcho,
350 Name: "echo",
351 Short: "Print strings on stdout",
352 Long: `
353Echo 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
386Multi has two variants of echo.
387
388Usage:
389 multi [flags] <command>
390
391The multi commands are:
392 echo Print strings on stdout
393 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700394 help Display help for commands or topics
395Run "multi help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700396
397The multi flags are:
398 -extra=false: Print an extra arg
399
400The 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
409Usage:
410 multi [flags] <command>
411
412The multi commands are:
413 echo Print strings on stdout
414 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700415 help Display help for commands or topics
416Run "multi help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700417
418The multi flags are:
419 -extra=false: Print an extra arg
420
421The 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
430Usage:
431 multi [flags] <command>
432
433The multi commands are:
434 echo Print strings on stdout
435 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700436 help Display help for commands or topics
437Run "multi help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700438
439The multi flags are:
440 -extra=false: Print an extra arg
441
442The global flags are:
443 -global1=: global test flag 1
444 -global2=0: global test flag 2
445================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700446Multi Echo
447
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700448Echo prints any strings passed in to stdout.
449
450Usage:
451 multi echo [strings]
452
453[strings] are arbitrary strings that will be echoed.
454================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700455Multi Echoopt
456
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700457Echoopt prints any args passed in to stdout.
458
459Usage:
460 multi echoopt [flags] [args]
461
462[args] are arbitrary strings that will be echoed.
463
464The echoopt flags are:
465 -n=false: Do not output trailing newline
466================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700467Multi Help
468
469Help with no args displays the usage of the parent command.
470Help with args displays the usage of the specified sub-command or help topic.
471"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700472
473Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -0700474 multi help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700475
Todd Wanga3ce2be2014-10-05 12:10:15 -0700476[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700477
478The help flags are:
479 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700480`,
481 },
482 {
483 Args: []string{"help", "echo"},
484 Stdout: `Echo prints any strings passed in to stdout.
485
486Usage:
487 multi echo [strings]
488
489[strings] are arbitrary strings that will be echoed.
490
491The 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
500Usage:
501 multi echoopt [flags] [args]
502
503[args] are arbitrary strings that will be echoed.
504
505The echoopt flags are:
506 -n=false: Do not output trailing newline
507
508The 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 Wanga3ce2be2014-10-05 12:10:15 -0700516 Stderr: `ERROR: multi: unknown command or topic "foo"
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700517
518Multi has two variants of echo.
519
520Usage:
521 multi [flags] <command>
522
523The multi commands are:
524 echo Print strings on stdout
525 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700526 help Display help for commands or topics
527Run "multi help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700528
529The multi flags are:
530 -extra=false: Print an extra arg
531
532The 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 Wanga3ce2be2014-10-05 12:10:15 -0700589
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700590Echo prints any strings passed in to stdout.
591
592Usage:
593 multi echo [strings]
594
595[strings] are arbitrary strings that will be echoed.
596
597The 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 Wanga3ce2be2014-10-05 12:10:15 -0700606
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700607Multi has two variants of echo.
608
609Usage:
610 multi [flags] <command>
611
612The multi commands are:
613 echo Print strings on stdout
614 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700615 help Display help for commands or topics
616Run "multi help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700617
618The multi flags are:
619 -extra=false: Print an extra arg
620
621The 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
630func TestMultiLevelCommands(t *testing.T) {
631 cmdEcho := &Command{
632 Run: runEcho,
633 Name: "echo",
634 Short: "Print strings on stdout",
635 Long: `
636Echo 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: `
659Hello 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 Wanga3ce2be2014-10-05 12:10:15 -0700669 Topics: []Topic{
670 {Name: "topic3", Short: "Help topic 3 short", Long: "Help topic 3 long."},
671 },
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700672 }
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 Wanga3ce2be2014-10-05 12:10:15 -0700679 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 Simsa5293dcb2014-05-10 09:56:38 -0700683 }
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
692Toplevelprog has the echo subprogram and the hello command.
693
694Usage:
695 toplevelprog [flags] <command>
696
697The toplevelprog commands are:
698 echoprog Set of echo commands
699 hello Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -0700700 help Display help for commands or topics
701Run "toplevelprog help [command]" for command usage.
702
703The toplevelprog additional help topics are:
704 topic1 Help topic 1 short
705 topic2 Help topic 2 short
706Run "toplevelprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700707
708The toplevelprog flags are:
709 -tlextra=false: Print an extra arg for all commands
710
711The 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
720Usage:
721 toplevelprog [flags] <command>
722
723The toplevelprog commands are:
724 echoprog Set of echo commands
725 hello Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -0700726 help Display help for commands or topics
727Run "toplevelprog help [command]" for command usage.
728
729The toplevelprog additional help topics are:
730 topic1 Help topic 1 short
731 topic2 Help topic 2 short
732Run "toplevelprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700733
734The toplevelprog flags are:
735 -tlextra=false: Print an extra arg for all commands
736
737The 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
746Usage:
747 toplevelprog [flags] <command>
748
749The toplevelprog commands are:
750 echoprog Set of echo commands
751 hello Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -0700752 help Display help for commands or topics
753Run "toplevelprog help [command]" for command usage.
754
755The toplevelprog additional help topics are:
756 topic1 Help topic 1 short
757 topic2 Help topic 2 short
758Run "toplevelprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700759
760The toplevelprog flags are:
761 -tlextra=false: Print an extra arg for all commands
762
763The global flags are:
764 -global1=: global test flag 1
765 -global2=0: global test flag 2
766================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700767Toplevelprog Echoprog
768
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700769Echoprog has two variants of echo.
770
771Usage:
772 toplevelprog echoprog [flags] <command>
773
774The echoprog commands are:
775 echo Print strings on stdout
776 echoopt Print strings on stdout, with opts
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700777
Todd Wanga3ce2be2014-10-05 12:10:15 -0700778The echoprog additional help topics are:
779 topic3 Help topic 3 short
780
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700781The echoprog flags are:
782 -extra=false: Print an extra arg
783================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700784Toplevelprog Echoprog Echo
785
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700786Echo prints any strings passed in to stdout.
787
788Usage:
789 toplevelprog echoprog echo [strings]
790
791[strings] are arbitrary strings that will be echoed.
792================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700793Toplevelprog Echoprog Echoopt
794
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700795Echoopt prints any args passed in to stdout.
796
797Usage:
798 toplevelprog echoprog echoopt [flags] [args]
799
800[args] are arbitrary strings that will be echoed.
801
802The echoopt flags are:
803 -n=false: Do not output trailing newline
804================================================================================
Todd Wang0d12d712014-10-06 17:25:41 -0700805Toplevelprog Echoprog Topic3 - help topic
Todd Wanga3ce2be2014-10-05 12:10:15 -0700806
807Help topic 3 long.
808================================================================================
809Toplevelprog Hello
810
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700811Hello prints any strings passed in to stdout preceded by "Hello".
812
813Usage:
814 toplevelprog hello [strings]
815
816[strings] are arbitrary strings that will be printed.
817================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700818Toplevelprog Help
819
820Help with no args displays the usage of the parent command.
821Help with args displays the usage of the specified sub-command or help topic.
822"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700823
824Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -0700825 toplevelprog help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700826
Todd Wanga3ce2be2014-10-05 12:10:15 -0700827[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700828
829The help flags are:
830 -style=text: The formatting style for help output, either "text" or "godoc".
831================================================================================
Todd Wang0d12d712014-10-06 17:25:41 -0700832Toplevelprog Topic1 - help topic
Todd Wanga3ce2be2014-10-05 12:10:15 -0700833
834Help topic 1 long.
835================================================================================
Todd Wang0d12d712014-10-06 17:25:41 -0700836Toplevelprog Topic2 - help topic
Todd Wanga3ce2be2014-10-05 12:10:15 -0700837
838Help topic 2 long.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700839`,
840 },
841 {
842 Args: []string{"help", "echoprog"},
843 Stdout: `Echoprog has two variants of echo.
844
845Usage:
846 toplevelprog echoprog [flags] <command>
847
848The echoprog commands are:
849 echo Print strings on stdout
850 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700851 help Display help for commands or topics
852Run "toplevelprog echoprog help [command]" for command usage.
853
854The echoprog additional help topics are:
855 topic3 Help topic 3 short
856Run "toplevelprog echoprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700857
858The echoprog flags are:
859 -extra=false: Print an extra arg
860
861The global flags are:
862 -global1=: global test flag 1
863 -global2=0: global test flag 2
864`,
865 },
866 {
Todd Wanga3ce2be2014-10-05 12:10:15 -0700867 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 Simsa5293dcb2014-05-10 09:56:38 -0700877 Args: []string{"echoprog", "help", "..."},
878 Stdout: `Echoprog has two variants of echo.
879
880Usage:
881 toplevelprog echoprog [flags] <command>
882
883The echoprog commands are:
884 echo Print strings on stdout
885 echoopt Print strings on stdout, with opts
Todd Wanga3ce2be2014-10-05 12:10:15 -0700886 help Display help for commands or topics
887Run "toplevelprog echoprog help [command]" for command usage.
888
889The echoprog additional help topics are:
890 topic3 Help topic 3 short
891Run "toplevelprog echoprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700892
893The echoprog flags are:
894 -extra=false: Print an extra arg
895
896The global flags are:
897 -global1=: global test flag 1
898 -global2=0: global test flag 2
899================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700900Toplevelprog Echoprog Echo
901
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700902Echo prints any strings passed in to stdout.
903
904Usage:
905 toplevelprog echoprog echo [strings]
906
907[strings] are arbitrary strings that will be echoed.
908================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700909Toplevelprog Echoprog Echoopt
910
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700911Echoopt prints any args passed in to stdout.
912
913Usage:
914 toplevelprog echoprog echoopt [flags] [args]
915
916[args] are arbitrary strings that will be echoed.
917
918The echoopt flags are:
919 -n=false: Do not output trailing newline
920================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -0700921Toplevelprog Echoprog Help
922
923Help with no args displays the usage of the parent command.
924Help with args displays the usage of the specified sub-command or help topic.
925"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700926
927Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -0700928 toplevelprog echoprog help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700929
Todd Wanga3ce2be2014-10-05 12:10:15 -0700930[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700931
932The help flags are:
933 -style=text: The formatting style for help output, either "text" or "godoc".
934================================================================================
Todd Wang0d12d712014-10-06 17:25:41 -0700935Toplevelprog Echoprog Topic3 - help topic
Todd Wanga3ce2be2014-10-05 12:10:15 -0700936
937Help topic 3 long.
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700938`,
939 },
940 {
941 Args: []string{"echoprog", "help", "echoopt"},
942 Stdout: `Echoopt prints any args passed in to stdout.
943
944Usage:
945 toplevelprog echoprog echoopt [flags] [args]
946
947[args] are arbitrary strings that will be echoed.
948
949The echoopt flags are:
950 -n=false: Do not output trailing newline
951
952The global flags are:
953 -global1=: global test flag 1
954 -global2=0: global test flag 2
955`,
956 },
957 {
Todd Wanga3ce2be2014-10-05 12:10:15 -0700958 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 Simsa5293dcb2014-05-10 09:56:38 -0700968 Args: []string{"help", "hello"},
969 Stdout: `Hello prints any strings passed in to stdout preceded by "Hello".
970
971Usage:
972 toplevelprog hello [strings]
973
974[strings] are arbitrary strings that will be printed.
975
976The 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 Wanga3ce2be2014-10-05 12:10:15 -0700984 Stderr: `ERROR: toplevelprog: unknown command or topic "foo"
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700985
986Toplevelprog has the echo subprogram and the hello command.
987
988Usage:
989 toplevelprog [flags] <command>
990
991The toplevelprog commands are:
992 echoprog Set of echo commands
993 hello Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -0700994 help Display help for commands or topics
995Run "toplevelprog help [command]" for command usage.
996
997The toplevelprog additional help topics are:
998 topic1 Help topic 1 short
999 topic2 Help topic 2 short
1000Run "toplevelprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001001
1002The toplevelprog flags are:
1003 -tlextra=false: Print an extra arg for all commands
1004
1005The 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 Wanga3ce2be2014-10-05 12:10:15 -07001058
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001059Hello prints any strings passed in to stdout preceded by "Hello".
1060
1061Usage:
1062 toplevelprog hello [strings]
1063
1064[strings] are arbitrary strings that will be printed.
1065
1066The 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 Wanga3ce2be2014-10-05 12:10:15 -07001075
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001076Toplevelprog has the echo subprogram and the hello command.
1077
1078Usage:
1079 toplevelprog [flags] <command>
1080
1081The toplevelprog commands are:
1082 echoprog Set of echo commands
1083 hello Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -07001084 help Display help for commands or topics
1085Run "toplevelprog help [command]" for command usage.
1086
1087The toplevelprog additional help topics are:
1088 topic1 Help topic 1 short
1089 topic2 Help topic 2 short
1090Run "toplevelprog help [topic]" for topic details.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001091
1092The toplevelprog flags are:
1093 -tlextra=false: Print an extra arg for all commands
1094
1095The 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
1104func TestMultiLevelCommandsOrdering(t *testing.T) {
1105 cmdHello11 := &Command{
1106 Name: "hello11",
1107 Short: "Print strings on stdout preceded by \"Hello\"",
1108 Long: `
1109Hello 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: `
1119Hello 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: `
1129Hello 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: `
1139Hello 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: `
1149Hello 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: `
1159Hello 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
1190Prog1 has two variants of hello and a subprogram prog2.
1191
1192Usage:
1193 prog1 <command>
1194
1195The 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 Wanga3ce2be2014-10-05 12:10:15 -07001199 help Display help for commands or topics
1200Run "prog1 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001201
1202The 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
1211Usage:
1212 prog1 <command>
1213
1214The 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 Wanga3ce2be2014-10-05 12:10:15 -07001218 help Display help for commands or topics
1219Run "prog1 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001220
1221The 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
1230Usage:
1231 prog1 <command>
1232
1233The 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 Wanga3ce2be2014-10-05 12:10:15 -07001237 help Display help for commands or topics
1238Run "prog1 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001239
1240The global flags are:
1241 -global1=: global test flag 1
1242 -global2=0: global test flag 2
1243================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001244Prog1 Hello11
1245
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001246Hello prints any strings passed in to stdout preceded by "Hello".
1247
1248Usage:
1249 prog1 hello11 [strings]
1250
1251[strings] are arbitrary strings that will be printed.
1252================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001253Prog1 Hello12
1254
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001255Hello prints any strings passed in to stdout preceded by "Hello".
1256
1257Usage:
1258 prog1 hello12 [strings]
1259
1260[strings] are arbitrary strings that will be printed.
1261================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001262Prog1 Prog2
1263
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001264Prog2 has two variants of hello and a subprogram prog3.
1265
1266Usage:
1267 prog1 prog2 <command>
1268
1269The 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 Simsa5293dcb2014-05-10 09:56:38 -07001273================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001274Prog1 Prog2 Hello21
1275
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001276Hello prints any strings passed in to stdout preceded by "Hello".
1277
1278Usage:
1279 prog1 prog2 hello21 [strings]
1280
1281[strings] are arbitrary strings that will be printed.
1282================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001283Prog1 Prog2 Prog3
1284
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001285Prog3 has two variants of hello.
1286
1287Usage:
1288 prog1 prog2 prog3 <command>
1289
1290The prog3 commands are:
1291 hello31 Print strings on stdout preceded by "Hello"
1292 hello32 Print strings on stdout preceded by "Hello"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001293================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001294Prog1 Prog2 Prog3 Hello31
1295
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001296Hello prints any strings passed in to stdout preceded by "Hello".
1297
1298Usage:
1299 prog1 prog2 prog3 hello31 [strings]
1300
1301[strings] are arbitrary strings that will be printed.
1302================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001303Prog1 Prog2 Prog3 Hello32
1304
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001305Hello prints any strings passed in to stdout preceded by "Hello".
1306
1307Usage:
1308 prog1 prog2 prog3 hello32 [strings]
1309
1310[strings] are arbitrary strings that will be printed.
1311================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001312Prog1 Prog2 Hello22
1313
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001314Hello prints any strings passed in to stdout preceded by "Hello".
1315
1316Usage:
1317 prog1 prog2 hello22 [strings]
1318
1319[strings] are arbitrary strings that will be printed.
1320================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001321Prog1 Help
1322
1323Help with no args displays the usage of the parent command.
1324Help with args displays the usage of the specified sub-command or help topic.
1325"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001326
1327Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001328 prog1 help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001329
Todd Wanga3ce2be2014-10-05 12:10:15 -07001330[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001331
1332The help flags are:
1333 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001334`,
1335 },
1336 {
1337 Args: []string{"prog2", "help", "..."},
1338 Stdout: `Prog2 has two variants of hello and a subprogram prog3.
1339
1340Usage:
1341 prog1 prog2 <command>
1342
1343The 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 Wanga3ce2be2014-10-05 12:10:15 -07001347 help Display help for commands or topics
1348Run "prog1 prog2 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001349
1350The global flags are:
1351 -global1=: global test flag 1
1352 -global2=0: global test flag 2
1353================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001354Prog1 Prog2 Hello21
1355
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001356Hello prints any strings passed in to stdout preceded by "Hello".
1357
1358Usage:
1359 prog1 prog2 hello21 [strings]
1360
1361[strings] are arbitrary strings that will be printed.
1362================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001363Prog1 Prog2 Prog3
1364
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001365Prog3 has two variants of hello.
1366
1367Usage:
1368 prog1 prog2 prog3 <command>
1369
1370The prog3 commands are:
1371 hello31 Print strings on stdout preceded by "Hello"
1372 hello32 Print strings on stdout preceded by "Hello"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001373================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001374Prog1 Prog2 Prog3 Hello31
1375
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001376Hello prints any strings passed in to stdout preceded by "Hello".
1377
1378Usage:
1379 prog1 prog2 prog3 hello31 [strings]
1380
1381[strings] are arbitrary strings that will be printed.
1382================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001383Prog1 Prog2 Prog3 Hello32
1384
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001385Hello prints any strings passed in to stdout preceded by "Hello".
1386
1387Usage:
1388 prog1 prog2 prog3 hello32 [strings]
1389
1390[strings] are arbitrary strings that will be printed.
1391================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001392Prog1 Prog2 Hello22
1393
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001394Hello prints any strings passed in to stdout preceded by "Hello".
1395
1396Usage:
1397 prog1 prog2 hello22 [strings]
1398
1399[strings] are arbitrary strings that will be printed.
1400================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001401Prog1 Prog2 Help
1402
1403Help with no args displays the usage of the parent command.
1404Help with args displays the usage of the specified sub-command or help topic.
1405"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001406
1407Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001408 prog1 prog2 help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001409
Todd Wanga3ce2be2014-10-05 12:10:15 -07001410[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001411
1412The help flags are:
1413 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001414`,
1415 },
1416 {
1417 Args: []string{"prog2", "prog3", "help", "..."},
1418 Stdout: `Prog3 has two variants of hello.
1419
1420Usage:
1421 prog1 prog2 prog3 <command>
1422
1423The prog3 commands are:
1424 hello31 Print strings on stdout preceded by "Hello"
1425 hello32 Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -07001426 help Display help for commands or topics
1427Run "prog1 prog2 prog3 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001428
1429The global flags are:
1430 -global1=: global test flag 1
1431 -global2=0: global test flag 2
1432================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001433Prog1 Prog2 Prog3 Hello31
1434
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001435Hello prints any strings passed in to stdout preceded by "Hello".
1436
1437Usage:
1438 prog1 prog2 prog3 hello31 [strings]
1439
1440[strings] are arbitrary strings that will be printed.
1441================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001442Prog1 Prog2 Prog3 Hello32
1443
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001444Hello prints any strings passed in to stdout preceded by "Hello".
1445
1446Usage:
1447 prog1 prog2 prog3 hello32 [strings]
1448
1449[strings] are arbitrary strings that will be printed.
1450================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001451Prog1 Prog2 Prog3 Help
1452
1453Help with no args displays the usage of the parent command.
1454Help with args displays the usage of the specified sub-command or help topic.
1455"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001456
1457Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001458 prog1 prog2 prog3 help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001459
Todd Wanga3ce2be2014-10-05 12:10:15 -07001460[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001461
1462The help flags are:
1463 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001464`,
1465 },
1466 {
1467 Args: []string{"help", "prog2", "prog3", "..."},
1468 Stdout: `Prog3 has two variants of hello.
1469
1470Usage:
1471 prog1 prog2 prog3 <command>
1472
1473The prog3 commands are:
1474 hello31 Print strings on stdout preceded by "Hello"
1475 hello32 Print strings on stdout preceded by "Hello"
Todd Wanga3ce2be2014-10-05 12:10:15 -07001476 help Display help for commands or topics
1477Run "prog1 prog2 prog3 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001478
1479The global flags are:
1480 -global1=: global test flag 1
1481 -global2=0: global test flag 2
1482================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001483Prog1 Prog2 Prog3 Hello31
1484
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001485Hello prints any strings passed in to stdout preceded by "Hello".
1486
1487Usage:
1488 prog1 prog2 prog3 hello31 [strings]
1489
1490[strings] are arbitrary strings that will be printed.
1491================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001492Prog1 Prog2 Prog3 Hello32
1493
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001494Hello prints any strings passed in to stdout preceded by "Hello".
1495
1496Usage:
1497 prog1 prog2 prog3 hello32 [strings]
1498
1499[strings] are arbitrary strings that will be printed.
1500================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001501Prog1 Prog2 Prog3 Help
1502
1503Help with no args displays the usage of the parent command.
1504Help with args displays the usage of the specified sub-command or help topic.
1505"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001506
1507Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001508 prog1 prog2 prog3 help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001509
Todd Wanga3ce2be2014-10-05 12:10:15 -07001510[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001511
1512The help flags are:
1513 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001514`,
1515 },
1516 {
1517 Args: []string{"help", "-style=godoc", "..."},
1518 Stdout: `Prog1 has two variants of hello and a subprogram prog2.
1519
1520Usage:
1521 prog1 <command>
1522
1523The 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 Wanga3ce2be2014-10-05 12:10:15 -07001527 help Display help for commands or topics
1528Run "prog1 help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001529
1530The global flags are:
1531 -global1=: global test flag 1
1532 -global2=0: global test flag 2
1533
1534Prog1 Hello11
1535
1536Hello prints any strings passed in to stdout preceded by "Hello".
1537
1538Usage:
1539 prog1 hello11 [strings]
1540
1541[strings] are arbitrary strings that will be printed.
1542
1543Prog1 Hello12
1544
1545Hello prints any strings passed in to stdout preceded by "Hello".
1546
1547Usage:
1548 prog1 hello12 [strings]
1549
1550[strings] are arbitrary strings that will be printed.
1551
1552Prog1 Prog2
1553
1554Prog2 has two variants of hello and a subprogram prog3.
1555
1556Usage:
1557 prog1 prog2 <command>
1558
1559The 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 Simsa5293dcb2014-05-10 09:56:38 -07001563
1564Prog1 Prog2 Hello21
1565
1566Hello prints any strings passed in to stdout preceded by "Hello".
1567
1568Usage:
1569 prog1 prog2 hello21 [strings]
1570
1571[strings] are arbitrary strings that will be printed.
1572
1573Prog1 Prog2 Prog3
1574
1575Prog3 has two variants of hello.
1576
1577Usage:
1578 prog1 prog2 prog3 <command>
1579
1580The prog3 commands are:
1581 hello31 Print strings on stdout preceded by "Hello"
1582 hello32 Print strings on stdout preceded by "Hello"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001583
1584Prog1 Prog2 Prog3 Hello31
1585
1586Hello prints any strings passed in to stdout preceded by "Hello".
1587
1588Usage:
1589 prog1 prog2 prog3 hello31 [strings]
1590
1591[strings] are arbitrary strings that will be printed.
1592
1593Prog1 Prog2 Prog3 Hello32
1594
1595Hello prints any strings passed in to stdout preceded by "Hello".
1596
1597Usage:
1598 prog1 prog2 prog3 hello32 [strings]
1599
1600[strings] are arbitrary strings that will be printed.
1601
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001602Prog1 Prog2 Hello22
1603
1604Hello prints any strings passed in to stdout preceded by "Hello".
1605
1606Usage:
1607 prog1 prog2 hello22 [strings]
1608
1609[strings] are arbitrary strings that will be printed.
1610
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001611Prog1 Help
1612
Todd Wanga3ce2be2014-10-05 12:10:15 -07001613Help with no args displays the usage of the parent command.
1614Help with args displays the usage of the specified sub-command or help topic.
1615"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001616
1617Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001618 prog1 help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001619
Todd Wanga3ce2be2014-10-05 12:10:15 -07001620[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001621
1622The help flags are:
1623 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001624`,
1625 },
1626 }
1627
1628 runTestCases(t, progHello1, tests)
1629}
1630
1631func TestCommandAndArgs(t *testing.T) {
1632 cmdEcho := &Command{
1633 Name: "echo",
1634 Short: "Print strings on stdout",
1635 Long: `
1636Echo 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
1666Usage:
1667 cmdargs <command>
1668 cmdargs [strings]
1669
1670The cmdargs commands are:
1671 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001672 help Display help for commands or topics
1673Run "cmdargs help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001674
1675[strings] are arbitrary strings that will be printed.
1676
1677The 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
1686Usage:
1687 cmdargs echo [strings]
1688
1689[strings] are arbitrary strings that will be echoed.
1690
1691The 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
1700Usage:
1701 cmdargs <command>
1702 cmdargs [strings]
1703
1704The cmdargs commands are:
1705 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001706 help Display help for commands or topics
1707Run "cmdargs help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001708
1709[strings] are arbitrary strings that will be printed.
1710
1711The global flags are:
1712 -global1=: global test flag 1
1713 -global2=0: global test flag 2
1714================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001715Cmdargs Echo
1716
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001717Echo prints any strings passed in to stdout.
1718
1719Usage:
1720 cmdargs echo [strings]
1721
1722[strings] are arbitrary strings that will be echoed.
1723================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001724Cmdargs Help
1725
1726Help with no args displays the usage of the parent command.
1727Help with args displays the usage of the specified sub-command or help topic.
1728"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001729
1730Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001731 cmdargs help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001732
Todd Wanga3ce2be2014-10-05 12:10:15 -07001733[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001734
1735The help flags are:
1736 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001737`,
1738 },
1739 {
1740 Args: []string{"help", "foo"},
1741 Err: ErrUsage,
Todd Wanga3ce2be2014-10-05 12:10:15 -07001742 Stderr: `ERROR: cmdargs: unknown command or topic "foo"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001743
1744Cmdargs has the echo command and a Run function with args.
1745
1746Usage:
1747 cmdargs <command>
1748 cmdargs [strings]
1749
1750The cmdargs commands are:
1751 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001752 help Display help for commands or topics
1753Run "cmdargs help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001754
1755[strings] are arbitrary strings that will be printed.
1756
1757The 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
1775Echo prints any strings passed in to stdout.
1776
1777Usage:
1778 cmdargs echo [strings]
1779
1780[strings] are arbitrary strings that will be echoed.
1781
1782The 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
1791func TestCommandAndRunNoArgs(t *testing.T) {
1792 cmdEcho := &Command{
1793 Name: "echo",
1794 Short: "Print strings on stdout",
1795 Long: `
1796Echo 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
1821Cmdrun has the echo command and a Run function with no args.
1822
1823Usage:
1824 cmdrun <command>
1825 cmdrun
1826
1827The cmdrun commands are:
1828 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001829 help Display help for commands or topics
1830Run "cmdrun help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001831
1832The 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
1841Usage:
1842 cmdrun <command>
1843 cmdrun
1844
1845The cmdrun commands are:
1846 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001847 help Display help for commands or topics
1848Run "cmdrun help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001849
1850The 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
1859Usage:
1860 cmdrun echo [strings]
1861
1862[strings] are arbitrary strings that will be echoed.
1863
1864The 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
1873Usage:
1874 cmdrun <command>
1875 cmdrun
1876
1877The cmdrun commands are:
1878 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001879 help Display help for commands or topics
1880Run "cmdrun help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001881
1882The global flags are:
1883 -global1=: global test flag 1
1884 -global2=0: global test flag 2
1885================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001886Cmdrun Echo
1887
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001888Echo prints any strings passed in to stdout.
1889
1890Usage:
1891 cmdrun echo [strings]
1892
1893[strings] are arbitrary strings that will be echoed.
1894================================================================================
Todd Wanga3ce2be2014-10-05 12:10:15 -07001895Cmdrun Help
1896
1897Help with no args displays the usage of the parent command.
1898Help with args displays the usage of the specified sub-command or help topic.
1899"help ..." recursively displays help for all commands and topics.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001900
1901Usage:
Todd Wanga3ce2be2014-10-05 12:10:15 -07001902 cmdrun help [flags] [command/topic ...]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001903
Todd Wanga3ce2be2014-10-05 12:10:15 -07001904[command/topic ...] optionally identifies a specific sub-command or help topic.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001905
1906The help flags are:
1907 -style=text: The formatting style for help output, either "text" or "godoc".
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001908`,
1909 },
1910 {
1911 Args: []string{"help", "foo"},
1912 Err: ErrUsage,
Todd Wanga3ce2be2014-10-05 12:10:15 -07001913 Stderr: `ERROR: cmdrun: unknown command or topic "foo"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001914
1915Cmdrun has the echo command and a Run function with no args.
1916
1917Usage:
1918 cmdrun <command>
1919 cmdrun
1920
1921The cmdrun commands are:
1922 echo Print strings on stdout
Todd Wanga3ce2be2014-10-05 12:10:15 -07001923 help Display help for commands or topics
1924Run "cmdrun help [command]" for command usage.
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001925
1926The 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
1944Echo prints any strings passed in to stdout.
1945
1946Usage:
1947 cmdrun echo [strings]
1948
1949[strings] are arbitrary strings that will be echoed.
1950
1951The 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 Shankarfb5f1c12014-10-15 03:37:38 -07001959
1960func 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
1984Usage:
1985 program <command>
1986
1987The program commands are:
1988 x description of short command.
1989 thisisaverylongcommand description of very long command.
1990 help Display help for commands or topics
1991Run "program help [command]" for command usage.
1992
1993The 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}