# Helpers

Cmdr 也包含一些辅助函数(Helpers functions)。

# 对 Option Store 的操作

# func AsJSON (opens new window)

func AsJSON() (b []byte)
1

AsJSON returns a json string bytes about all options

# func AsJSONExt (opens new window)

func AsJSONExt() (b []byte, err error)
1

AsJSONExt returns a json string bytes about all options

# func AsToml (opens new window)

func AsToml() (b []byte)
1

AsToml returns a toml string bytes about all options

# func AsTomlExt (opens new window)

func AsTomlExt() (b []byte, err error)
1

AsTomlExt returns a toml string bytes about all options

# func AsYaml (opens new window)

func AsYaml() (b []byte)
1

AsYaml returns a yaml string bytes about all options

# func AsYamlExt (opens new window)

func AsYamlExt() (b []byte, err error)
1

AsYamlExt returns a yaml string bytes about all options

# func DeleteKey (opens new window)

func DeleteKey(key string)
1

DeleteKey deletes a key from cmdr options store

# func GetHierarchyList (opens new window)

func GetHierarchyList() map[string]interface{}
1

GetHierarchyList returns the hierarchy data

# func GetSectionFrom (opens new window)

func GetSectionFrom(sectionKeyPath string, holder interface{}) (err error)
1

GetSectionFrom returns error while cannot yaml Marshal and Unmarshal cmdr.GetSectionFrom(sectionKeyPath, &holder) could load all sub-tree nodes from sectionKeyPath and transform them into holder structure, such as:

type ServerConfig struct {
  Port int
  HttpMode int
  EnableTls bool
}
var serverConfig = new(ServerConfig)
cmdr.GetSectionFrom("server", &serverConfig)
assert serverConfig.Port == 7100
1
2
3
4
5
6
7
8

# func HasKey (opens new window)

func HasKey(key string) (ok bool)
1

HasKey detects whether a key exists in cmdr options store or not

# func MergeWith (opens new window)

func MergeWith(m map[string]interface{}) (err error)
1

MergeWith will merge a map recursive. You could merge a yaml/json/toml options into cmdr Hierarchy Options.

# func ResetOptions (opens new window)

func ResetOptions()
1

ResetOptions to reset the exists Options, so that you could follow a LoadConfigFile() with it.

# func SaveAsJSON (opens new window)

func SaveAsJSON(filename string) (err error)
1

SaveAsJSON to Save all config entries as a json file

# func SaveAsToml (opens new window)

func SaveAsToml(filename string) (err error)
1

SaveAsToml to Save all config entries as a toml file

# func SaveAsYaml (opens new window)

func SaveAsYaml(filename string) (err error)
1

SaveAsYaml to Save all config entries as a yaml file

# func SaveObjAsToml (opens new window)

func SaveObjAsToml(obj interface{}, filename string) (err error)
1

SaveObjAsToml to Save an object as a toml file

# func WrapWithRxxtPrefix (opens new window)

func WrapWithRxxtPrefix(key string) string
1

WrapWithRxxtPrefix wrap an key with [RxxtPrefix], for [GetXxx(key)] and [GetXxxP(prefix,key)]

# 文件、文件夹操作

# func EnsureDir (opens new window)

func EnsureDir(dir string) (err error)
1

EnsureDir checks and creates the directory.

# func EnsureDirEnh (opens new window)

func EnsureDirEnh(dir string) (err error)
1

EnsureDirEnh checks and creates the directory, via sudo if necessary.

# func FileExists (opens new window)

func FileExists(name string) bool
1

FileExists returns the existence of an directory or file

# func GetCurrentDir (opens new window)

func GetCurrentDir() string
1

GetCurrentDir returns the current workingFlag directory it should be equal with os.Getenv("PWD")

# func GetExecutableDir (opens new window)

func GetExecutableDir() string
1

GetExecutableDir returns the executable file directory

# func GetExecutablePath (opens new window)

func GetExecutablePath() string
1

