2019-03-07 14:42:18 -06:00
# complete
[![Build Status ](https://travis-ci.org/posener/complete.svg?branch=master )](https://travis-ci.org/posener/complete)
[![codecov ](https://codecov.io/gh/posener/complete/branch/master/graph/badge.svg )](https://codecov.io/gh/posener/complete)
2020-04-10 02:36:11 -05:00
[![GoDoc ](https://img.shields.io/badge/pkg.go.dev-doc-blue )](http://pkg.go.dev/github.com/posener/complete)
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
Package complete is everything for bash completion and Go.
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
Writing bash completion scripts is a hard work, usually done in the bash scripting language.
This package provides:
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
* A library for bash completion for Go programs.
* A tool for writing bash completion script in the Go language. For any Go or non Go program.
* Bash completion for the `go` command line (See [./gocomplete ](./gocomplete )).
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
* Library for bash-completion enabled flags (See [./compflag ](./compflag )).
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
* Enables an easy way to install/uninstall the completion of the command.
The library and tools are extensible such that any program can add its one logic, completion types
2020-04-10 02:36:11 -05:00
or methologies.
2019-11-17 17:10:11 -06:00
2020-04-09 14:06:10 -05:00
## Go Command Bash Completion
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
[./gocomplete ](./gocomplete ) is the script for bash completion for the `go` command line. This is an example
that uses the `complete` package on the `go` command - the `complete` package can also be used to
implement any completions, see #usage .
Install:
2019-03-07 14:42:18 -06:00
1. Type in your shell:
2019-03-08 01:45:57 -06:00
```go
2019-11-23 04:30:12 -06:00
go get -u github.com/posener/complete/v2/gocomplete
2019-11-17 17:10:11 -06:00
COMP_INSTALL=1 gocomplete
2019-03-08 01:45:57 -06:00
```
2019-03-07 14:42:18 -06:00
2. Restart your shell
2019-11-17 17:10:11 -06:00
Uninstall by `COMP_UNINSTALL=1 gocomplete`
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
Features:
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
- Complete `go` command, including sub commands and flags.
2019-03-07 14:42:18 -06:00
- Complete packages names or `.go` files when necessary.
- Complete test names after `-run` flag.
2020-04-09 14:06:10 -05:00
## Complete Package
2019-03-07 14:42:18 -06:00
Supported shells:
- [x] bash
- [x] zsh
- [x] fish
2020-04-04 04:47:08 -05:00
The installation of completion for a command line tool is done automatically by this library by
running the command line tool with the `COMP_INSTALL` environment variable set. Uninstalling the
completion is similarly done by the `COMP_UNINSTALL` environment variable.
For example, if a tool called `my-cli` uses this library, the completion can install by running
`COMP_INSTALL=1 my-cli` .
2020-04-09 14:06:10 -05:00
## Usage
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
Add bash completion capabilities to any Go program. See [./example/command ](./example/command ).
2019-03-07 14:42:18 -06:00
2019-03-08 01:45:57 -06:00
```go
2019-11-17 23:30:00 -06:00
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...
}
2019-11-17 17:10:11 -06:00
```
2019-03-08 01:45:57 -06:00
2019-11-17 17:10:11 -06:00
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
2019-11-17 23:30:00 -06:00
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()
}
2019-11-17 17:10:11 -06:00
```
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
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
2019-11-23 04:30:12 -06:00
flag with values, it is possible to use the `github.com/posener/complete/v2/compflag` package.
2019-11-17 17:10:11 -06:00
(See [./example/compflag ](./example/compflag )).
```diff
2019-11-17 23:30:00 -06:00
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()
}
2019-03-08 01:45:57 -06:00
```
2019-11-17 17:10:11 -06:00
Instead of calling both `complete.CommandLine` and `flag.Parse` , one can call just `compflag.Parse`
which does them both.
2020-04-09 14:06:10 -05:00
## Testing
2019-03-07 14:42:18 -06:00
2019-11-17 17:10:11 -06:00
For command line bash completion testing use the `complete.Test` function.
2019-03-07 14:42:18 -06:00
## Sub Packages
2019-11-17 17:10:11 -06:00
* [compflag ](./compflag ): Package compflag provides a handful of standard library-compatible flags with bash complition capabilities.
2019-03-07 14:42:18 -06:00
2020-05-09 06:18:46 -05:00
* [compflag/gen ](./compflag/gen ): Generates flags.go.
2020-04-09 13:35:26 -05:00
* [example/command ](./example/command ): command shows how to have bash completion to an arbitrary Go program using the `complete.Command` struct.
* [example/compflag ](./example/compflag ): compflag shows how to use the github.com/posener/complete/v2/compflag package to have auto bash completion for a defined set of flags.
* [example/stdlib ](./example/stdlib ): stdlib shows how to have flags bash completion to an arbitrary Go program that uses the standard library flag package.
2019-03-07 14:42:18 -06:00
* [gocomplete ](./gocomplete ): Package main is complete tool for the go command line
2019-11-18 22:32:42 -06:00
* [install ](./install ): Package install provide installation functions of command completion.
2019-11-17 17:10:11 -06:00
* [predict ](./predict ): Package predict provides helper functions for completion predictors.
2019-03-07 14:42:18 -06:00
2019-05-29 03:47:43 -05:00
---
2020-04-09 13:35:26 -05:00
Readme created from Go doc with [goreadme ](https://github.com/posener/goreadme )