Go to file
Eyal Posener c06142941d [gocomplete] fix test name completion 2017-05-08 06:44:00 +03:00
cmd Fix one more vet 2017-05-06 22:26:29 +03:00
gocomplete [gocomplete] fix test name completion 2017-05-08 06:44:00 +03:00
match Improve Matcher interface 2017-05-07 19:57:44 +03:00
tests Fix tests 2017-05-06 00:46:03 +03:00
.gitignore Add go test script and travis file 2017-05-06 00:38:16 +03:00
.travis.yml [travis] test multiple packages 2017-05-08 06:30:01 +03:00
LICENSE.txt Add licence 2017-05-06 22:16:39 +03:00
command.go Move match to a separate package 2017-05-07 19:57:41 +03:00
log.go Roughly add all go commands 2017-05-06 00:25:27 +03:00
predicate.go Move match to a separate package 2017-05-07 19:57:41 +03:00
predicate_test.go [test] chdir and set logger only once 2017-05-06 23:07:26 +03:00
readme.md fix readme install instructions 2017-05-07 16:57:09 +03:00
run.go Remove Name from Command struct 2017-05-07 07:59:42 +03:00
run_test.go [test] chdir and set logger only once 2017-05-06 23:07:26 +03:00
test.sh [travis] test multiple packages 2017-05-08 06:30:01 +03:00
tests.go [test] chdir and set logger only once 2017-05-06 23:07:26 +03:00

readme.md

complete

Build Status codecov GoDoc Go Report Card

A tool for bash writing bash completion in go.

Writing bash completion scripts is a hard work. This package provides an easy way to create bash completion scripts for any command, and also an easy way to install/uninstall the completion of the command.

go command bash completion

In gocomplete there is an example for bash completion for the go command line.

Install

  1. Type in your shell:
go install github.com/posener/complete/gocomplete
gocomplete -install
  1. Restart your shell

Uninstall

gocomplete -uninstall

Usage

Assuming you have program called run and you want to have bash completion for it, meaning, if you type run then space, then press the Tab key, the shell will suggest relevant complete options.

In that case, we will create a program called runcomplete, a go program, with a func main() and so, that will make the completion of the run program. Once the runcomplete will be in a binary form, we could runcomplete -install and that will add to our shell all the bash completion options for run.

So here it is:

import "github.com/posener/complete"

func main() {

	// create a Command object, that represents the command we want
	// to complete.
	run := complete.Command{

		// Sub defines a list of sub commands of the program,
		// this is recursive, since every command is of type command also.
		Sub: complete.Commands{

			// add a build sub command
			"build": complete.Command {

				// define flags of the build sub command
				Flags: complete.Flags{
					// build sub command has a flag '-fast', which
					// does not expects anything after it.
					"-fast": complete.PredictNothing,
				},
			},
		},

		// define flags of the 'run' main command
		Flags: complete.Flags{

			// a flag '-h' which does not expects anything after it
			"-h": complete.PredictNothing,

			// a flag -o, which expects a file ending with .out after
			// it, the tab completion will auto complete for files matching
			// the given pattern.
			"-o": complete.PredictFiles("*.out"),
		},
	}

	// run the command completion, as part of the main() function.
	// this triggers the autocompletion when needed.
    // name must be exactly as the binary that we want to complete.
	complete.Run("run", run)
}