Skip to content
Myr

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

3 files+76 added−13 removed

modified:cobra_test.go+56 added-7 removed
112 unmodified lines
113 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}121122type resulter struct {123 Error error124 Output string125 Command *Command126}127128func fullSetupTest(input string) resulter {129 c := initializeWithRootCmd()130131 return fullTester(c, input)132}133134func noRRSetupTest(input string) resulter {135 c := initialize()136137 return fullTester(c, input)138}139140func 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, " "))147148 err := c.Execute()149 output := buf.String()150151 return resulter{err, output, c}152}153154func 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}159160func checkOutputContains(t *testing.T, c *Command, check string) {161 buf := new(bytes.Buffer)162 c.SetOutput(buf)163 c.Execute()
321 unmodified lines
485 }486}487488func 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")490491 checkResultContains(t, x, "Available Commands:")492493 if strings.Contains(x.Output, "unknown flag: --help") {494 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)495 }496497 x = fullSetupTest("echo --help")498499 checkResultContains(t, x, "Available Commands:")500501 if strings.Contains(x.Output, "unknown flag: --help") {502 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)503 }504505}506507 checkOutputContains(t, c, "Available Commands:")508}
modified:command.go+15 added-1 removed
61 unmodified lines
62 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}6869// os.Args[1:] by default, if desired, can be overridden70// particularly useful when testing.
230 unmodified lines
301302 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 }310311 argWoFlags := c.Flags().Args()312 c.Run(c, argWoFlags)313 return nil314 }
40 unmodified lines
355 fmt.Println(e)356 c.Usage()357 return e358 } else {359360 // If help is called, regardless of other flags, we print that361 if c.helpFlagVal {362 c.Help()363 return nil364 }365366 argWoFlags := c.Flags().Args()367 if len(argWoFlags) > 0 {368 c.Usage()369 } else {
198 unmodified lines
568 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 lines
631632// Parses persistent flag tree & local flags633func (c *Command) ParseFlags(args []string) (err error) {634 c.mergePersistentFlags()620635 err = c.Flags().Parse(args)636637 // The upstream library adds spaces to the error638 // response regardless of success.
31 unmodified lines
modified:README.md+5 added-5 removed
296 unmodified lines
297 hugo [flags]298 hugo [command]299300 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 command306307 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 draft
67 unmodified lines