Documentation
¶
Overview ¶
Package snek provides a simplified, slimmed-down wrapper around the cobra CLI library.
Index ¶
- Constants
- Variables
- func CommandErr(ctx context.Context) io.Writer
- func CommandOut(ctx context.Context) io.Writer
- func DisableColor(ctx context.Context) context.Context
- func ErrorPrefix(ctx context.Context) string
- func Errorf(ctx context.Context, format string, args ...any)
- func Examples(examples ...string) []string
- func ForceColor(ctx context.Context) context.Context
- func IsTerminal(w io.Writer) bool
- func IsUsageError(err error) bool
- func Lines(lines ...string) string
- func NoticePrefix(ctx context.Context) string
- func Noticef(ctx context.Context, format string, args ...any)
- func PanicPrefix(ctx context.Context) string
- func Panicf(ctx context.Context, format string, args ...any)
- func Printf(ctx context.Context, format string, args ...any)
- func UsageError(message string) error
- func UsageErrorf(format string, args ...interface{}) error
- func WarningPrefix(ctx context.Context) string
- func Warningf(ctx context.Context, format string, args ...any)
- type Annotations
- type AppInfo
- type Application
- func (a *Application) Execute(ctx context.Context) (code *StatusCode)
- func (a *Application) SetErr(w io.Writer)
- func (a *Application) SetHelpTemplate(tmpl string)
- func (a *Application) SetOut(w io.Writer)
- func (a *Application) SetPanicTemplate(tmpl string)
- func (a *Application) SetUsageTemplate(tmpl string)
- func (a *Application) SetVersionTemplate(tmpl string)
- type BaseCommand
- func (c *BaseCommand) Commands() Commands
- func (c *BaseCommand) Complete(args []string, toComplete string) Completion
- func (c *BaseCommand) Flags() []*FlagSet
- func (c *BaseCommand) Info() *CommandInfo
- func (c *BaseCommand) PositionalArgs() PositionalArgs
- func (c *BaseCommand) Run(ctx context.Context, args []string) error
- type Command
- type CommandGroup
- type CommandInfo
- type Commands
- type Completer
- type CompleterFunc
- type Completion
- type Flag
- type FlagCompleters
- type FlagSet
- func (fs *FlagSet) Bool(out *bool, name string, value bool, usage string) *Flag
- func (fs *FlagSet) BoolP(out *bool, name, shorthand string, value bool, usage string) *Flag
- func (fs *FlagSet) CompletionFuncs() FlagCompleters
- func (fs *FlagSet) Duration(out *time.Duration, name string, value time.Duration, usage string) *Flag
- func (fs *FlagSet) DurationP(out *time.Duration, name, shorthand string, value time.Duration, usage string) *Flag
- func (fs *FlagSet) FlagSet() *pflag.FlagSet
- func (f *FlagSet) FlagUsages() string
- func (fs *FlagSet) Float64(out *float64, name string, value float64, usage string) *Flag
- func (fs *FlagSet) Float64P(out *float64, name, shorthand string, value float64, usage string) *Flag
- func (f *FlagSet) FormattedFlagUsages(opts *FormatOptions) string
- func (fs *FlagSet) Int(out *int, name string, value int, usage string) *Flag
- func (fs *FlagSet) Int16(out *int16, name string, value int16, usage string) *Flag
- func (fs *FlagSet) Int16P(out *int16, name, shorthand string, value int16, usage string) *Flag
- func (fs *FlagSet) Int32(out *int32, name string, value int32, usage string) *Flag
- func (fs *FlagSet) Int32P(out *int32, name, shorthand string, value int32, usage string) *Flag
- func (fs *FlagSet) Int64(out *int64, name string, value int64, usage string) *Flag
- func (fs *FlagSet) Int64P(out *int64, name, shorthand string, value int64, usage string) *Flag
- func (fs *FlagSet) Int8(out *int8, name string, value int8, usage string) *Flag
- func (fs *FlagSet) Int8P(out *int8, name, shorthand string, value int8, usage string) *Flag
- func (fs *FlagSet) IntP(out *int, name, shorthand string, value int, usage string) *Flag
- func (fs *FlagSet) IntSlice(out *[]int, name string, value []int, usage string) *Flag
- func (fs *FlagSet) IntSliceP(out *[]int, name, shorthand string, value []int, usage string) *Flag
- func (fs *FlagSet) MarkMutuallyExclusive(flags ...*Flag)
- func (f *FlagSet) Name() string
- func (fs *FlagSet) RequireOneOf(flags ...*Flag)
- func (fs *FlagSet) RequireTogether(flags ...*Flag)
- func (fs *FlagSet) String(out *string, name, value, usage string) *Flag
- func (fs *FlagSet) StringP(out *string, name, shorthand, value, usage string) *Flag
- func (fs *FlagSet) StringSlice(out *[]string, name string, value []string, usage string) *Flag
- func (fs *FlagSet) StringSliceP(out *[]string, name, shorthand string, value []string, usage string) *Flag
- func (fs *FlagSet) Uint(out *uint, name string, value uint, usage string) *Flag
- func (fs *FlagSet) Uint16(out *uint16, name string, value uint16, usage string) *Flag
- func (fs *FlagSet) Uint16P(out *uint16, name, shorthand string, value uint16, usage string) *Flag
- func (fs *FlagSet) Uint32(out *uint32, name string, value uint32, usage string) *Flag
- func (fs *FlagSet) Uint32P(out *uint32, name, shorthand string, value uint32, usage string) *Flag
- func (fs *FlagSet) Uint64(out *uint64, name string, value uint64, usage string) *Flag
- func (fs *FlagSet) Uint64P(out *uint64, name, shorthand string, value uint64, usage string) *Flag
- func (fs *FlagSet) Uint8(out *uint8, name string, value uint8, usage string) *Flag
- func (fs *FlagSet) Uint8P(out *uint8, name, shorthand string, value uint8, usage string) *Flag
- func (fs *FlagSet) UintP(out *uint, name, shorthand string, value uint, usage string) *Flag
- func (fs *FlagSet) UintSlice(out *[]uint, name string, value []uint, usage string) *Flag
- func (fs *FlagSet) UintSliceP(out *[]uint, name, shorthand string, value []uint, usage string) *Flag
- func (fs *FlagSet) Var(name string, value pflag.Value, usage string) *Flag
- func (fs *FlagSet) VarP(value pflag.Value, name, shorthand, usage string) *Flag
- type FormatOptions
- type PanicError
- type PositionalArgs
- type StatusCode
Constants ¶
const ( ExitSuccess = 0 ExitError = 1 ExitPanic = 2 )
Variables ¶
var ( // ArbitraryArgs is used to represent a command that can take any number of // positional arguments. ArbitraryArgs = positionalArgs(cobra.ArbitraryArgs) // NoArgs is used to represent a command that takes no positional arguments. NoArgs = positionalArgs(cobra.NoArgs) )
var ( FormatLink = ansi.Format(ansi.Underline, ansi.FGWhite) FormatHeading = ansi.FGYellow FormatFlag = ansi.FGCyan FormatArg = ansi.Format(ansi.Bold, ansi.FGWhite) FormatCommand = ansi.FGGreen FormatKeyword = ansi.FGCyan FormatStrong = ansi.Bold FormatCall = ansi.Format(ansi.FGWhite, ansi.Bold) FormatQuote = ansi.FGGray FormatError = ansi.FGRed FormatWarning = ansi.FGYellow FormatInfo = ansi.FGBlue )
var CompletionError = completion(func() ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveError })
CompletionError indicates that an error occurred while trying to provide completions, and the shell should not provide any completions.
var NoCompletion = CompleteNoFiles()
Functions ¶
func CommandErr ¶
CommandErr returns the stderr writer from the context, if available, or os.Stderr.
func CommandOut ¶
CommandOut returns the stdout writer from the context, if available, or os.Stdout.
func DisableColor ¶
DisableColor disables color output for the given context.
func ErrorPrefix ¶
ErrorPrefix returns the error prefix for the given context. If no application name can be extracted from the context, a default one will be provided.
func Errorf ¶
Errorf writes a formatted string to the output stored within the context, or standard error if unavailable, with an error prefix.
func ForceColor ¶
ForceColor forces color output for the given context.
func IsTerminal ¶
IsTerminal returns true if the writer is a terminal.
func IsUsageError ¶
IsUsageError returns true if the error is a usage error.
func NoticePrefix ¶
NoticePrefix returns the notice prefix for the given context.
func Noticef ¶
Noticef writes a formatted string to the output stored within the context, or standard output if unavailable, with a notice prefix.
func PanicPrefix ¶
func Panicf ¶
Panicf writes a formatted string to the output stored within the context, or standard error if unavailable, with a panic prefix.
func Printf ¶
Printf writes a formatted string to the output stored within the context, or standard output if unavailable.
func UsageError ¶
UsageError returns an error that should be displayed to the user as a usage error. This will be accompanied with the usage instructions for the command.
func UsageErrorf ¶
UsageErrorf returns an error that should be displayed to the user as a usage error. This will be accompanied with the usage instructions for the command.
func WarningPrefix ¶
WarningPrefix returns the warning prefix for the given context.
Types ¶
type Annotations ¶
Annotations is a map of string keys to string values that can be used to annotate a command.
type AppInfo ¶
type AppInfo struct {
// Name is the name of the application.
Name string
// Website is the URL for the contact person or organization responsible for
// the application.
Website string
// RepositoryURL is the URL for the repository for the application.
RepositoryURL string
// ReportIssueURL is the URL for the issue tracker for the application.
ReportIssueURL string
// DocsURL is the URL for the documentation for the application, which may be
// different from the base Website URL.
DocsURL string
// KeyTerms are words that are important in the command, and will be
// highlighted in the help output. (Optional)
KeyTerms []string
// Variables are words that are variable inputs in the command, and will be
// highlighted in the help output. (Optional)
Variables []string
// UserData is an optional (unspecified) piece of data that can be provided
// to the application. Default is nil.
UserData any
}
type Application ¶
type Application struct {
// contains filtered or unexported fields
}
Application is a wrapper around a cobra command that represents an application.
func NewApplication ¶
func NewApplication(root Command, appInfo *AppInfo) *Application
NewApplication creates a new application from the given root command. This function will panic if any of the commands in the root do not define a Command.Info function that returns non-nil information.
func (*Application) Execute ¶
func (a *Application) Execute(ctx context.Context) (code *StatusCode)
Execute runs the application with the given context.
func (*Application) SetErr ¶
func (a *Application) SetErr(w io.Writer)
SetErr sets the error writer for the application.
func (*Application) SetHelpTemplate ¶
func (a *Application) SetHelpTemplate(tmpl string)
SetHelpTemplate sets the template that will be used to render the help output.
func (*Application) SetOut ¶
func (a *Application) SetOut(w io.Writer)
SetOut sets the out writer for the application.
func (*Application) SetPanicTemplate ¶
func (a *Application) SetPanicTemplate(tmpl string)
SetPanicTemplate sets the template that will be used to render the panic output.
This function will panic if the template is invalid.
func (*Application) SetUsageTemplate ¶
func (a *Application) SetUsageTemplate(tmpl string)
SetUsageTemplate sets the template that will be used to render the usage output.
func (*Application) SetVersionTemplate ¶
func (a *Application) SetVersionTemplate(tmpl string)
SetVersionTemplate sets the template that will be used to render the version output.
type BaseCommand ¶
type BaseCommand struct{}
BaseCommand is a simple implementation of the Command interface that must be embedded in all command implementations.
func (*BaseCommand) Commands ¶
func (c *BaseCommand) Commands() Commands
func (*BaseCommand) Complete ¶
func (c *BaseCommand) Complete(args []string, toComplete string) Completion
func (*BaseCommand) Flags ¶
func (c *BaseCommand) Flags() []*FlagSet
func (*BaseCommand) Info ¶
func (c *BaseCommand) Info() *CommandInfo
func (*BaseCommand) PositionalArgs ¶
func (c *BaseCommand) PositionalArgs() PositionalArgs
type Command ¶
type Command interface {
// Info returns information about the command.
Info() *CommandInfo
// Run executes the command with the given arguments.
Run(ctx context.Context, args []string) error
// Complete returns a list of possible completions for the current set of args
// and argument being completed.
Complete(args []string, toComplete string) Completion
// PositionalArgs returns information about the positional arguments for this
// command.
PositionalArgs() PositionalArgs
// Flags returns a list of flags that can be used with this command.
Flags() []*FlagSet
// Commands returns a list of sub-commands that can be run under this
// command.
Commands() Commands
// contains filtered or unexported methods
}
Command is the primary entry-point interface for use in the snek package. This will be translated into a cobra.Command for use in the CLI.
type CommandGroup ¶
type CommandGroup []Command
func (*CommandGroup) Add ¶
func (c *CommandGroup) Add(commands ...Command)
type CommandInfo ¶
type CommandInfo struct {
// Use is an example usage of the command.
// This must be prefixed with the command name.
Use string
Aliases []string
Summary string
Description string
Examples []string
// KeyTerms are words that are important in the command, and will be
// highlighted in the help output. (Optional)
KeyTerms []string
// Variables are words that are variable inputs in the command, and will be
// highlighted in the help output. (Optional)
Variables []string
Annotations Annotations
Version string
Hidden bool
ShowCursor bool
}
type Commands ¶
type Commands map[string]*CommandGroup
func (*Commands) Group ¶
func (c *Commands) Group(name string) *CommandGroup
type Completer ¶
type Completer interface {
// Complete returns a list of possible completions for the current set of args
// and argument being completed.
Complete(args []string, toComplete string) Completion
}
Completer is an interface that can be implemented by commands to provide custom completions for arguments.
type CompleterFunc ¶
type CompleterFunc func(args []string, toComplete string) Completion
CompleterFunc is a function that can be used to implement the Completer interface.
func (CompleterFunc) Complete ¶
func (f CompleterFunc) Complete(args []string, toComplete string) Completion
Complete calls the underlying function to provide completions.
type Completion ¶
type Completion interface {
// contains filtered or unexported methods
}
Completion is an interface that can be implemented by commands to provide custom completions for arguments.
func CompleteDirs ¶
func CompleteDirs(completions ...string) Completion
CompleteDirs indicates that only directory names should be provided in file completion. To request directory names within another directory, the returned completions should specify the directory within which to search. The BashCompSubdirsInDir annotation can be used to obtain the same behavior but only for flags.
func CompleteFileExts ¶
func CompleteFileExts(extensions ...string) Completion
CompleteFileExts indicates that the provided completions should be used as file extension filters.
For flags, using Command.MarkFlagFilename() and Command.MarkPersistentFlagFilename() is a shortcut to using this directive explicitly. The BashCompFilenameExt annotation can also be used to obtain the same behavior for flags.
func CompleteNoFiles ¶
func CompleteNoFiles(completions ...string) Completion
CompleteNoFiles indicates that the shell should not provide file completion even when no completion is provided.
func CompleteWithoutSpace ¶
func CompleteWithoutSpace(completions ...string) Completion
CompleteWithoutSpace indicates that the shell should not add a space after the completion even if there is only a single completion provided.
type Flag ¶
type Flag struct {
// contains filtered or unexported fields
}
Flag is an interface for setting options on a flag.
func (*Flag) MarkDeprecated ¶
MarkDeprecated marks the flag as deprecated.
func (*Flag) MarkRequired ¶
MarkRequired marks the flag as required.
func (*Flag) SetCompleter ¶
SetCompleter sets the completer for the flag.
type FlagCompleters ¶
FlagCompleters is a map of completion functions for flags.
type FlagSet ¶
type FlagSet struct {
// contains filtered or unexported fields
}
FlagSet is a wrapper around pflag.FlagSet that provides a more fluent API for defining flags.
func NewFlagSet ¶
NewFlagSet creates a new FlagSet with the specified name.
func (*FlagSet) Bool ¶
Bool defines a bool flag with the specified name, default value, and usage string.
func (*FlagSet) BoolP ¶
BoolP defines a bool flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) CompletionFuncs ¶
func (fs *FlagSet) CompletionFuncs() FlagCompleters
CompletionFuncs returns the completion functions for the flags in the FlagSet.
func (*FlagSet) Duration ¶
func (fs *FlagSet) Duration(out *time.Duration, name string, value time.Duration, usage string) *Flag
Duration defines a time.Duration flag with the specified name, default value, and usage string.
func (*FlagSet) DurationP ¶
func (fs *FlagSet) DurationP(out *time.Duration, name, shorthand string, value time.Duration, usage string) *Flag
DurationP defines a time.Duration flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) FlagUsages ¶
FlagUsages returns a string containing the usage information for the flags in the FlagSet.
func (*FlagSet) Float64 ¶
Float64 defines a float64 flag with the specified name, default value, and usage string.
func (*FlagSet) Float64P ¶
func (fs *FlagSet) Float64P(out *float64, name, shorthand string, value float64, usage string) *Flag
Float64P defines a float64 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) FormattedFlagUsages ¶
func (f *FlagSet) FormattedFlagUsages(opts *FormatOptions) string
FormattedFlagUsages returns a string containing the usage information for the flags in the FlagSet, formatted according to the provided options.
func (*FlagSet) Int ¶
Int defines an int flag with the specified name, default value, and usage string.
func (*FlagSet) Int16 ¶
Int16 defines an int16 flag with the specified name, default value, and usage string.
func (*FlagSet) Int16P ¶
Int16P defines an int16 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Int32 ¶
Int32 defines an int32 flag with the specified name, default value, and usage string.
func (*FlagSet) Int32P ¶
Int32P defines an int32 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Int64 ¶
Int64 defines an int64 flag with the specified name, default value, and usage string.
func (*FlagSet) Int64P ¶
Int64P defines an int64 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Int8 ¶
Int8 defines an int8 flag with the specified name, default value, and usage string.
func (*FlagSet) Int8P ¶
Int8P defines an int8 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) IntP ¶
IntP defines an int flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) IntSlice ¶
IntSlice defines an int slice flag with the specified name, default value, and usage string.
func (*FlagSet) IntSliceP ¶
IntSliceP defines an int slice flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) MarkMutuallyExclusive ¶
MarkMutuallyExclusive marks the flags as mutually exclusive.
func (*FlagSet) RequireOneOf ¶
RequireOneOf marks the flags as requiring at least one of them to be set.
func (*FlagSet) RequireTogether ¶
RequireTogether marks the flags as required together.
func (*FlagSet) String ¶
String defines a string flag with the specified name, default value, and usage string.
func (*FlagSet) StringP ¶
StringP defines a string flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) StringSlice ¶
StringSlice defines a string slice flag with the specified name, default value, and usage string.
func (*FlagSet) StringSliceP ¶
func (fs *FlagSet) StringSliceP(out *[]string, name, shorthand string, value []string, usage string) *Flag
StringSliceP defines a string slice flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Uint ¶
Uint defines a uint flag with the specified name, default value, and usage string.
func (*FlagSet) Uint16 ¶
Uint16 defines a uint16 flag with the specified name, default value, and usage string.
func (*FlagSet) Uint16P ¶
Uint16P defines a uint16 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Uint32 ¶
Uint32 defines a uint32 flag with the specified name, default value, and usage string.
func (*FlagSet) Uint32P ¶
Uint32P defines a uint32 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Uint64 ¶
Uint64 defines a uint64 flag with the specified name, default value, and usage string.
func (*FlagSet) Uint64P ¶
Uint64P defines a uint64 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) Uint8 ¶
Uint8 defines a uint8 flag with the specified name, default value, and usage string.
func (*FlagSet) Uint8P ¶
Uint8P defines a uint8 flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) UintP ¶
UintP defines a uint flag with the specified name, shorthand, default value, and usage string.
func (*FlagSet) UintSliceP ¶
type FormatOptions ¶
type FormatOptions struct {
ArgFormat ansi.Display
FlagFormat ansi.Display
DeprecatedFormat ansi.Display
}
FormatOptions is used to configure the formatting of the flag usage.
type PanicError ¶
type PanicError string
PanicError is an error that is used to represent a panic in the application.
func (PanicError) Error ¶
func (e PanicError) Error() string
Error returns the string representation of the panic error.
type PositionalArgs ¶
type PositionalArgs interface {
// contains filtered or unexported methods
}
PositionalArgs is used to represent positional argument requirements for a command.
func Condition ¶
func Condition(cond func(args []string) error) PositionalArgs
Condition returns a PositionalArgs that requires the condition to be met.
func ExactArgs ¶
func ExactArgs(n int) PositionalArgs
ExactArgs returns a PositionalArgs that requires exactly n arguments.
func MatchAll ¶
func MatchAll(pargs ...PositionalArgs) PositionalArgs
MinimumNArgs returns a PositionalArgs that requires at least n arguments.
func MaximumNArgs ¶
func MaximumNArgs(n int) PositionalArgs
MaximumNArgs returns a PositionalArgs that requires at most n arguments.
func MinimumNArgs ¶
func MinimumNArgs(n int) PositionalArgs
MinimumNArgs returns a PositionalArgs that requires at least n arguments.
func RangeArgs ¶
func RangeArgs(min, max int) PositionalArgs
RangeArgs returns a PositionalArgs that requires between min and max
type StatusCode ¶
StatusCode represents the response from the Application.
This provides the exact underlying error that the system encountered, and provides a suggestion on the exit status-code, with an optional helper func to exit the program with.
func (*StatusCode) Error ¶
func (s *StatusCode) Error() string