Documentation
¶
Overview ¶
Package app defines an application framework used to create application.
Index ¶
- func FormatBaseName(basename string) string
- func PrintFlag(flags *pflag.FlagSet)
- func PrintSections(w io.Writer, fss NamedFlagSets, cols int)
- func WordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName
- type App
- type CliOptions
- type Command
- type CommandOption
- type CompleteableOptions
- type ConfigurableOptions
- type NamedFlagSets
- type Option
- func WithCommands(commands ...*Command) Option
- func WithDefaultValidArgs() Option
- func WithDescription(desc string) Option
- func WithNoConfig() Option
- func WithNoVersion() Option
- func WithOptions(opt CliOptions) Option
- func WithRunFunc(run RunFunc) Option
- func WithSilence() Option
- func WithValidArgs(args cobra.PositionalArgs) Option
- type PrintableOptions
- type RunCommandFunc
- type RunFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FormatBaseName ¶
FormatBaseName is formatted as an executable file name under different operating systems according to the given name.
func PrintSections ¶
func PrintSections(w io.Writer, fss NamedFlagSets, cols int)
PrintSections prints the given names flag sets in sections, with the maximal given column number. If cols is zero, lines are not wrapped.
func WordSepNormalizeFunc ¶
func WordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName
WordSepNormalizeFunc changes all flags that contain "_" separators to "-".
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the main structure of a cli app, it's recommanded that an app be created with the app.NewApp func.
func NewApp ¶
NewApp creates a new application instance based on the given application name, base name, and other options.
func (*App) AddCommand ¶
AddCommand adds sub command to the application.
func (*App) AddCommands ¶
AddCommands adds multiple sub commands to the application.
type CliOptions ¶
type CliOptions interface { //AddFlags(fs *pflag.FlagSet) // AddFlags adds flags to the specified FlagSet object. Flags() NamedFlagSets // get the specified name flagset Validate() []error // check whether option args is validate }
CliOptions abstracts app-defined options for reading parameters from the command line.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command is a sub command structure of a cli application. It is recommended that a command be created with the app.NewCommand() function.
func NewCommand ¶
func NewCommand(usage string, desc string, opts ...CommandOption) *Command
NewCommand creates a new sub command instance based on the given command name and other options.
func (*Command) AddCommand ¶
AddCommand adds sub command to the current command.
func (*Command) AddCommands ¶
AddCommands adds multiple sub commands to the current command.
func (*Command) CobraCommand ¶
type CommandOption ¶
type CommandOption func(*Command)
CommandOption defines optional parameters for initializing the command structure.
func WithCommandOptions ¶
func WithCommandOptions(opt CliOptions) CommandOption
WithCommandOptions to open the application's function to read from the command line.
func WithCommandRunFunc ¶
func WithCommandRunFunc(run RunCommandFunc) CommandOption
WithCommandRunFunc is used to set the application's command startup callback function option.
type CompleteableOptions ¶
type CompleteableOptions interface { // If option args could be completed, the complete it. // Otherwise return error Complete() error }
CompleteableOptions abstracts app-defined options which can be completed.
type ConfigurableOptions ¶
type ConfigurableOptions interface { // ApplyFlags parsing parameters from the command line or configuration file // to the app-defined options struct instance. ApplyFlags() []error }
ConfigurableOptions abstracts app-defined options for reading parameters from a configuration file.
type NamedFlagSets ¶
type NamedFlagSets struct { Order []string // Order is an ordered list of flag set names. FlagSets map[string]*pflag.FlagSet // FlagSets stores the flag sets by name. }
NamedFlagSets stores named flag sets in the order of calling FlagSet.
type Option ¶
type Option func(*App)
Option defines optional parameters for initializing the application structure.
func WithCommands ¶
WithCommands add sub-commands to root command.
func WithDefaultValidArgs ¶
func WithDefaultValidArgs() Option
WithDefaultValidArgs set default validation function to valid valid non-flag arguments.
func WithDescription ¶
WithDescription is used to set the description of the application.
func WithNoConfig ¶
func WithNoConfig() Option
WithNoConfig set the application does not provide "--config" flag.
func WithNoVersion ¶
func WithNoVersion() Option
WithNoVersion set the application does not provide "--version" flag.
func WithOptions ¶
func WithOptions(opt CliOptions) Option
WithOptions to open the application's function to read from the command line or read parameters from the configuration file for every app. Application shoule provide an user-defined structure that implements CliOptions to give cli flag and receive cli params.
func WithRunFunc ¶
WithRunFunc is used to set the application startup callback function option.
func WithSilence ¶
func WithSilence() Option
WithSilence sets the application to silent mode, in which the program startup information, configuration information, and version information are not printed in the console.
func WithValidArgs ¶
func WithValidArgs(args cobra.PositionalArgs) Option
WithValidArgs set the validation function to valid root command arguments.
type PrintableOptions ¶
type PrintableOptions interface {
String() string
}
PrintableOptions abstracts app-defined options which can be printed.
type RunCommandFunc ¶
RunCommandFunc defines the application's command startup callback function.