Snippets

Atlassian Labs golang-bash-completions-with-kingpin

Created by Nicholas Whyte
package main

import (
	"fmt"
	"os"

	"gopkg.in/alecthomas/kingpin.v2"
)

func addSubCommand(app *kingpin.Application, name string, description string) {
	app.Command(name, description).Action(func(c *kingpin.ParseContext) error {
		fmt.Printf("Would have run command %s.\n", name)
		return nil
	})
}

func main() {
	app := kingpin.New("my-app", "My Sample Kingpin App!")

	configureNetcatCommand(app)

	// Add some additional top level commands
	addSubCommand(app, "ls", "Additional top level command to show command completion")
	addSubCommand(app, "ping", "Additional top level command to show command completion")
	addSubCommand(app, "nmap", "Additional top level command to show command completion")

	kingpin.MustParse(app.Parse(os.Args[1:]))
}

type NetcatCommand struct {
	hostName string
	port     int
	format   string
}

func (n *NetcatCommand) run(c *kingpin.ParseContext) error {
	fmt.Printf("Would have run netcat to hostname %v, port %d, and output format %v\n", n.hostName, n.port, n.format)
	return nil
}

func configureNetcatCommand(app *kingpin.Application) {
	c := &NetcatCommand{}
	nc := app.Command("nc", "Connect to a Host").Action(c.run)
	nc.Flag("nop-flag", "Example of a flag with no options").Bool()

	// You can provide hint options statically
	nc.Flag("port", "Provide a port to connect to").
		Required().
		HintOptions("80", "443", "8080").
		IntVar(&c.port)

	// Enum/EnumVar options will be turned into completion options automatically
	nc.Flag("format", "Define the output format").
		Default("raw").
		EnumVar(&c.format, "raw", "json")

	// You can provide hint options using a function to generate them
	nc.Flag("host", "Provide a hostname to nc").
		Required().
		HintAction(listHosts).
		StringVar(&c.hostName)

}

func listHosts() []string {
	// Provide a dynamic list of hosts from a hosts file or otherwise
	// for bash completion. In this example we simply return static slice.

	// You could use this functionality to reach into a hosts file to provide
	// completion for a list of known hosts.
	return []string{"sshhost.example", "webhost.example", "ftphost.example"}
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.