cmd

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package cmd provides (sub-)command functionality for command-line programs. This package works closely with the standard library flag package.

Index

Constants

This section is empty.

Variables

View Source
var DefaultCmdMap = NewCmdMap()

DefaultCmdMap is the default command map.

Functions

func PrintCmds

func PrintCmds()

PrintCmds prints help text for all commands in the default command map to stderr.

func Run

func Run()

Run parses the command line and executes the specified (sub-)command from the default command map.

func UsageFunc

func UsageFunc(args ...interface{}) func()

UsageFunc returns a usage function appropriate for use with flag.FlagSet.

Types

type Cmd

type Cmd struct {
	// Flag contains the command flag set.
	Flag *flag.FlagSet

	// Main is the function to run when the command is selected.
	Main func(cmd *Cmd, args []string)

	// Use contains the command usage string.
	Use string

	// Desc contains the command description.
	Desc string
}

Cmd encapsulates a (sub-)command.

func Add

func Add(name string, main func(*Cmd, []string)) *Cmd

Add adds a new command in the default command map.

The name parameter is the command name. However if this parameter contains a space or newline it is interpreted as described below. Consider:

NAME ARGUMENTS
DESCRIPTION

Then the command name becomes "NAME", the command Use field becomes "NAME ARGUMENTS" and the command Desc field becomes "DESCRIPTION".

func (*Cmd) GetFlag

func (self *Cmd) GetFlag(name string) interface{}

GetFlag gets the value of the named flag.

type CmdMap

type CmdMap struct {
	// contains filtered or unexported fields
}

CmdMap encapsulates a (sub-)command map.

func NewCmdMap

func NewCmdMap() *CmdMap

NewCmdMap creates a new command map.

func (*CmdMap) Add

func (self *CmdMap) Add(name string, main func(*Cmd, []string)) (cmd *Cmd)

Add adds a new command in the command map.

The name parameter is the command name. However if this parameter contains a space or newline it is interpreted as described below. Consider:

NAME ARGUMENTS
DESCRIPTION

Then the command name becomes "NAME", the command Use field becomes "NAME ARGUMENTS" and the command Desc field becomes "DESCRIPTION".

func (*CmdMap) Get

func (self *CmdMap) Get(name string) *Cmd

Get gets a command by name.

func (*CmdMap) GetNames

func (self *CmdMap) GetNames() []string

GetNames gets all command names.

func (*CmdMap) PrintCmds

func (self *CmdMap) PrintCmds()

PrintCmds prints help text for all commands to stderr.

func (*CmdMap) Run

func (self *CmdMap) Run(flagSet *flag.FlagSet, args []string)

Run parses the command line and executes the specified (sub-)command.

Jump to

Keyboard shortcuts

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