cobra

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

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"
}

Documentation

Index

Constants

View Source
const (
	GopPackage = true
)

Variables

This section is empty.

Functions

func Gopt_App_Main

func Gopt_App_Main(app iAppProto, cmds ...iCommandProto)

Gopt_App_Main is required by Go+ compiler as the entry of a Cobra project.

Types

type App

type App struct {
	Command
}

App is the project class of Cobra classfile.

type Command

type Command struct {
	cobra.Command
}

Command is the worker class of Cobra classfile.

func (*Command) Long

func (p *Command) Long(l string)

Long sets the long message shown in the 'help <this-command>' output.

func (*Command) Main

func (p *Command) Main(cmd string)

Main is required by Go+ compiler as the entry of a Cobra command.

func (*Command) Run__0 added in v0.5.0

func (p *Command) Run__0(fn func())

Run sets the actual work function. Most commands will only implement this.

func (*Command) Run__1 added in v0.5.0

func (p *Command) Run__1(fn func(args []string))

Run sets the actual work function. Most commands will only implement this.

func (*Command) Short

func (p *Command) Short(s string)

Short sets the short description shown in the 'help' output.

func (*Command) Use

func (p *Command) Use(use string)

Use sets the one-line usage message.

Directories

Path Synopsis
demo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL