reaper

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2019 License: Unlicense Imports: 11 Imported by: 0

README

Reaper

A go library for creating a command line app.

func main() {
  app := reaper.NewApp("example")

  app.Description = "This is an example app for the Reaper CLI library"

  app.Command("greet", func(c *reaper.Context) error {
    name, err := c.Input("What's your name?")
    if err != nil {
      return err
    }

    c.Outputf("%s %s!\n", c.FlagString("greeting"), name)

    return nil
  }).Configure(func(c *reaper.Command) {
    c.Description = "prints the greeting and name"
    c.Flag("greeting", "string", "Hi", "the greeting to perform")
  })

  err := app.Execute(os.Args[1:])
  if err != nil {
    log.Fatal(err)
  }
}

$ example greet

Builtin Help

Given the above example, out of the box the help sub-command will print this.

example help
Version: 1.0.0

This is an example app for the Reaper CLI library

Commands
greet -- prints the greeting and name
  Flags:
    -greeting [string] -- the greeting to perform (default Hi)

help -- prints this help dialogue

version -- print the current version

Documentation

Index

Constants

View Source
const (
	// Version is the current version of reaper
	Version = "0.2.0"
)

Variables

This section is empty.

Functions

func Fatal added in v0.1.0

func Fatal(err error)

Fatal prints the error and exits with a code of 1

Types

type App

type App struct {
	Description string
	Version     string
	// contains filtered or unexported fields
}

App contains the configuration for a command line application

func NewApp

func NewApp(name string) *App

NewApp returns a new app ready to be configured

func (*App) AddApp added in v0.2.0

func (a *App) AddApp(app *App)

AddApp adds an app as a sub-application

func (*App) Before added in v0.0.2

func (a *App) Before(fn CallbackHandler)

Before adds a function handler

This auto-excludes the help & version commands

func (*App) BeforeExcluding added in v0.0.3

func (a *App) BeforeExcluding(fn CallbackHandler, exclude ...string)

BeforeExcluding adds a function handler but doesn't run it for the passed command names

func (*App) Command

func (a *App) Command(name string, handler CommandHandler) *Command

Command creates a new command with the handler and adds it to the app.

func (*App) Configure added in v0.2.0

func (a *App) Configure(fn func(c *Command))

Configure takes a function to call that is passed whenever a command is called

func (*App) Execute

func (a *App) Execute(args []string) error

Execute takes a list of arguments and runs the command that matches.

type CallbackHandler added in v0.0.2

type CallbackHandler func(*Context) error

CallbackHandler is the function signature for app callbacks

type Command

type Command struct {
	Description string

	Private bool
	// contains filtered or unexported fields
}

Command represents a single sub-command in the application

func (*Command) Argument

func (c *Command) Argument(name string, required bool, usage string)

Argument adds a named argument to the command

func (*Command) Configure

func (c *Command) Configure(fn func(*Command)) *Command

Configure calls the passed function with the command.

This is a convenience method for chaining `app.Command("thing", thingHandler).Configure(func(c *Command) { ... })`

func (*Command) Example

func (c *Command) Example(e string)

Example adds and example to the output

func (*Command) Flag

func (c *Command) Flag(name, kind string, value interface{}, usage string)

Flag adds a cli flag to the command

func (*Command) IsInternal added in v0.1.0

func (c *Command) IsInternal() bool

IsInternal returns if the command is defined from reaper

type CommandHandler

type CommandHandler func(*Context) error

CommandHandler is the function that will be called when a user performs a sub-command

type Context

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

Context is used to hold the state of the application

func (*Context) Argument

func (c *Context) Argument(name string) string

Argument returns a named argument

func (*Context) Confirm

func (c *Context) Confirm(msg string) bool

Confirm asks for conformation before continuing

func (*Context) Flag

func (c *Context) Flag(name string) (interface{}, error)

Flag returns a flag with the passed name. If the flag is not found an error will be returned.

func (*Context) FlagCollection added in v0.0.4

func (c *Context) FlagCollection(name string) []string

func (*Context) FlagString

func (c *Context) FlagString(name string) string

FlagString returns a flag string value.

This will panic if the flag does not exist

func (*Context) Get

func (c *Context) Get(key string) interface{}

Get returns a stored value from the context

func (*Context) Input added in v0.0.2

func (c *Context) Input(msg string) (string, error)

Input asks for user input

func (*Context) Output

func (c *Context) Output(value string)

Output writes the string to stdout

func (*Context) OutputJSON added in v0.2.0

func (c *Context) OutputJSON(obj interface{}) error

OutputJSON dumps the object into a pretty-printed JSON string.

func (*Context) Outputf

func (c *Context) Outputf(format string, args ...interface{})

Outputf writes a formatted string to stdout

func (*Context) Set added in v0.0.2

func (c *Context) Set(key string, value interface{})

Set stores a value in the context for the key

type Error added in v0.1.0

type Error struct {
	Message    string
	ExitStatus int
}

Error is a reaper error that can be returned from a command handler to exit with a specific status code

func NewError added in v0.1.0

func NewError(status int, message string) Error

NewError returns a new error

func NewErrorf added in v0.1.0

func NewErrorf(status int, format string, args ...interface{}) Error

NewErrorf returns a new error

func (Error) Error added in v0.1.0

func (e Error) Error() string

type FlagCollection added in v0.0.4

type FlagCollection struct {
	Values []string
}

FlagCollection contains a list of flags

func (*FlagCollection) Set added in v0.0.4

func (f *FlagCollection) Set(value string) error

Set adds a flag value

func (*FlagCollection) String added in v0.0.4

func (f *FlagCollection) String() string

Jump to

Keyboard shortcuts

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