This commit is contained in:
Eyal Posener 2019-11-18 07:29:45 +02:00
parent 7c7ab2f59a
commit dd939deef3
1 changed files with 57 additions and 61 deletions

118
doc.go
View File

@ -27,8 +27,8 @@ Install:
1. Type in your shell: 1. Type in your shell:
go get -u github.com/posener/complete/gocomplete go get -u github.com/posener/complete/gocomplete
COMP_INSTALL=1 gocomplete COMP_INSTALL=1 gocomplete
2. Restart your shell 2. Restart your shell
@ -52,75 +52,71 @@ Usage
Add bash completion capabilities to any Go program. See ./example/command. Add bash completion capabilities to any Go program. See ./example/command.
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). To use this feature, simply call `complete.CommandLine` before `flag.Parse`. (See ./example/stdlib).
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`
function a `flag.Value` that also implements the `complete.Predictor` interface. For standard function a `flag.Value` that also implements the `complete.Predictor` interface. For standard
flag with values, it is possible to use the `github.com/posener/complete/compflag` package. flag with values, it is possible to use the `github.com/posener/complete/compflag` package.
(See ./example/compflag). (See ./example/compflag).
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`
which does them both. which does them both.