Skip to content
Myr

Add docs, tests, and bit of rearrainging...

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Eric Paris · committed by Eric Paris · parent fbce60c · Browse files · 3 files+126 added−10 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+126 added−10 removed

  • other:+126 added−10 removed

3 files+126 added−10 removed

modified:cobra_test.go+42 added
11 unmodified lines
12var _ = fmt.Println13var _ = os.Stderr1415var tp, te, tt, t1 []string16var rootPersPre, echoPre, echoPersPre, timesPersPre []string17var flagb1, flagb2, flagb3, flagbr, flagbp bool18var flags1, flags2a, flags2b, flags3 string19var flagi1, flagi2, flagi3, flagir int20var globalFlag1 bool
17 unmodified lines
38 Aliases: []string{"say"},39 Short: "Echo anything to the screen",40 Long: `an utterly useless command for testing.`,41 Example: "Just run cobra-test echo",42 PersistentPreRun: func(cmd *Command, args []string) {43 echoPersPre = args44 },45 PreRun: func(cmd *Command, args []string) {46 echoPre = args47 },48 Run: func(cmd *Command, args []string) {49 te = args50 },51}
18 unmodified lines
70var cmdTimes = &Command{71 Use: "times [# times] [string to echo]",72 Short: "Echo anything to the screen more times",73 Long: `a slightly useless command for testing.`,74 PersistentPreRun: func(cmd *Command, args []string) {75 timesPersPre = args76 },77 Run: func(cmd *Command, args []string) {78 tt = args79 },80}
1 unmodified line
82var cmdRootNoRun = &Command{83 Use: "cobra-test",84 Short: "The root can run it's own function",85 Long: "The root description for help",86 PersistentPreRun: func(cmd *Command, args []string) {87 rootPersPre = args88 },89}9091var cmdRootSameName = &Command{92 Use: "print",
68 unmodified lines
161}162163func initialize() *Command {164 tt, tp, te = nil, nil, nil165 rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil166167 var c = cmdRootNoRun168 flagInit()169 commandInit()170 return c171}172173func initializeWithSameName() *Command {174 tt, tp, te = nil, nil, nil175 rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil176 var c = cmdRootSameName177 flagInit()178 commandInit()179 return c
662 unmodified lines
842 c := fullSetupTest("deprecated")843844 checkResultContains(t, c, cmdDeprecated.Deprecated)845}846847func TestPreRun(t *testing.T) {848 noRRSetupTest("echo one two")849 if echoPre == nil || echoPersPre == nil {850 t.Error("PreRun or PersistentPreRun not called")851 }852 if rootPersPre != nil || timesPersPre != nil {853 t.Error("Wrong *Pre functions called!")854 }855856 noRRSetupTest("echo times one two")857 if timesPersPre == nil {858 t.Error("PreRun or PersistentPreRun not called")859 }860 if echoPre != nil || echoPersPre != nil || rootPersPre != nil {861 t.Error("Wrong *Pre functions called!")862 }863864 noRRSetupTest("print one two")865 if rootPersPre == nil {866 t.Error("Parent PersistentPreRun not called but should not have been")867 }868 if echoPre != nil || echoPersPre != nil || timesPersPre != nil {869 t.Error("Wrong *Pre functions called!")870 }871}
modified:command.go+14 added-10 removed
54 unmodified lines
55 // Set of flags childrens of this command will inherit56 pflags *flag.FlagSet57 // Flags that are declared specifically by this command (not inherited).58 lflags *flag.FlagSet59 // Run runs the command.60 // The args are the arguments after the command name.61 Run func(cmd *Command, args []string)62 // PreRun runs the command after the flags are parsed and before run.63 // The args are the arguments after the command name.59 // The *Run functions are executed in the following order:60 // * PersistentPreRun()61 // * PreRun()62 // * Run()63 // * PostRun()64 // * PersistentPostRun()65 // All functions get the same args, the arguments after the command name66 // PersistentPreRun: children of this command will inherit and execute67 PersistentPreRun func(cmd *Command, args []string)68 // PreRun: children of this command will not inherit.69 PreRun func(cmd *Command, args []string)65 // PostRun runs the command after run.66 // The args are the arguments after the command name.70 // Run: Typically the actual work function. Most commands will only implement this71 Run func(cmd *Command, args []string)72 // PostRun: run after the Run command.73 PostRun func(cmd *Command, args []string)68 // PreRun which children of this command will inherit.69 PersistentPreRun func(cmd *Command, args []string)70 // PostRun which children of this command will inherit.74 // PersistentPostRun: children of this command will inherit and execute after PostRun75 PersistentPostRun func(cmd *Command, args []string)76 // Commands is the list of commands supported by this program.77 commands []*Command78 // Parent Command for this command
927 unmodified lines
modified:README.md+70 added
346 unmodified lines
347 command.SetUsageFunc(f func(*Command) error)348349 command.SetUsageTemplate(s string)350351## PreRun or PostRun Hooks352353It is possible to run functions before or after the main `Run` function of your command. The `PersistentPreRun` and `PreRun` functions will be executed before `Run`. `PersistendPostRun` and `PostRun` will be executed after `Run`. The `Persistent*Run` functions will be inherrited by children if they do not declare their own. These function are run in the following order:354355- `PersistentPreRun`356- `PreRun`357- `Run`358- `PostRun`359- `PersistenPostRun`360361And example of two commands which use all of these features is below. When the subcommand in executed it will run the root command's `PersistentPreRun` but not the root command's `PersistentPostRun`362363```go364package main365366import (367 "fmt"368369 "github.com/spf13/cobra"370)371372func main() {373374 var rootCmd = &cobra.Command{375 Use: "root [sub]",376 Short: "My root command",377 PersistentPreRun: func(cmd *cobra.Command, args []string) {378 fmt.Printf("Inside rootCmd PersistentPreRun with args: %v\n", args)379 },380 PreRun: func(cmd *cobra.Command, args []string) {381 fmt.Printf("Inside rootCmd PreRun with args: %v\n", args)382 },383 Run: func(cmd *cobra.Command, args []string) {384 fmt.Printf("Inside rootCmd Run with args: %v\n", args)385 },386 PostRun: func(cmd *cobra.Command, args []string) {387 fmt.Printf("Inside rootCmd PostRun with args: %v\n", args)388 },389 PersistentPostRun: func(cmd *cobra.Command, args []string) {390 fmt.Printf("Inside rootCmd PersistentPostRun with args: %v\n", args)391 },392 }393394 var subCmd = &cobra.Command{395 Use: "sub [no options!]",396 Short: "My sub command",397 PreRun: func(cmd *cobra.Command, args []string) {398 fmt.Printf("Inside subCmd PreRun with args: %v\n", args)399 },400 Run: func(cmd *cobra.Command, args []string) {401 fmt.Printf("Inside subCmd Run with args: %v\n", args)402 },403 PostRun: func(cmd *cobra.Command, args []string) {404 fmt.Printf("Inside subCmd PostRun with args: %v\n", args)405 },406 PersistentPostRun: func(cmd *cobra.Command, args []string) {407 fmt.Printf("Inside subCmd PersistentPostRun with args: %v\n", args)408 },409 }410411 rootCmd.AddCommand(subCmd)412413 rootCmd.SetArgs([]string{""})414 _ = rootCmd.Execute()415 fmt.Print("\n")416 rootCmd.SetArgs([]string{"sub", "arg1", "arg2"})417 _ = rootCmd.Execute()418}419```420421## Generating markdown formatted documentation for your command422423Cobra can generate a markdown formatted document based on the subcommands, flags, etc. A simple example of how to do this for your command can be found in [Markdown Docs](md_docs.md)424
61 unmodified lines