Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

mitchellh/cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jul 22, 2024
e73bd00 Â· Jul 22, 2024
Apr 21, 2022
Dec 10, 2013
Mar 23, 2020
Jul 22, 2024
Jul 17, 2017
May 13, 2022
May 13, 2022
Jul 17, 2017
Jul 17, 2017
Nov 3, 2013
Jul 1, 2022
Jul 1, 2022
Feb 1, 2017
Dec 30, 2015
Apr 5, 2018
Apr 29, 2015
Nov 5, 2013
Dec 25, 2017
Dec 25, 2017
Dec 25, 2017
Jan 8, 2016
Jan 8, 2016

Repository files navigation

🚨 This project is archived. 🚨 Learn More

Go CLI Library GoDoc

cli is a library for implementing command-line interfaces in Go. cli is the library that powers the CLI for Packer, Consul, Vault, Terraform, Nomad, and more.

Features

  • Easy sub-command based CLIs: cli foo, cli bar, etc.

  • Support for nested subcommands such as cli foo bar.

  • Optional support for default subcommands so cli does something other than error.

  • Support for shell autocompletion of subcommands, flags, and arguments with callbacks in Go. You don't need to write any shell code.

  • Automatic help generation for listing subcommands.

  • Automatic help flag recognition of -h, --help, etc.

  • Automatic version flag recognition of -v, --version.

  • Helpers for interacting with the terminal, such as outputting information, asking for input, etc. These are optional, you can always interact with the terminal however you choose.

  • Use of Go interfaces/types makes augmenting various parts of the library a piece of cake.

Example

Below is a simple example of creating and running a CLI

package main

import (
	"log"
	"os"

	"github.com/mitchellh/cli"
)

func main() {
	c := cli.NewCLI("app", "1.0.0")
	c.Args = os.Args[1:]
	c.Commands = map[string]cli.CommandFactory{
		"foo": fooCommandFactory,
		"bar": barCommandFactory,
	}

	exitStatus, err := c.Run()
	if err != nil {
		log.Println(err)
	}

	os.Exit(exitStatus)
}