star

package module
v0.0.0-...-5913a16 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2022 License: MPL-2.0 Imports: 12 Imported by: 0

README

Star

Star is an opinionated Go library for writing CLI applications.

Goals

  • Make testing commands easy.
  • Make working with parameters (args and flags) easy.
  • No parsing or nullable flag/arg noise in the applications main flow.
  • Separate the way a parameter is passed (via flag or positionally) from its use.
  • Eliminate the need to use shared package level state.

Design

Commands in Star are just metadata around functions. Utilities are provided to create "directory" or "parent" commands which are common in modern CLI apps. Parent commands take 1 argument and use it to lookup the name of a child command.

Command functions are of type func(*star.Context) error

The Context has references to std{in, out, err}, as well as methods to access any parameters the command is expecting. Commands will never be executed without their requested parameters.

Retreiving a parameter from the star.Context will always succeed or always panic, regardless of the programs runtime input. So panicing when accessing parameters is always a logic error, never a user error. If the user forgets a parameter it will not panic and instead return an error without running any of the application logic.

Documentation

Overview

package star is for creating command line tools

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(ctx context.Context, c *Command, env Env, calledAs string, args []string) error

Execute executes the command c.

func M

func M[T any](ctx *Context, x string) (T, bool)

M is a generic function which retrieve maybe values from a Context The M stands for Maybe.

func Main

func Main(name string, c *Command, opts ...MainOpt)

Main calls Execute with context.Background and IO streams and args from the OS.

You can call Main from main. e.g.

func main() {
  star.Main(cmd)
}

func V

func V[T any](ctx *Context, x string) T

V is a generic function which retrieves values from a Context The V stands for Value

Types

type Command

type Command struct {
	// Short is a short doc string
	Short string

	// Pos is a slice of specifications for positional arguments
	Pos []Pos
	// Flags are the flags the command is expecting
	Flags []Flag

	// F is the function to run
	F Func
}

Command is a Func and associated metadata.

func NewParent

func NewParent(short string, flags []Flag, children map[string]*Command) *Command

NewParent constructs a parent command grouping several child commans

func (*Command) Clone

func (c *Command) Clone() *Command

func (*Command) Usage

func (c *Command) Usage() string

type Context

type Context struct {
	context.Context

	// In is stdin
	In io.Reader
	// Out is stdout
	Out io.Writer
	// Err is stderr
	Err io.Writer

	CalledAs string
	Args     []string
	// contains filtered or unexported fields
}

Context is passed to Funcs

func (*Context) Bytes

func (c *Context) Bytes(x string) []byte

func (*Context) Int64

func (c *Context) Int64(x string) int64

func (*Context) MaybeInt64

func (c *Context) MaybeInt64(x string) (int64, bool)

func (*Context) MaybeString

func (c *Context) MaybeString(x string) (string, bool)

func (*Context) MaybeUint64

func (c *Context) MaybeUint64(x string) (uint64, bool)

func (*Context) String

func (c *Context) String(x string) string

func (*Context) StringSlice

func (c *Context) StringSlice(x string) []string

func (*Context) Time

func (c *Context) Time(x string) string

func (*Context) Uint64

func (c *Context) Uint64(x string) uint64

func (*Context) WithValue

func (c *Context) WithValue(k, v interface{}) *Context

type Env

type Env struct {
	// In is stdin
	In io.Reader
	// Out is stdout
	Out io.Writer
	Err io.Writer
}

Env is the total environment in which the command runs. TODO: add FS interface, and environment variables.

type ExitCodeError

type ExitCodeError interface {
	ExitCode() int
}

ExitCodeError is an error which can set an exit code.

type Flag

type Flag struct {
	Name  string
	Char  byte
	Short string

	IsRepeated bool
	Param
}

Flag is a the specification of an argument

func NewFlag

func NewFlag[T any](name, shortDoc string, isRequired bool) Flag

NewFlag constructs a new Flag parameter

func (Flag) WithDefault

func (f Flag) WithDefault(x any) Flag

type Func

type Func = func(ctx *Context) error

Func is the type of functions which can be run as commands

func PostExec

func PostExec(x Func, postF func(ctx *Context) error) Func

PostExec returns a function which calls postF after x. PostExec always runs after x, even if x returns an error. x's error is prioritized in the return value

func PreExec

func PreExec(x Func, preF func(ctx *Context) (*Context, error)) Func

PreExec returns a function x which calls preF before x.

func PrePostExec

func PrePostExec(x Func, preF func(ctx *Context) (*Context, error), postF func(ctx *Context) error) Func

type MainOpt

type MainOpt = func(*mainConfig)

MainOpt configures Main

type Param

type Param struct {
	Type       Type
	IsRequired bool
	Default    any
	Check      func(any) error
}

Param is a value that needs to be passed to a program

func (Param) String

func (p Param) String() string

type Pos

type Pos struct {
	Name  string
	Short string

	IsRepeated bool
	Param
}

Pos is a specification for a positional argument

func NewPos

func NewPos[T any](name, shortDoc string, isRequired bool) Pos

NewPos returns a new positional parameter

func (Pos) WithDefault

func (p Pos) WithDefault(x any) Pos

type Type

type Type = reflect.Type

func TypeOf

func TypeOf[T any]() Type

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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