Skip to content

Latest commit

 

History

History
119 lines (86 loc) · 2.55 KB

README.md

File metadata and controls

119 lines (86 loc) · 2.55 KB

cobra - A Commander for modern Go+ CLI interactions

Build Status Go Report Card GitHub release GoDoc Language

The cobra classfile has the file suffix _cmd.gox.

How to use in Go+

First let us initialize a hellocli project:

gop mod init hellocli

Then we have it reference the cobra classfile as the CLI framework:

gop get github.com/goplus/cobra@latest

Create a file named version_cmd.gox with the following content:

run => {
	echo "command: version"
}

Execute the following commands:

gop mod tidy
gop install .
hellocli

You may get the following output:

Usage:
  hellocli [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  help        Help about any command
  version

Flags:
  -h, --help   help for hellocli

Use "hellocli [command] --help" for more information about a command.

Command settings

Continue to modify the version command:

// short sets the short description shown in the 'help' output.
short "print Go version"

// long sets the long message shown in the 'help <this-command>' output.
long `Version prints the build information for Go binary files.
`

run => {
	echo "command: version"
}

Command flags

The cobra classfile uses tags of class fields to specify command flags.

var (
	Verbose bool `flag:"verbose, short: v, val: false, usage: print verbose information"`
)

run => {
	echo "command: version", "verbose:", Verbose
}

Command args

If a command has non-flag arguments, use run args => { ... } instead of run => { ... } to get non-flag arguments:

run args => {
	echo "version:", args
}

Subcommand

Create a file named mod_cmd.gox with the following content:

run => {
	help
}

And create a file named mod_init_cmd.gox with the following content:

run => {
	echo "subcommand: mod init"
}