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.
|
Cobra doesn't require any special constructors. Simply create your commands.
|
||||||
|
|
||||||
var HugoCmd = &cobra.Command{
|
```go
|
||||||
|
var HugoCmd = &cobra.Command{
|
||||||
Use: "hugo",
|
Use: "hugo",
|
||||||
Short: "Hugo is a very fast static site generator",
|
Short: "Hugo is a very fast static site generator",
|
||||||
Long: `A Fast and Flexible Static Site Generator built with
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Do Stuff Here
|
// Do Stuff Here
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Create additional commands
|
### Create additional commands
|
||||||
|
|
||||||
Additional commands can be defined.
|
Additional commands can be defined.
|
||||||
|
|
||||||
var versionCmd = &cobra.Command{
|
```go
|
||||||
|
var versionCmd = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print the version number of Hugo",
|
Short: "Print the version number of Hugo",
|
||||||
Long: `All software has versions. This is Hugo's`,
|
Long: `All software has versions. This is Hugo's`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
|
fmt.Println("Hugo Static Site Generator v0.9 -- HEAD")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Attach command to its parent
|
### Attach command to its parent
|
||||||
In this example we are attaching it to the root, but commands can be attached at any level.
|
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.
|
We have only defined one flag for a single command.
|
||||||
|
|
||||||
More documentation about flags is available at https://github.com/spf13/pflag
|
More documentation about flags is available at https://github.com/spf13/pflag
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import(
|
import (
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
var echoTimes int
|
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
|
Long: `echo things multiple times back to the user by providing
|
||||||
a count and a string.`,
|
a count and a string.`,
|
||||||
Run: func(cmd *cobra.Command, args []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, " "))
|
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)
|
rootCmd.AddCommand(cmdPrint, cmdEcho)
|
||||||
cmdEcho.AddCommand(cmdTimes)
|
cmdEcho.AddCommand(cmdTimes)
|
||||||
rootCmd.Execute()
|
rootCmd.Execute()
|
||||||
}
|
}
|
||||||
|
```
|
||||||
For a more complete example of a larger application, please checkout [Hugo](http://hugo.spf13.com)
|
For a more complete example of a larger application, please checkout [Hugo](http://hugo.spf13.com)
|
||||||
|
|
||||||
## The Help Command
|
## 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
|
The default help command is
|
||||||
|
|
||||||
func (c *Command) initHelp() {
|
```go
|
||||||
|
func (c *Command) initHelp() {
|
||||||
if c.helpCommand == nil {
|
if c.helpCommand == nil {
|
||||||
c.helpCommand = &Command{
|
c.helpCommand = &Command{
|
||||||
Use: "help [command]",
|
Use: "help [command]",
|
||||||
@ -319,7 +327,8 @@ The default help command is
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.AddCommand(c.helpCommand)
|
c.AddCommand(c.helpCommand)
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
You can provide your own command, function or template through the following methods.
|
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:**
|
**Example Usage using RunE:**
|
||||||
|
|
||||||
```go
|
```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
|
## Suggestions when "unknown command" happens
|
||||||
|
Loading…
Reference in New Issue
Block a user