Documentation ¶
Index ¶
- Constants
- Variables
- func AddOnConfigLoadedListener(c ConfigReloaded)
- func Clone(fromValue, toValue interface{}) interface{}
- func DumpAsString() (str string)
- func EnsureDir(dir string) (err error)
- func Exec(rootCmd *RootCommand) (err error)
- func FileExists(name string) bool
- func Get(key string) interface{}
- func GetBool(key string) bool
- func GetBoolP(prefix, key string) bool
- func GetCurrentDir() string
- func GetDebugMode() bool
- func GetExcutableDir() string
- func GetInt(key string) int
- func GetInt64(key string) int64
- func GetInt64P(prefix, key string) int64
- func GetIntP(prefix, key string) int
- func GetPredefinedLocations() []string
- func GetQuietMode() bool
- func GetStrictMode() bool
- func GetString(key string) string
- func GetStringP(prefix, key string) string
- func GetStringSlice(key string) []string
- func GetStringSliceP(prefix, key string) []string
- func GetUint(key string) uint
- func GetUint64(key string) uint64
- func GetUint64P(prefix, key string) uint64
- func GetUintP(prefix, key string) uint
- func GetUsedConfigFile() string
- func GetUsedConfigSubDir() string
- func GetVerboseMode() bool
- func InternalExecFor(rootCmd *RootCommand, args []string) (err error)
- func LoadConfigFile(file string) (err error)
- func RemoveOnConfigLoadedListener(c ConfigReloaded)
- func Set(key string, val interface{})
- func SetCustomShowBuildInfo(fn func())
- func SetCustomShowVersion(fn func())
- func SetInternalOutputStreams(out, err *bufio.Writer)
- func SetNx(key string, val interface{})
- func SetOnConfigLoadedListener(c ConfigReloaded, enabled bool)
- func SetPredefinedLocations(locations []string)
- func StripOrderPrefix(s string) string
- func WalkAllCommands(walk func(cmd *Command, index int) (err error)) (err error)
- type BaseOpt
- func (s *BaseOpt) GetDescZsh() (desc string)
- func (s *BaseOpt) GetLongTitleNamesArray() []string
- func (s *BaseOpt) GetShortTitleNamesArray() []string
- func (s *BaseOpt) GetTitleFlagNames() string
- func (s *BaseOpt) GetTitleFlagNamesBy(delimChar string) string
- func (s *BaseOpt) GetTitleFlagNamesByMax(delimChar string, maxCount int) string
- func (s *BaseOpt) GetTitleName() string
- func (s *BaseOpt) GetTitleNames() string
- func (s *BaseOpt) GetTitleNamesArray() []string
- func (s *BaseOpt) GetTitleNamesBy(delimChar string) string
- func (s *BaseOpt) GetTitleZshFlagName() (str string)
- func (s *BaseOpt) GetTitleZshFlagNames(delimChar string) (str string)
- func (s *BaseOpt) GetTitleZshFlagNamesArray() (ary []string)
- func (c *BaseOpt) HasParent() bool
- type Command
- func (c *Command) GetExpandableNames() string
- func (c *Command) GetExpandableNamesArray() []string
- func (c *Command) GetName() string
- func (c *Command) GetParentName() string
- func (c *Command) GetQuotedGroupName() string
- func (c *Command) GetRoot() *RootCommand
- func (c *Command) GetSubCommandNamesBy(delimChar string) string
- func (c *Command) IsRoot() bool
- func (c *Command) PrintHelp(justFlags bool)
- func (c *Command) PrintVersion()
- type ConfigReloaded
- type Copier
- type CopierImpl
- type Flag
- type Options
- func (s *Options) DumpAsString() (str string)
- func (s *Options) Get(key string) interface{}
- func (s *Options) GetBool(key string) (ret bool)
- func (s *Options) GetInt(key string) (ir int64)
- func (s *Options) GetString(key string) (ret string)
- func (s *Options) GetStringSlice(key string) (ir []string)
- func (s *Options) GetUint(key string) (ir uint64)
- func (s *Options) LoadConfigFile(file string) (err error)
- func (s *Options) Reset()
- func (s *Options) Set(key string, val interface{})
- func (s *Options) SetNx(key string, val interface{})
- type Painter
- type RootCommand
Constants ¶
const ( // UnsortedGroup for commands and flags UnsortedGroup = "zzzz.unsorted" // SysMgmtGroup for commands and flags SysMgmtGroup = "zzz9.Misc" )
const ( // AppName const AppName = "cmdr" // main app-name // Version const Version = "0.2.9" // version name // VersionInt const VersionInt = 0x000209 // using as )
const ( FgBlack = 30 FgRed = 31 FgGreen = 32 FgYellow = 33 FgBlue = 34 FgMagenta = 35 FgCyan = 36 FgLightGray = 37 FgDarkGray = 90 FgLightRed = 91 FgLightGreen = 92 FgLightYellow = 93 FgLightBlue = 94 FgLightMagenta = 95 FgLightCyan = 96 FgWhite = 97 BgNormal = 0 BgBoldOrBright = 1 BgDim = 2 BgItalic = 3 BgUnderline = 4 BgUlink = 5 BgHidden = 8 DarkColor = FgLightGray )
Variables ¶
var ( // GormDefaultCopier used for gorm GormDefaultCopier = &CopierImpl{true, true, true} // StandardCopier is a normal copier StandardCopier = &CopierImpl{false, false, false} )
var ( // EnableVersionCommands supports injecting the default `--version` flags and commands EnableVersionCommands = true // EnableHelpCommands supports injecting the default `--help` flags and commands EnableHelpCommands = true // EnableVerboseCommands supports injecting the default `--verbose` flags and commands EnableVerboseCommands = true // EnableGenerateCommands supports injecting the default `generate` commands and subcommands EnableGenerateCommands = true // RxxtPrefix create a top-level namespace, which contains all normalized `Flag`s. RxxtPrefix = []string{"app"} // EnvPrefix attaches a prefix to key to retrieve the option value. EnvPrefix = []string{"CMDR"} CurrentDescColor = FgDarkGray CurrentDefaultValueColor = FgDarkGray CurrentGroupTitleColor = DarkColor // ErrShouldBeStopException tips `Exec()` cancelled the following actions after `PreAction()` ErrShouldBeStopException = errors.New("should be stop right now") )
Functions ¶
func AddOnConfigLoadedListener ¶
func AddOnConfigLoadedListener(c ConfigReloaded)
AddOnConfigLoadedListener add an functor on config loaded and merged
func Clone ¶ added in v0.2.3
func Clone(fromValue, toValue interface{}) interface{}
Clone deep copy source to target
func FileExists ¶
FileExists returns the existence of an directory or file
func Get ¶
func Get(key string) interface{}
Get returns the generic value of an `Option` key. Such as: ```golang cmdr.Get("app.logger.level") => 'DEBUG',... ```
func GetCurrentDir ¶
func GetCurrentDir() string
GetCurrentDir returns the current working directory
func GetDebugMode ¶ added in v0.2.5
func GetDebugMode() bool
GetDebugMode returns the flag value of `--debug`/`-D`
func GetExcutableDir ¶
func GetExcutableDir() string
GetExcutableDir returns the executable file directory
func GetPredefinedLocations ¶ added in v0.2.5
func GetPredefinedLocations() []string
GetPredefinedLocations return the searching locations for loading config files.
func GetQuietMode ¶ added in v0.2.5
func GetQuietMode() bool
GetDebugMode returns the flag value of `--quiet`/`-q`
func GetStrictMode ¶ added in v0.2.5
func GetStrictMode() bool
GetStrictMode enables error when opt value missed. such as: xxx a b --prefix” => error: prefix opt has no value specified. xxx a b --prefix'/' => ok.
ENV: use `CMDR_APP_STRICT_MODE=true` to enable strict-mode. NOTE: `CMDR_APP_` prefix could be set by user (via: `EnvPrefix` && `RxxtPrefix`).
the flag value of `--strict-mode`.
func GetStringP ¶ added in v0.2.3
GetStringP returns the string value of an `Option` key.
func GetStringSlice ¶
GetStringSlice returns the string slice value of an `Option` key.
func GetStringSliceP ¶ added in v0.2.3
GetStringSliceP returns the string slice value of an `Option` key.
func GetUint64P ¶ added in v0.2.3
GetUint64P returns the uint64 value of an `Option` key.
func GetUsedConfigFile ¶ added in v0.2.3
func GetUsedConfigFile() string
GetUsedConfigFile returns the main config filename (generally it's `<appname>.yml`)
func GetUsedConfigSubDir ¶ added in v0.2.3
func GetUsedConfigSubDir() string
GetUsedConfigSubDir returns the sub-directory `conf.d` of config files
func GetVerboseMode ¶ added in v0.2.5
func GetVerboseMode() bool
GetDebugMode returns the flag value of `--verbose`/`-v`
func InternalExecFor ¶ added in v0.2.3
func InternalExecFor(rootCmd *RootCommand, args []string) (err error)
InternalExecFor is an internal helper, esp for debugging
func LoadConfigFile ¶
LoadConfigFile Load a yaml config file and merge the settings into `rxxtOptions` and load files in the `conf.d` child directory too.
func RemoveOnConfigLoadedListener ¶
func RemoveOnConfigLoadedListener(c ConfigReloaded)
RemoveOnConfigLoadedListener remove an functor on config loaded and merged
func Set ¶ added in v0.2.3
func Set(key string, val interface{})
Set set the value of an `Option` key. ```golang cmdr.Set("app.logger.level", "DEBUG") cmdr.Set("app.ms.tags.port", 8500) ... ```
func SetCustomShowBuildInfo ¶ added in v0.2.3
func SetCustomShowBuildInfo(fn func())
SetCustomShowBuildInfo supports your `ShowBuildInfo()` instead of internal `showBuildInfo()`
func SetCustomShowVersion ¶ added in v0.2.3
func SetCustomShowVersion(fn func())
SetCustomShowVersion supports your `ShowVersion()` instead of internal `showVersion()`
func SetInternalOutputStreams ¶ added in v0.2.3
SetInternalOutputStreams sets the internal output streams for debugging
func SetNx ¶ added in v0.2.3
func SetNx(key string, val interface{})
SetNx but without prefix auto-wrapped. `rxxtPrefix` is a string slice to define the prefix string array, default is ["app"]. So, cmdr.Set("debug", true) will put an real entry with (`app.debug`, true).
func SetOnConfigLoadedListener ¶
func SetOnConfigLoadedListener(c ConfigReloaded, enabled bool)
SetOnConfigLoadedListener enable/disable an functor on config loaded and merged
func SetPredefinedLocations ¶ added in v0.2.5
func SetPredefinedLocations(locations []string)
SetPredefinedLocations to customize the searching locations for loading config files. It MUST be invoked before `cmdr.Exec`. Such as: ```go
SetPredefinedLocations([]string{"./config", "~/.config/cmdr/", "$GOPATH/running-configs/cmdr"})
```
func StripOrderPrefix ¶ added in v0.2.9
Types ¶
type BaseOpt ¶
type BaseOpt struct { Name string // single char. example for flag: "a" -> "-a" // Short rune. Short string // word string. example for flag: "addr" -> "--addr" Full string // more synonyms Aliases []string // group name Group string // to-do: Toggle Group ToggleGroup string Flags []*Flag Description string LongDescription string Examples string Hidden bool DefaultValuePlaceholder string // cmd 是 flag 被识别时已经得到的子命令 // return: ErrShouldBeStopException will break the following flow and exit right now Action func(cmd *Command, args []string) (err error) // contains filtered or unexported fields }
BaseOpt is base of `Command`, `Flag`
func (*BaseOpt) GetLongTitleNamesArray ¶ added in v0.2.3
GetLongTitleNamesArray temp
func (*BaseOpt) GetShortTitleNamesArray ¶ added in v0.2.3
GetShortTitleNamesArray temp
func (*BaseOpt) GetTitleFlagNames ¶
GetTitleFlagNames temp
func (*BaseOpt) GetTitleFlagNamesBy ¶
GetTitleFlagNamesBy temp
func (*BaseOpt) GetTitleFlagNamesByMax ¶
GetTitleFlagNamesByMax temp
func (*BaseOpt) GetTitleNamesArray ¶
GetTitleNamesArray temp
func (*BaseOpt) GetTitleNamesBy ¶
GetTitleNamesBy temp
func (*BaseOpt) GetTitleZshFlagName ¶
GetTitleZshFlagName temp
func (*BaseOpt) GetTitleZshFlagNames ¶
GetTitleZshFlagNames temp
func (*BaseOpt) GetTitleZshFlagNamesArray ¶
GetTitleZshFlagNamesArray temp
type Command ¶
type Command struct { BaseOpt SubCommands []*Command // return: ErrShouldBeStopException will break the following flow and exit right now PreAction func(cmd *Command, args []string) (err error) // PostAction will be run after Action() invoked. PostAction func(cmd *Command, args []string) // be shown at tail of command usages line. Such as for TailPlaceHolder="<host-fqdn> <ipv4/6>": // austr dns add <host-fqdn> <ipv4/6> [Options] [Parent/Global Options] TailPlaceHolder string // contains filtered or unexported fields }
Command holds the structure of commands and subcommands
func (*Command) GetExpandableNames ¶
GetExpandableNames returns the names comma splitted string.
func (*Command) GetExpandableNamesArray ¶
GetExpandableNamesArray returns the names array of command, includes short name and long name.
func (*Command) GetParentName ¶
GetParentName returns the owner command name
func (*Command) GetQuotedGroupName ¶
GetQuotedGroupName returns the group name quoted string.
func (*Command) GetRoot ¶
func (c *Command) GetRoot() *RootCommand
GetRoot returns the `RootCommand`
func (*Command) GetSubCommandNamesBy ¶
GetSubCommandNamesBy returns the joint string of subcommands
func (*Command) PrintVersion ¶
func (c *Command) PrintVersion()
PrintVersion prints versions information
type ConfigReloaded ¶
type ConfigReloaded interface {
OnConfigReloaded()
}
ConfigReloaded for config reloaded
type Copier ¶ added in v0.2.3
type Copier interface {
Copy(toValue interface{}, fromValue interface{}, ignoreNames ...string) (err error)
}
Copier interface
type CopierImpl ¶ added in v0.2.3
type CopierImpl struct { KeepIfFromIsNil bool // 源字段值为nil指针时,目标字段的值保持不变 ZeroIfEqualsFrom bool // 源和目标字段值相同时,目标字段被清除为未初始化的零值 KeepIfFromIsZero bool // 源字段值为未初始化的零值时,目标字段的值保持不变 // 此条尚未实现 }
CopierImpl impl
func (*CopierImpl) Copy ¶ added in v0.2.3
func (s *CopierImpl) Copy(toValue interface{}, fromValue interface{}, ignoreNames ...string) (err error)
Copy copy things
type Flag ¶
type Flag struct { BaseOpt // default value for flag DefaultValue interface{} ValidArgs []string Required bool }
Flag means a flag, a option, or a opt.
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options is a holder of all options
func NewOptionsWith ¶
NewOptionsWith returns an `Options` structure pointer
func (*Options) DumpAsString ¶
DumpAsString for debugging.
func (*Options) Get ¶
Get an `Option` by key string, eg: ```golang cmdr.Get("app.logger.level") => 'DEBUG',... ```
func (*Options) GetStringSlice ¶
GetStringSlice returns the string slice value of an `Option` key.
func (*Options) LoadConfigFile ¶
LoadConfigFile Load a yaml config file and merge the settings into `rxxtOptions` and load files in the `conf.d` child directory too.
func (*Options) Reset ¶
func (s *Options) Reset()
Reset the exists `Options`, so that you could follow a `LoadConfigFile()` with it.
type Painter ¶ added in v0.2.9
type Painter interface { Printf(fmtStr string, args ...interface{}) FpPrintHeader(command *Command) FpPrintHelpTailLine(command *Command) FpUsagesTitle(command *Command, title string) FpUsagesLine(command *Command, fmt, appName, cmdList, cmdsTitle, tailPlaceHolder string) FpDescTitle(command *Command, title string) FpDescLine(command *Command) FpExamplesTitle(command *Command, title string) FpExamplesLine(command *Command) FpCommandsTitle(command *Command) FpCommandsGroupTitle(group string) FpCommandsLine(command *Command) FpFlagsTitle(command *Command, flag *Flag, title string) FpFlagsGroupTitle(group string) FpFlagsLine(command *Command, flag *Flag, defValStr string) Flush() }
type RootCommand ¶
type RootCommand struct { Command AppName string Version string VersionInt uint32 Copyright string Author string Header string // using `Header` for header and ignore built with `Copyright` and `Author`, and no usage lines too. // contains filtered or unexported fields }
RootCommand holds some application information