Documentation ¶
Index ¶
- Variables
- func GetFlag[T FlagTypes](cc *cli.Context, name string) T
- func Main(ctx context.Context, c *Commander)
- func Run(ctx context.Context, c *Commander, args []string) error
- type Action
- type AppOptions
- type CommandOptions
- type Commander
- func AddOperation[T any](c *Commander, hook Hook[T], op Operation[T], flags ...Flag) *Commander
- func AddOperationSpec[T any](c *Commander, s *OperationSpec[T]) *Commander
- func MakeCommander() *Commander
- func MakeRootCommander() *Commander
- func OptionsCommander[T any](opts CommandOptions[T]) *Commander
- func Subcommander[T any](c *Commander, hook Hook[T], op Operation[T], flags ...Flag) *Commander
- func (c *Commander) Aliases(a ...string) *Commander
- func (c *Commander) App() *cli.App
- func (c *Commander) Command() *cli.Command
- func (c *Commander) Flags(flags ...Flag) *Commander
- func (c *Commander) Hooks(op ...Action) *Commander
- func (c *Commander) Middleware(mws ...Middleware) *Commander
- func (c *Commander) SetAction(in Action) *Commander
- func (c *Commander) SetAppOptions(opts AppOptions) *Commander
- func (c *Commander) SetBlocking(b bool) *Commander
- func (c *Commander) SetName(n string) *Commander
- func (c *Commander) SetUsage(u string) *Commander
- func (c *Commander) Subcommanders(subs ...*Commander) *Commander
- func (c *Commander) UrfaveCommands(cc ...*cli.Command) *Commander
- func (c *Commander) With(op func(c *Commander)) *Commander
- type Flag
- type FlagOptions
- func (fo *FlagOptions[T]) Add(c *Commander)
- func (fo *FlagOptions[T]) AddAliases(a ...string) *FlagOptions[T]
- func (fo *FlagOptions[T]) Flag() Flag
- func (fo *FlagOptions[T]) SetAliases(a []string) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetDefault(d T) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetDestination(p *T) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetFilePath(s string) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetHidden(b bool) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetName(s ...string) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetRequired(b bool) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetTakesFile(b bool) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetTimestmapLayout(l string) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetUsage(s string) *FlagOptions[T]
- func (fo *FlagOptions[T]) SetValidate(v func(T) error) *FlagOptions[T]
- type FlagTypes
- type Hook
- type Middleware
- type Operation
- type OperationSpec
Constants ¶
This section is empty.
Variables ¶
var ErrNotDefined = errors.New("not defined")
var ErrNotSet = errors.New("not set")
var ErrNotSpecified = errors.New("not specified")
Functions ¶
func GetFlag ¶
GetFlag resolves a flag of the specified name to the type as specified.
This will panic at runtime if the type of the flag specified does not match the type of the flag as defined.
Types ¶
type Action ¶
Action defines the core functionality for a command line entry point or handler, providing both the process' context (managed by the commander,) as well as the pre-operation hooks/validation hooks.
Upon execution these functions get the context processed by the middleware, and the cli package's context. In practice, rather than defining action functions directly, use the AddOperation function to define more strongly typed operations.
type AppOptions ¶
AppOptions provides the structure for construction a cli.App from a commander.
type CommandOptions ¶
type CommandOptions[T any] struct { Name string Usage string Hook Hook[T] Operation Operation[T] Flags []Flag Middleware func(context.Context, T) context.Context Hidden bool Subcommand bool }
CommandOptions are the arguments to create a sub-command in a commander.
func (CommandOptions[T]) Add ¶
func (opts CommandOptions[T]) Add(c *Commander)
Add modifies the provided commander with the options and operation defined by the CommandOptions. Use in combination with the Command.With method.
type Commander ¶
type Commander struct {
// contains filtered or unexported fields
}
Commander provides a chainable and ergonomic way of defining a command.
The Commander objects largely mirror the semantics of the underlying cli library, which handles execution at runtime. Future versions may use different underlying tools.
Commander provides a strong integration with the github.com/tychoish/fun/srv package's service orchestration framework. A service orchestrator is created and runs during the execution of the program and users can add services and rely on Commander to shut down the orchestrator service and wait for running services to return before returning.
Commanders provide an integrated and strongly typed method for defining setup and configuration before running the command itself. For cleanup after the main operation finishes use the github.com/tychoish/fun/srv package's srv.AddCleanupHook() and srv.AddCleanupError().
func AddOperation ¶
AddOperation uses generics to create a hook/operation pair that splits interacting with the cli.Context from the core operation: the Hook creates an object--likely a structure--with the data from the cli args, while the operation can use that structure for the core business logic of the entry point.
An optional number of flags can be added to the command as well.
This form for producing operations separates the parsing of inputs from the operation should serve to make these operations easier to test.
func AddOperationSpec ¶
func AddOperationSpec[T any](c *Commander, s *OperationSpec[T]) *Commander
AddOperationSpec adds an operation to a Commander (and returns the commander.) The OperationSpec makes it possible to define (most) of the operation using strongly typed operations, while passing state directly. A single object of the type T is captured between the function calls.
Because the operation spec builds hooks, middleware, and operations and adds these functions to the convert, it's possible to use AddOperationSpec more than once on a single command. However, there is only ever one action on a commander, so the last non-nil Action specified will be used.
func MakeCommander ¶
func MakeCommander() *Commander
MakeCommander constructs and initializes a command builder object.
func MakeRootCommander ¶
func MakeRootCommander() *Commander
MakeRootCommander constructs a root commander object with basic services configured. From the tychoish/fun/srv package, this pre-populates a base context, shutdown signal, service orchestrator, and cleanup system.
Use MakeCommander to create a commander without these services enabled/running.
func OptionsCommander ¶
func OptionsCommander[T any](opts CommandOptions[T]) *Commander
OptionsCommander creates a new commander as a sub-command returning the new subcommander. Typically you could use this as:
c := cmdr.MakeRootCommand(). Commander(OptionsCommander(optsOne).SetName("one")). Commander(OptionsCommander(optsTwo).SetName("two"))
func Subcommander ¶
Subcommander uses the same AddOperation semantics and form, but creates a new Commander adds the operation to that commander, and then adds the subcommand to the provided commander, returning the new sub-commander.
func (*Commander) App ¶
func (c *Commander) App() *cli.App
App resolves a command object from the commander and the provided options. You must set the context on the Commander using the setContext before calling this command directly.
In most cases you will use the Run() or Main() methods, rather than App() to use the commander, and Run()/Main() provide their own contexts.
func (*Commander) Command ¶
func (c *Commander) Command() *cli.Command
Command resolves the commander into a cli.Command instance. This operation is safe to call more options.
You should only call this function *after* setting the context on the commander.
func (*Commander) Hooks ¶
Hooks adds a new hook to the commander. Hooks are all executed before the command runs. While all hooks run and errors are collected, if any hook errors the action will not execute.
func (*Commander) Middleware ¶
func (c *Commander) Middleware(mws ...Middleware) *Commander
SetMiddlware allows users to modify the context passed to the hooks and actions of a command.
func (*Commander) SetAppOptions ¶
func (c *Commander) SetAppOptions(opts AppOptions) *Commander
SetAppOptions set's the commander's options. This is only used by the top-level root commands.
func (*Commander) SetBlocking ¶
SetBlocking configures the blocking semantics of the command. This setting is only used by root Commander objects. It defaults to false, which means that the action function returns the context passed to services will be canceled.
When true, commanders do not cancel the context after the Action function returns, including for relevant sub commands; instead waiting for any services, managed by the Commanders' orchestrator to return, for the services to signal shutdown, or the context passed to the cmdr.Run or cmdr.Main functions to expire.
func (*Commander) Subcommanders ¶
Subcommanders adds a subcommander, returning the original parent commander object.
func (*Commander) UrfaveCommands ¶
UrfaveCommands directly adds a urfae/cli.Command as a subcommand to the Commander.
Commanders do not modify the raw subcommands added in this way, with one exception. Because cli.Command.Action is untyped and it may be reasonable to add Action functions with different signatures, the Commander will attempt to convert common function to `func(*cli.Context) error` functions and avert the error.
Comander will convert Action functions of following types:
func(context.Context) error func(context.Context, *cli.Context) error func(context.Context) func() error func()
The commander processes the sub commands recursively. All wrapping happens when building the cli.App/cli.Command for the converter, and has limited overhead.
type Flag ¶
type Flag struct {
// contains filtered or unexported fields
}
Flag defines a command line flag, and is produced using the FlagOptions struct by the MakeFlag function.
func MakeFlag ¶
func MakeFlag[T FlagTypes](opts *FlagOptions[T]) Flag
MakeFlag builds a commandline flag instance and validation from a typed flag to options to a flag object for the command line.
type FlagOptions ¶
type FlagOptions[T FlagTypes] struct { Name string Aliases []string Usage string FilePath string Required bool Hidden bool TakesFile bool Validate func(T) error TimestampLayout string // Default values are provided to the parser for many // types. However, slice-types do not support default values. Default T // Destination provides a pointer to a variable where the flag // parser will store the result. The parser only supports this // for a subset of types, and this will panic if the type does // not support this. Destination *T }
FlagOptions provide a generic way to generate a flag object. Methods on FlagOptions are provided for consistency and ergonomics: they are not safe for concurrent use.
func FlagBuilder ¶
func FlagBuilder[T FlagTypes](defaultVal T) *FlagOptions[T]
FlagBuilder provides a constructor that you can use to build a FlagOptions. Provide the constructor with the default value, which you can override later, if needed. Slice values *must* be the empty list.
func (*FlagOptions[T]) Add ¶
func (fo *FlagOptions[T]) Add(c *Commander)
func (*FlagOptions[T]) AddAliases ¶
func (fo *FlagOptions[T]) AddAliases(a ...string) *FlagOptions[T]
func (*FlagOptions[T]) Flag ¶
func (fo *FlagOptions[T]) Flag() Flag
func (*FlagOptions[T]) SetAliases ¶
func (fo *FlagOptions[T]) SetAliases(a []string) *FlagOptions[T]
func (*FlagOptions[T]) SetDefault ¶
func (fo *FlagOptions[T]) SetDefault(d T) *FlagOptions[T]
func (*FlagOptions[T]) SetDestination ¶
func (fo *FlagOptions[T]) SetDestination(p *T) *FlagOptions[T]
func (*FlagOptions[T]) SetFilePath ¶
func (fo *FlagOptions[T]) SetFilePath(s string) *FlagOptions[T]
func (*FlagOptions[T]) SetHidden ¶
func (fo *FlagOptions[T]) SetHidden(b bool) *FlagOptions[T]
func (*FlagOptions[T]) SetName ¶
func (fo *FlagOptions[T]) SetName(s ...string) *FlagOptions[T]
func (*FlagOptions[T]) SetRequired ¶
func (fo *FlagOptions[T]) SetRequired(b bool) *FlagOptions[T]
func (*FlagOptions[T]) SetTakesFile ¶
func (fo *FlagOptions[T]) SetTakesFile(b bool) *FlagOptions[T]
func (*FlagOptions[T]) SetTimestmapLayout ¶
func (fo *FlagOptions[T]) SetTimestmapLayout(l string) *FlagOptions[T]
func (*FlagOptions[T]) SetUsage ¶
func (fo *FlagOptions[T]) SetUsage(s string) *FlagOptions[T]
func (*FlagOptions[T]) SetValidate ¶
func (fo *FlagOptions[T]) SetValidate(v func(T) error) *FlagOptions[T]
type FlagTypes ¶
type FlagTypes interface { string | int | uint | int64 | uint64 | float64 | bool | *time.Time | time.Duration | []string | []int | []int64 }
FlagTypes defines the limited set of types which are supported by the flag parsing system.
type Hook ¶
Hook generates an object, typically a configuration struct, from the cli.Context provided. Hooks are always processed first, before middleware and the main opreation.
type Middleware ¶
Middleware processes the context, attaching timeouts, or values as needed. Middlware is processed after hooks but before the operation.
type OperationSpec ¶
type OperationSpec[T any] struct { // Constructor is required and constructs the output object // for the operation. Constructor Hook[T] // The functions in HookOperations are a sequence of // type-specialized hooks that you can use to post-process the // constructed object, particularly if T is mutable. These run // after the constructor during the "hook" phase of the // commander. HookOperations []Operation[T] // Middlware is optional and makes it possible to attach T to // a context for later use. Middlewares Middleware func(context.Context, T) context.Context // Action, the core action. may be (optionally) specified here as an Operation // or directly on the command. Action Operation[T] }
OperationSpec defines a set of functions that The AddOperationSpec functions use to modify a Commander. Unlike using the commander directly, these operations make it possible to
func SpecBuilder ¶
func SpecBuilder[T any](constr Hook[T]) *OperationSpec[T]
SpecBuilder provides an alternate (chainable) method for building an OperationSpec that is consistent with the Commander interface, and that the compiler can correctly infer the correct type for T.
This builder is not safe for concurrent use.
func (*OperationSpec[T]) Add ¶
func (s *OperationSpec[T]) Add(c *Commander)
Add is an option end of a spec builder chain, that adds the chain to the provided Commander. Use this directly or indirectly with the Commander.With method.
func (*OperationSpec[T]) Hooks ¶
func (s *OperationSpec[T]) Hooks(hook ...Operation[T]) *OperationSpec[T]
func (*OperationSpec[T]) SetAction ¶
func (s *OperationSpec[T]) SetAction(op Operation[T]) *OperationSpec[T]
func (*OperationSpec[T]) SetMiddleware ¶
func (s *OperationSpec[T]) SetMiddleware(mw func(context.Context, T) context.Context) *OperationSpec[T]