GetExecutablePath returns the executable file path

# func IsDirectory (opens new window)

func IsDirectory(path string) (bool, error)
1

IsDirectory tests whether path is a directory or not

# func IsRegularFile (opens new window)

func IsRegularFile(path string) (bool, error)
1

IsRegularFile tests whether path is a normal regular file or not

# func NormalizeDir (opens new window)

func NormalizeDir(s string) string
1

NormalizeDir make dir name normalized

# func RemoveDirRecursive (opens new window)

func RemoveDirRecursive(dir string) (err error)
1

RemoveDirRecursive removes a directory and any children it contains.

# Debugger/Debugging 操作

# func GetDebugMode (opens new window)

func GetDebugMode() bool
1

GetDebugMode returns the flag value of --debug/-D

NOTE

log.GetDebugMode()/SetDebugMode() have higher universality
1

# func GetNoColorMode (opens new window)

func GetNoColorMode() bool
1

GetNoColorMode return the flag value of --no-color

# func GetQuietMode (opens new window)

func GetQuietMode() bool
1

GetQuietMode returns the flag value of --quiet/-q

# func GetStrictMode (opens new window)

func GetStrictMode() bool
1

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 GetTraceMode (opens new window)

func GetTraceMode() bool
1

GetTraceMode returns the flag value of --trace/-tr

NOTE

log.GetTraceMode()/SetTraceMode() have higher universality
1

# func GetVerboseMode (opens new window)

func GetVerboseMode() bool
1

GetVerboseMode returns the flag value of --verbose/-v

# func InDebugging (opens new window)

func InDebugging() bool
1

InDebugging return the status if cmdr was built with debug mode / or the app running under a debugger attached.

To enable the debugger attached mode for cmdr, run go build with -tags=delve options. eg:

go run -tags=delve ./cli
go build -tags=delve -o my-app ./cli
1
2

For Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'

-tags=delve
1

InDebugging() is a synonym to IsDebuggerAttached().

NOTE that isdelve algor is from https://stackoverflow.com/questions/47879070/how-can-i-see-if-the-goland-debugger-is-running-in-the-program

noinspection GoBoolExpressions

# func InTesting (opens new window)

func InTesting() bool
1

InTesting detects whether is running under go test mode

# func IsDebuggerAttached (opens new window)

func IsDebuggerAttached() bool
1

IsDebuggerAttached return the status if cmdr was built with debug mode / or the app running under a debugger attached.

To enable the debugger attached mode for cmdr, run go build with -tags=delve options. eg:

go run -tags=delve ./cli
go build -tags=delve -o my-app ./cli
1
2

For Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'

-tags=delve
1

IsDebuggerAttached() is a synonym to InDebugging().

NOTE that isdelve algor is from https://stackoverflow.com/questions/47879070/how-can-i-see-if-the-goland-debugger-is-running-in-the-program

noinspection GoBoolExpressions

# 配置文件操作 (Config file Operations)

# func GetPredefinedLocations (opens new window)

func GetPredefinedLocations() []string
1

GetPredefinedLocations return the searching locations for loading config files.

# func GetUsedConfigFile (opens new window)

func GetUsedConfigFile() string
1

GetUsedConfigFile returns the main config filename (generally it's <appname>.yml)

# func GetUsedConfigSubDir (opens new window)

func GetUsedConfigSubDir() string
1

GetUsedConfigSubDir returns the sub-directory conf.d of config files. Note that it be always normalized now. Sometimes it might be empty string ("") if conf.d have not been found.

# func GetUsingConfigFiles (opens new window)

func GetUsingConfigFiles() []string
1

GetUsingConfigFiles returns all loaded config files, includes the main config file and children in sub-directory conf.d.

# func LoadConfigFile (opens new window)

func LoadConfigFile(file string) (err error)
1

LoadConfigFile loads a yaml config file and merge the settings into rxxtOptions and load files in the conf.d child directory too.

# More

tool 子包中也包含一些工具函数。

...

# 🔚