tool

package
v0.0.0-...-5a1941a Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2016 License: BSD-2-Clause Imports: 4 Imported by: 8

Documentation

Overview

Package tool is used for parsing input parameters, and starting subcommands (aka tools).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

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

Context stores information about available subcommands (aka tools) and other related things that are necessary for their start.

func NewContext

func NewContext(handlers ...Handler) *Context

NewContext gets a number of handlers as arguments, allocates a new Context and returns it.

func (*Context) Run

func (c *Context) Run(args []string) error

Run gets a list of arguments and either starts an entry function of the requested subcommand (aka tool) or returns an error.

type Data

type Data []string

Data represents a set of non-flag arguments for a tool.

func (Data) GetDefault

func (a Data) GetDefault(i int, defaultVal string) string

GetDefault is a helper for getting a value with specific index or returning a default value if the index does not exist.

type Handler

type Handler struct {
	// Run is an entry function of the handler.
	// The args are the arguments after the command name.
	Run func(hs []Handler, i int, args Data)

	// Default means the handler must be executed if no arguments are
	// received from user (in addition to when it is called explicitly).
	// Only first default handler is used, others will be ignored.
	Default bool

	Name  string // Name of the handler, e.g. "new" or "generate stuff".
	Usage string // Possible arguments of the command, e.g. "[input] [output]".
	Info  string // One line description of the command.
	Desc  string // Detailed description of what the command does.

	Flags flag.FlagSet // Set of flags specific to the command.
}

Handler is a type for representation of a subcommand (aka tool) such as "cli run" or "cli new".

func (Handler) Requested

func (h Handler) Requested(args []string) ([]string, bool)

Requested checks whether the handler is the one that is requested by user, i.e. handler's name or alias is a part of args. It returns arguments (not including the handler name) and true in case of success, and nil, false otherwise.

Jump to

Keyboard shortcuts

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