Update readme according to go doc

This commit is contained in:
goreadme 2019-11-18 05:30:00 +00:00
parent dd939deef3
commit 345db24139
1 changed files with 55 additions and 59 deletions

View File

@ -62,20 +62,18 @@ Supported shells:
Add bash completion capabilities to any Go program. See [./example/command](./example/command). Add bash completion capabilities to any Go program. See [./example/command](./example/command).
```go ```go
import ( import (
"flag" "flag"
"github.com/posener/complete/v2" "github.com/posener/complete/v2"
"github.com/posener/complete/v2/predict" "github.com/posener/complete/v2/predict"
) )
var (
var (
// Add variables to the program. // Add variables to the program.
name = flag.String("name", "", "") name = flag.String("name", "", "")
something = flag.String("something", "", "") something = flag.String("something", "", "")
nothing = flag.String("nothing", "", "") nothing = flag.String("nothing", "", "")
) )
func main() {
func main() {
// Create the complete command. // Create the complete command.
// Here we define completion values for each flag. // Here we define completion values for each flag.
cmd := &complete.Command{ cmd := &complete.Command{
@ -90,7 +88,7 @@ func main() {
// Parse the flags. // Parse the flags.
flag.Parse() flag.Parse()
// Program logic... // Program logic...
} }
``` ```
This package also enables to complete flags defined by the standard library `flag` package. This package also enables to complete flags defined by the standard library `flag` package.
@ -99,16 +97,15 @@ To use this feature, simply call `complete.CommandLine` before `flag.Parse`. (Se
```diff ```diff
import ( import (
"flag" "flag"
+ "github.com/posener/complete/v2" + "github.com/posener/complete/v2"
) )
var ( var (
// Define flags here... // Define flags here...
foo = flag.Bool("foo", false, "") foo = flag.Bool("foo", false, "")
) )
func main() { func main() {
// Call command line completion before parsing the flags - provide it with the binary name. // Call command line completion before parsing the flags - provide it with the binary name.
+ complete.CommandLine("my-program") + complete.CommandLine("my-program")
flag.Parse() flag.Parse()
} }
``` ```
@ -121,18 +118,17 @@ flag with values, it is possible to use the `github.com/posener/complete/compfla
```diff ```diff
import ( import (
"flag" "flag"
+ "github.com/posener/complete/v2" + "github.com/posener/complete/v2"
+ "github.com/posener/complete/v2/compflag" + "github.com/posener/complete/v2/compflag"
) )
var ( var (
// Define flags here... // Define flags here...
- foo = flag.Bool("foo", false, "") - foo = flag.Bool("foo", false, "")
+ foo = compflag.Bool("foo", false, "") + foo = compflag.Bool("foo", false, "")
) )
func main() { func main() {
// Call command line completion before parsing the flags. // Call command line completion before parsing the flags.
+ complete.CommandLine("my-program") + complete.CommandLine("my-program")
flag.Parse() flag.Parse()
} }
``` ```