Documentation ¶
Overview ¶
Package alf is a small, no-frills toolset for creating simple command line applications. It's a thin wrapper around the standard library's flag package with some stuff to make composition and documentation generation easier. See the examples directory for demo usage.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrShowUsage = errors.New("")
ErrShowUsage should be returned or wrapped when you want to show the command's help menu (run its Usage func) even though the user did not specifically request it. It has no text so it doesn't mess up your error message if you're use error wrapping.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { // Description should provide a short summary. Description string // Setup is passed the parent flag set and returns a flag set. Call the // (*flag.FlagSet).Init method to reuse the parent flags and modify it as // needed. You could also just allow the input flagset to pass through. If // you don't want to share any flag data between parent and child, then // create a new flag set. Setup func(parentFlags flag.FlagSet) *flag.FlagSet // Run is a wrapper function that selects the necessary command line inputs, // executes the command and returns any errors. Run func(ctx context.Context) error // contains filtered or unexported fields }
A Command performs a task. A parent Delegator passes control to one of these.
type Delegator ¶
type Delegator struct { // Description should provide a short summary. Description string // Flags collect and share inputs to its sub directives. Flags *flag.FlagSet // Selected is the chosen transfer point of control. Selected Directive // Subs associates a name with another Directive. The name is what to // specify from the command line. Subs map[string]Directive }
A Delegator is a parent to a set of commands. Its sole purpose is to direct traffic to a selected command. It can also collect common flag inputs to pass on to subcommands.
func (*Delegator) DescribeSubcommands ¶
DescribeSubcommands outputs summaries of each subcommand ordered by name.
type Directive ¶
type Directive interface { // Summary provides a short, one-line description. Summary() string // Perform should either choose a subcommand or do a task. Perform(ctx context.Context) error }
Directive is an abstraction for a parent or child command. A parent would delegate to a subcommand, while a subcommand does the actual task.
type Root ¶
type Root struct { *Delegator // PrePerform is an optional function to invoke during Run, after the flags // have been parsed but before a subcommand is chosen. Run will return early // if this function returns an error. PrePerform func(ctx context.Context) error }
Root is your main, top-level command.