diff options
| author | Eyal Posener <[email protected]> | 2019-11-18 07:30:20 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-11-18 07:30:20 +0200 |
| commit | ca6cedb61484d2a211c105900f663e355b13626a (patch) | |
| tree | 56672d7a7f32ca62e4df8bccb90a7f1fbfb3b1fa | |
| parent | dd939deef39f017909b555d687ab390e56a5ce91 (diff) | |
| parent | 345db2413930ed699d76ac24c61dd32cdf3fe77a (diff) | |
Merge pull request #106 from posener/goreadme
readme: Update according to go doc
| -rw-r--r-- | README.md | 114 |
1 files changed, 55 insertions, 59 deletions
@@ -62,55 +62,52 @@ Supported shells: Add bash completion capabilities to any Go program. See [./example/command](./example/command). ```go -import ( - "flag" - "github.com/posener/complete/v2" - "github.com/posener/complete/v2/predict" -) - -var ( - // Add variables to the program. - name = flag.String("name", "", "") - something = flag.String("something", "", "") - nothing = flag.String("nothing", "", "") -) - -func main() { - // Create the complete command. - // Here we define completion values for each flag. - cmd := &complete.Command{ - Flags: map[string]complete.Predictor{ - "name": predict.Set{"foo", "bar", "foo bar"}, - "something": predict.Something, - "nothing": predict.Nothing, - }, - } - // Run the completion - provide it with the binary name. - cmd.Complete("my-program") - // Parse the flags. - flag.Parse() - // Program logic... -} + import ( + "flag" + "github.com/posener/complete/v2" + "github.com/posener/complete/v2/predict" + ) + var ( + // Add variables to the program. + name = flag.String("name", "", "") + something = flag.String("something", "", "") + nothing = flag.String("nothing", "", "") + ) + func main() { + // Create the complete command. + // Here we define completion values for each flag. + cmd := &complete.Command{ + Flags: map[string]complete.Predictor{ + "name": predict.Set{"foo", "bar", "foo bar"}, + "something": predict.Something, + "nothing": predict.Nothing, + }, + } + // Run the completion - provide it with the binary name. + cmd.Complete("my-program") + // Parse the flags. + flag.Parse() + // Program logic... + } ``` 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)). ```diff - import ( - "flag" - + "github.com/posener/complete/v2" - ) - var ( - // Define flags here... - foo = flag.Bool("foo", false, "") - ) - - func main() { - // Call command line completion before parsing the flags - provide it with the binary name. - + complete.CommandLine("my-program") - flag.Parse() - } + import ( + "flag" ++ "github.com/posener/complete/v2" + ) + var ( + // Define flags here... + foo = flag.Bool("foo", false, "") + ) + func main() { + // Call command line completion before parsing the flags - provide it with the binary name. ++ complete.CommandLine("my-program") + flag.Parse() + } ``` 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)). ```diff - import ( - "flag" - + "github.com/posener/complete/v2" - + "github.com/posener/complete/v2/compflag" - ) - var ( - // Define flags here... - - foo = flag.Bool("foo", false, "") - + foo = compflag.Bool("foo", false, "") - ) - - func main() { - // Call command line completion before parsing the flags. - + complete.CommandLine("my-program") - flag.Parse() - } + import ( + "flag" ++ "github.com/posener/complete/v2" ++ "github.com/posener/complete/v2/compflag" + ) + var ( + // Define flags here... +- foo = flag.Bool("foo", false, "") ++ foo = compflag.Bool("foo", false, "") + ) + func main() { + // Call command line completion before parsing the flags. ++ complete.CommandLine("my-program") + flag.Parse() + } ``` Instead of calling both `complete.CommandLine` and `flag.Parse`, one can call just `compflag.Parse` |
