commands

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2020 License: BSD-3-Clause Imports: 7 Imported by: 3

Documentation

Overview

Package Commands provides common definitions & functionality for a CLI tool subcommand implementations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

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

Command related data

func (*Command) Description

func (c *Command) Description() string

func (*Command) Flags

func (cmd *Command) Flags() *flag.FlagSet

Flags returns the command's own FlagSet

func (*Command) Handle

func (cmd *Command) Handle(arguments []string) int

Handle is Command's handler receiver that invokes the handles, each command implementation must provide

func (*Command) Name

func (c *Command) Name() string

func (*Command) Usage

func (cmd *Command) Usage()

Usage prints the command's Usage help

type Handler

type Handler func(*Command, []string) int

Handler a subcommand must provide

type Map

type Map struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Map of Commands keyed by a normalized command name. Provides a global table of all registered subcommands

func (*Map) Add

func (cmds *Map) Add(name, descr string, handler Handler) *Command

Add adds a new subcommnd to the global registered commands table

func (*Map) Get

func (cmds *Map) Get(cmdName string) *Command

Get finds & returns a command with the given name. Returns nil if not found

func (*Map) GetCommand

func (cmds *Map) GetCommand() (cmd *Command, cmdArgs []string)

GetCommand returns first command line command (non-flag argument) if any & its list of arguments

func (*Map) GetIdx

func (cmds *Map) GetIdx(cmdName string) (cmd *Command, idx int)

GetIdx finds & returns a command with cmdName and it's index in the command line. Returns (nil, idx) if not found

func (*Map) HandleCommand

func (cmds *Map) HandleCommand() (exitCode int, err error)

HandleCommand parses command line & handles first found command with its arguments it returns command exit code with nil error if successful or error if command is not registered/invalid HandleCommand also prints help/usage into stdout if command is missing or is help/h

func (*Map) Usage

func (cmds *Map) Usage()

Usage prints out all commands' usages shifted right by two tabs

Jump to

Keyboard shortcuts

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