Adding support for --help
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by spf13 · committed by spf13 · parent 3e874b3 · Browse files · 3 files·+76 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+76 added−13 removed
- other:+76 added−13 removed
Provenance
- Commit
07be814- Tree
a75eed9- Author
spf13 <steve.francia@gmail.com>- Committer
spf13 <steve.francia@gmail.com>- Parent
3e874b3
3 files changed+76 added−13 removed
modified:cobra_test.go+56 added-7 removed
112 unmodified lines113 cmdRootWithRun.ResetCommands()114 tt, tp, te, rootcalled = nil, nil, nil, false115 flagInit()116 cmdRootWithRun.Flags().BoolVarP(&flagbr, "boolroot", "b", false, "help message for flag boolroot")117- cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag intthree")117+ cmdRootWithRun.Flags().IntVarP(&flagir, "introot", "i", 321, "help message for flag introot")118 commandInit()119 return cmdRootWithRun120 }121 122+type resulter struct {123+ Error error124+ Output string125+ Command *Command126+}127+128+func fullSetupTest(input string) resulter {129+ c := initializeWithRootCmd()130+131+ return fullTester(c, input)132+}133+134+func noRRSetupTest(input string) resulter {135+ c := initialize()136+137+ return fullTester(c, input)138+}139+140+func fullTester(c *Command, input string) resulter {141+ buf := new(bytes.Buffer)142+ // Testing flag with invalid input143+ c.SetOutput(buf)144+ cmdEcho.AddCommand(cmdTimes)145+ c.AddCommand(cmdPrint, cmdEcho)146+ c.SetArgs(strings.Split(input, " "))147+148+ err := c.Execute()149+ output := buf.String()150+151+ return resulter{err, output, c}152+}153+154+func checkResultContains(t *testing.T, x resulter, check string) {155+ if !strings.Contains(x.Output, check) {156+ t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, x.Output)157+ }158+}159+160 func checkOutputContains(t *testing.T, c *Command, check string) {161 buf := new(bytes.Buffer)162 c.SetOutput(buf)163 c.Execute()321 unmodified lines485 }486 }487 488 func TestRootHelp(t *testing.T) {451- fmt.Println("testing root help")452- c := initializeWithRootCmd()453- c.AddCommand(cmdPrint, cmdEcho)454- c.SetArgs(strings.Split("--help", " "))455- e := c.Execute()456- fmt.Println(e)489+ x := fullSetupTest("--help")490 491+ checkResultContains(t, x, "Available Commands:")492+493+ if strings.Contains(x.Output, "unknown flag: --help") {494+ t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)495+ }496+497+ x = fullSetupTest("echo --help")498+499+ checkResultContains(t, x, "Available Commands:")500+501+ if strings.Contains(x.Output, "unknown flag: --help") {502+ t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)503+ }504+505+}506+507 checkOutputContains(t, c, "Available Commands:")508 }modified:command.go+15 added-1 removed
61 unmodified lines62 usageTemplate string // Can be defined by Application63 helpTemplate string // Can be defined by Application64 helpFunc func(*Command, []string) // Help can be defined by application65 helpCommand *Command // The help command66+ helpFlagVal bool67 }68 69 // os.Args[1:] by default, if desired, can be overridden70 // particularly useful when testing.230 unmodified lines301 302 if err != nil {303 return err304 } else {305+ // If help is called, regardless of other flags, we print that306+ if c.helpFlagVal {307+ c.Help()308+ return nil309+ }310+311 argWoFlags := c.Flags().Args()312 c.Run(c, argWoFlags)313 return nil314 }40 unmodified lines355 fmt.Println(e)356 c.Usage()357 return e358 } else {359+360+ // If help is called, regardless of other flags, we print that361+ if c.helpFlagVal {362+ c.Help()363+ return nil364+ }365+366 argWoFlags := c.Flags().Args()367 if len(argWoFlags) > 0 {368 c.Usage()369 } else {198 unmodified lines568 if c.flagErrorBuf == nil {569 c.flagErrorBuf = new(bytes.Buffer)570 }571 c.flags.SetOutput(c.flagErrorBuf)572+ c.flags.BoolVar(&c.helpFlagVal, "help", false, "help for "+c.Name())573 }574 return c.flags575 }576 54 unmodified lines631 632 // Parses persistent flag tree & local flags633 func (c *Command) ParseFlags(args []string) (err error) {634 c.mergePersistentFlags()620-635 err = c.Flags().Parse(args)636 637 // The upstream library adds spaces to the error638 // response regardless of success.31 unmodified linesmodified:README.md+5 added-5 removed
296 unmodified lines297 hugo [flags]298 hugo [command]299 300 Available Commands:301- server :: Hugo runs it's own a webserver to render the files302- version :: Print the version number of Hugo303- check :: Check content in the source directory304- benchmark :: Benchmark hugo by building a site a number of times305- help [command] :: Help about any command301+ server Hugo runs it's own a webserver to render the files302+ version Print the version number of Hugo303+ check Check content in the source directory304+ benchmark Benchmark hugo by building a site a number of times305+ help [command] Help about any command306 307 Available Flags:308 -b, --base-url="": hostname (and path) to the root eg. http://spf13.com/309 -D, --build-drafts=false: include content marked as draft67 unmodified lines