forked from mirror/cobra
changes README to have go code ran through gofmt
This commit is contained in:
parent
1e1d5137bf
commit
d4c0084f5d
61
README.md
61
README.md
|
@ -119,7 +119,8 @@ The root command represents your binary itself.
|
|||
|
||||
Cobra doesn't require any special constructors. Simply create your commands.
|
||||
|
||||
var HugoCmd = &cobra.Command{
|
||||
```go
|
||||
var HugoCmd = &cobra.Command{
|
||||
Use: "hugo",
|
||||
Short: "Hugo is a very fast static site generator",
|
||||
Long: `A Fast and Flexible Static Site Generator built with
|
||||
|
@ -128,20 +129,23 @@ Cobra doesn't require any special constructors. Simply create your commands.
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// Do Stuff Here
|
||||
},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Create additional commands
|
||||
|
||||
Additional commands can be defined.
|
||||
|
||||
var versionCmd = &cobra.Command{
|
||||
```go
|
||||
var versionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Print the version number of Hugo",
|
||||
Long: `All software has versions. This is Hugo's`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
|
||||
},
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Attach command to its parent
|
||||
In this example we are attaching it to the root, but commands can be attached at any level.
|
||||
|
@ -198,14 +202,17 @@ by not providing a 'Run' for the 'rootCmd'.
|
|||
We have only defined one flag for a single command.
|
||||
|
||||
More documentation about flags is available at https://github.com/spf13/pflag
|
||||
```go
|
||||
package main
|
||||
|
||||
import(
|
||||
"github.com/spf13/cobra"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
var echoTimes int
|
||||
|
||||
|
@ -237,7 +244,7 @@ More documentation about flags is available at https://github.com/spf13/pflag
|
|||
Long: `echo things multiple times back to the user by providing
|
||||
a count and a string.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
for i:=0; i < echoTimes; i++ {
|
||||
for i := 0; i < echoTimes; i++ {
|
||||
fmt.Println("Echo: " + strings.Join(args, " "))
|
||||
}
|
||||
},
|
||||
|
@ -249,8 +256,8 @@ More documentation about flags is available at https://github.com/spf13/pflag
|
|||
rootCmd.AddCommand(cmdPrint, cmdEcho)
|
||||
cmdEcho.AddCommand(cmdTimes)
|
||||
rootCmd.Execute()
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
For a more complete example of a larger application, please checkout [Hugo](http://hugo.spf13.com)
|
||||
|
||||
## The Help Command
|
||||
|
@ -308,7 +315,8 @@ You can provide your own Help command or you own template for the default comman
|
|||
|
||||
The default help command is
|
||||
|
||||
func (c *Command) initHelp() {
|
||||
```go
|
||||
func (c *Command) initHelp() {
|
||||
if c.helpCommand == nil {
|
||||
c.helpCommand = &Command{
|
||||
Use: "help [command]",
|
||||
|
@ -319,7 +327,8 @@ The default help command is
|
|||
}
|
||||
}
|
||||
c.AddCommand(c.helpCommand)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can provide your own command, function or template through the following methods.
|
||||
|
||||
|
@ -462,32 +471,6 @@ Cobra also has functions where the return signature is an error. This allows for
|
|||
**Example Usage using RunE:**
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "hugo",
|
||||
Short: "Hugo is a very fast static site generator",
|
||||
Long: `A Fast and Flexible Static Site Generator built with
|
||||
love by spf13 and friends in Go.
|
||||
Complete documentation is available at http://hugo.spf13.com`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
// Do Stuff Here
|
||||
return errors.New("some random error")
|
||||
},
|
||||
}
|
||||
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Suggestions when "unknown command" happens
|
||||
|
|
Loading…
Reference in New Issue