Merge pull request #106 from posener/goreadme

readme: Update according to go doc
This commit is contained in:
Eyal Posener 2019-11-18 07:30:20 +02:00 committed by GitHub
commit ca6cedb614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 55 additions and 59 deletions

114
README.md
View File

@ -62,55 +62,52 @@ 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() {
// Create the complete command.
func main() { // Here we define completion values for each flag.
// Create the complete command. cmd := &complete.Command{
// Here we define completion values for each flag. Flags: map[string]complete.Predictor{
cmd := &complete.Command{ "name": predict.Set{"foo", "bar", "foo bar"},
Flags: map[string]complete.Predictor{ "something": predict.Something,
"name": predict.Set{"foo", "bar", "foo bar"}, "nothing": predict.Nothing,
"something": predict.Something, },
"nothing": predict.Nothing, }
}, // Run the completion - provide it with the binary name.
} cmd.Complete("my-program")
// Run the completion - provide it with the binary name. // Parse the flags.
cmd.Complete("my-program") flag.Parse()
// Parse the flags. // Program logic...
flag.Parse() }
// 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.
To use this feature, simply call `complete.CommandLine` before `flag.Parse`. (See [./example/stdlib](./example/stdlib)). To use this feature, simply call `complete.CommandLine` before `flag.Parse`. (See [./example/stdlib](./example/stdlib)).
```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() }
}
``` ```
If flag value completion is desired, it can be done by providing the standard library `flag.Var` If flag value completion is desired, it can be done by providing the standard library `flag.Var`
@ -119,22 +116,21 @@ flag with values, it is possible to use the `github.com/posener/complete/compfla
(See [./example/compflag](./example/compflag)). (See [./example/compflag](./example/compflag)).
```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() }
}
``` ```
Instead of calling both `complete.CommandLine` and `flag.Parse`, one can call just `compflag.Parse` Instead of calling both `complete.CommandLine` and `flag.Parse`, one can call just `compflag.Parse`