Fix README.md
This commit is contained in:
parent
d870ad6ccb
commit
afbaca6ed7
39
README.md
39
README.md
@ -138,7 +138,6 @@ discovery. So a cli app can be as little as one line of code in `main()`.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -652,7 +651,7 @@ func main() {
|
|||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
|
|
||||||
app.Flags = []cli.Flag {
|
app.Flags = []cli.Flag {
|
||||||
cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "password, p",
|
Name: "password, p",
|
||||||
Usage: "password for the mysql database",
|
Usage: "password for the mysql database",
|
||||||
FilePath: "/etc/mysql/password",
|
FilePath: "/etc/mysql/password",
|
||||||
@ -754,6 +753,7 @@ For example this:
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -943,13 +943,12 @@ func main() {
|
|||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.BoolFlag{
|
&cli.BoolFlag{
|
||||||
Name: "ginger-crouton",
|
Name: "ginger-crouton",
|
||||||
Value: true,
|
|
||||||
Usage: "is it in the soup?",
|
Usage: "is it in the soup?",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Action: func(ctx *cli.Context) error {
|
Action: func(ctx *cli.Context) error {
|
||||||
if !ctx.Bool("ginger-crouton") {
|
if !ctx.Bool("ginger-crouton") {
|
||||||
return cli.Exit("it is not in the soup", 86)
|
return cli.Exit(Ginger croutons are not in the soup, 86)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@ -998,9 +997,9 @@ func main() {
|
|||||||
Name: "short",
|
Name: "short",
|
||||||
Usage: "complete a task on the list",
|
Usage: "complete a task on the list",
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.BoolFlag{Name: "serve, s"},
|
&cli.BoolFlag{Name: "serve", Aliases: []string{"s"}},
|
||||||
cli.BoolFlag{Name: "option, o"},
|
&cli.BoolFlag{Name: "option", Aliases: []string{"o"}},
|
||||||
cli.StringFlag{Name: "message, m"},
|
&cli.StringFlag{Name: "message", Aliases: []string{"m"}},
|
||||||
},
|
},
|
||||||
Action: func(c *cli.Context) error {
|
Action: func(c *cli.Context) error {
|
||||||
fmt.Println("serve:", c.Bool("serve"))
|
fmt.Println("serve:", c.Bool("serve"))
|
||||||
@ -1040,7 +1039,7 @@ show an app's subcommands, but you can write your own completion methods for
|
|||||||
the App or its subcommands.
|
the App or its subcommands.
|
||||||
|
|
||||||
<!-- {
|
<!-- {
|
||||||
"args": ["complete", "--generate-completion"],
|
"args": ["complete", "--generate-bash-completion"],
|
||||||
"output": "laundry"
|
"output": "laundry"
|
||||||
} -->
|
} -->
|
||||||
``` go
|
``` go
|
||||||
@ -1114,11 +1113,11 @@ to the name of their program (as above).
|
|||||||
|
|
||||||
#### Customization
|
#### Customization
|
||||||
|
|
||||||
The default shell completion flag (`--generate-completion`) is defined as
|
The default shell completion flag (`--generate-bash-completion`) is defined as
|
||||||
`cli.GenerateCompletionFlag`, and may be redefined if desired, e.g.:
|
`cli.EnableBashCompletion`, and may be redefined if desired, e.g.:
|
||||||
|
|
||||||
<!-- {
|
<!-- {
|
||||||
"args": ["--compgen"],
|
"args": ["--generate-bash-completion"],
|
||||||
"output": "wat\nhelp\nh"
|
"output": "wat\nhelp\nh"
|
||||||
} -->
|
} -->
|
||||||
``` go
|
``` go
|
||||||
@ -1132,13 +1131,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cli.GenerateCompletionFlag = &cli.BoolFlag{
|
|
||||||
Name: "compgen",
|
|
||||||
Hidden: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
app := &cli.App{
|
app := &cli.App{
|
||||||
EnableShellCompletion: true,
|
EnableBashCompletion: true,
|
||||||
Commands: []*cli.Command{
|
Commands: []*cli.Command{
|
||||||
{
|
{
|
||||||
Name: "wat",
|
Name: "wat",
|
||||||
@ -1174,7 +1168,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
@ -1233,7 +1226,6 @@ setting `cli.HelpFlag`, e.g.:
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -1269,7 +1261,6 @@ setting `cli.VersionFlag`, e.g.:
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -1300,7 +1291,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
@ -1352,7 +1342,6 @@ func init() {
|
|||||||
cli.SubcommandHelpTemplate += "\nor something\n"
|
cli.SubcommandHelpTemplate += "\nor something\n"
|
||||||
|
|
||||||
cli.HelpFlag = &cli.BoolFlag{Name: "halp"}
|
cli.HelpFlag = &cli.BoolFlag{Name: "halp"}
|
||||||
cli.GenerateCompletionFlag = &cli.BoolFlag{Name: "compgen", Hidden: true}
|
|
||||||
cli.VersionFlag = &cli.BoolFlag{Name: "print-version", Aliases: []string{"V"}}
|
cli.VersionFlag = &cli.BoolFlag{Name: "print-version", Aliases: []string{"V"}}
|
||||||
|
|
||||||
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
|
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
|
||||||
@ -1432,7 +1421,7 @@ func main() {
|
|||||||
HideHelp: false,
|
HideHelp: false,
|
||||||
Hidden: false,
|
Hidden: false,
|
||||||
HelpName: "doo!",
|
HelpName: "doo!",
|
||||||
ShellComplete: func(c *cli.Context) {
|
BashComplete: func(c *cli.Context) {
|
||||||
fmt.Fprintf(c.App.Writer, "--better\n")
|
fmt.Fprintf(c.App.Writer, "--better\n")
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: func(c *cli.Context) error {
|
||||||
@ -1475,10 +1464,10 @@ func main() {
|
|||||||
&cli.UintFlag{Name: "age"},
|
&cli.UintFlag{Name: "age"},
|
||||||
&cli.Uint64Flag{Name: "bigage"},
|
&cli.Uint64Flag{Name: "bigage"},
|
||||||
},
|
},
|
||||||
EnableShellCompletion: true,
|
EnableBashCompletion: true,
|
||||||
HideHelp: false,
|
HideHelp: false,
|
||||||
HideVersion: false,
|
HideVersion: false,
|
||||||
ShellComplete: func(c *cli.Context) {
|
BashComplete: func(c *cli.Context) {
|
||||||
fmt.Fprintf(c.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")
|
fmt.Fprintf(c.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")
|
||||||
},
|
},
|
||||||
Before: func(c *cli.Context) error {
|
Before: func(c *cli.Context) error {
|
||||||
|
@ -73,8 +73,8 @@ func NewExitError(message interface{}, exitCode int) ExitCoder {
|
|||||||
// HandleExitCoder
|
// HandleExitCoder
|
||||||
func Exit(message interface{}, exitCode int) ExitCoder {
|
func Exit(message interface{}, exitCode int) ExitCoder {
|
||||||
return &exitError{
|
return &exitError{
|
||||||
exitCode: exitCode,
|
|
||||||
message: message,
|
message: message,
|
||||||
|
exitCode: exitCode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,9 +98,9 @@ func HandleExitCoder(err error) {
|
|||||||
if exitErr, ok := err.(ExitCoder); ok {
|
if exitErr, ok := err.(ExitCoder); ok {
|
||||||
if err.Error() != "" {
|
if err.Error() != "" {
|
||||||
if _, ok := exitErr.(ErrorFormatter); ok {
|
if _, ok := exitErr.(ErrorFormatter); ok {
|
||||||
fmt.Fprintf(ErrWriter, "%+v\n", err)
|
_, _ = fmt.Fprintf(ErrWriter, "%+v\n", err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(ErrWriter, err)
|
_, _ = fmt.Fprintln(ErrWriter, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OsExiter(exitErr.ExitCode())
|
OsExiter(exitErr.ExitCode())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user