app

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFlags

func AddFlags(flagName string, fs *pflag.FlagSet)

AddFlags

func FormatBaseName

func FormatBaseName(basename string) string

FormatBaseName generates a executable file name under different os based on basename

func InitFlags

func InitFlags(flags *pflag.FlagSet)

func PrintSections

func PrintSections(w io.Writer, fs NamedFlagSets, cols int)

PrintSections prints the given names flagsets info in sections

func TerminalSize

func TerminalSize(w io.Writer) (int, int, error)

TerminalSize returns the width and height of the user's process terminal

func WordSepNormalizeFunc

func WordSepNormalizeFunc(_ *pflag.FlagSet, name string) pflag.NormalizedName

WordSepNormalizeFunc changes all flags that contain "_" separators.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App base structure for constructs a cli application

func NewApp

func NewApp(name, basename string, opts ...Option) *App

NewApp creates a new application instance with applicaiton name, binary name, and initial options

func (*App) Run

func (a *App) Run()

Run launches the application

type CmdLineOptioner

type CmdLineOptioner interface {
	Flags() (fs NamedFlagSets)
	// 验证命令行参数是否合法、参数补全、设置默认值等
	Validate() []error
}

CmdLineOptioner abstracts configuration options for reading parameters from command line.

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command a sub command structure of the cli application

func NewCommand

func NewCommand(usage, desc string, opts ...CommandOption) *Command

NewCommand creates a new sub command instance with command name and others options

type CommandOption

type CommandOption func(*Command)

CommandOption optional parameters for initializing the command

func WithCommandOptions

func WithCommandOptions(opt CmdLineOptioner) CommandOption

WithCommandOptions allows the app to read from the flags from command line

func WithCommandRunFunc

func WithCommandRunFunc(runFunc RunCommandFunc) CommandOption

type CompleteableOptions

type CompleteableOptions interface {
	Complete() error
}

CompleteableOptions abstracts options which can be completed.

type NamedFlagSets

type NamedFlagSets struct {
	Order    []string
	FlagSets map[string]*pflag.FlagSet
}

NamedFlagSets stores named flag sets in the order of calling FlagSet

func (*NamedFlagSets) FlagSet

func (nfs *NamedFlagSets) FlagSet(name string) *pflag.FlagSet

FlagSet returns the FlagSets by the given name and adds the flag to the ordered name list if it is not in there

type Option

type Option func(*App)

Option optional parameters for initializing the applicaiton

func WithDefaultValidArgs

func WithDefaultValidArgs() Option

WithDefaultValidArgs default args validation funciton

func WithDescription

func WithDescription(desc string) Option

func WithNoConfig

func WithNoConfig() Option

WithNoConfig not support config flag

func WithOptions

func WithOptions(opt CmdLineOptioner) Option

WithOptions reads from command line

func WithRunFunc

func WithRunFunc(run RunFunc) Option

func WithSilence

func WithSilence() Option

func WithValidArgs

func WithValidArgs(args cobra.PositionalArgs) Option

WithValidArgs validate the non-flag args

type RunCommandFunc

type RunCommandFunc func(args []string) error

RunCommandFunc the app's command startup callback function

type RunFunc

type RunFunc func(basename string) error

RunCommand the app's startup callback function

Jump to

Keyboard shortcuts

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