subcmd

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2019 License: MIT Imports: 5 Imported by: 10

README

subcmd

GoDoc

This is a Go package that's designed to work with the flag package to handle git-style CLI interfaces comprising multiple sub-commands:

myprog foo
myprog bar -f

See the example directory for a small example usage.

Documentation

Overview

Package subcmd works with the flags package to implement sub-commands in the manner of git and similar tools.

Index

Constants

This section is empty.

Variables

View Source
var ErrHelp = errors.New("subcmd: help requested")

ErrHelp is the error returned if the first argument is "help", "-h", "-help", or "--help".

View Source
var Usage = func(cmds []Command) {
	defaultUsage(os.Args[0], cmds)
}

Usage prints a help message listing the possible commands. The function is a variable that may be changed to point at a custom function.

Functions

func PrintDefaults

func PrintDefaults(cmds []Command)

PrintDefaults formats a list of commands. For each command, the output is

Name    Description

func Run

func Run(cmds []Command)

Run parses os.Args and dispatches to the correct subcommand given by cmds. It produces an error message listing the commands with their descriptions if a nonexistent subcommand is provided, or if the command is "help", "-h", "-help", or "--help". This error message may be customized by altering Usage. If the command provided isn't one of those in cmds, Run calls os.Exit(2) after printing the error message.

Run panics if any command is named "help", "-h", "-help", or "--help", or if any two commands have the same name.

Types

type Command

type Command struct {
	Name        string              // the command's one-word name
	Description string              // a short description of the command
	Do          func(args []string) // command implementation
}

A Command specifies a sub-command for a program's command-line interface.

type Runner added in v1.1.0

type Runner struct {

	// Usage prints the runner's usage.
	// If Usage is nil, the package-level Usage is called instead.
	Usage func()
	// contains filtered or unexported fields
}

A Runner runs sub-commands. To change Usage or ErrorHandling, alter these after creating a runner with New but before calling Runner.Run.

func New added in v1.1.0

func New(name string, cmds []Command, errorHandling flag.ErrorHandling) *Runner

New creates a Runner with the given name and command list. The error-handling behavior of Run is controlled by errorHandling and has the same semantics as for flag.FlagSet.

New panics if any command is named "help", "-h", "-help", or "--help", or if any two commands have the same name.

func (*Runner) Run added in v1.1.0

func (r *Runner) Run(args []string) error

Run parses args and dispatches to the correct subcommand. It produces an error message listing the commands with their descriptions if a nonexistent subcommand is provided, or if the command is "help", "-h", "-help", or "--help". This error message may be customized by altering r.Usage.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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