工具函数 (Helpers)
Cmdr 也包含一些辅助函数(Helpers functions)。
对 Option Store 的操作
func AsJSON
AsJSON returns a json string bytes about all options
func AsJSONExt
AsJSONExt returns a json string bytes about all options
func AsToml
AsToml returns a toml string bytes about all options
func AsTomlExt
AsTomlExt returns a toml string bytes about all options
func AsYaml
AsYaml returns a yaml string bytes about all options
func AsYamlExt
AsYamlExt returns a yaml string bytes about all options
func DeleteKey
DeleteKey deletes a key from cmdr options store
func GetHierarchyList
GetHierarchyList returns the hierarchy data
func GetSectionFrom
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:
func HasKey
HasKey detects whether a key exists in cmdr options store or not
func MergeWith
MergeWith will merge a map recursive. You could merge a yaml/json/toml options into cmdr Hierarchy Options.
func ResetOptions
ResetOptions to reset the exists Options
, so that you could follow a LoadConfigFile()
with it.
func SaveAsJSON
SaveAsJSON to Save all config entries as a json file
func SaveAsToml
SaveAsToml to Save all config entries as a toml file
func SaveAsYaml
SaveAsYaml to Save all config entries as a yaml file
func SaveObjAsToml
SaveObjAsToml to Save an object as a toml file
func WrapWithRxxtPrefix
WrapWithRxxtPrefix wrap an key with [RxxtPrefix], for [GetXxx(key)] and [GetXxxP(prefix,key)]
文件、文件夹操作
func EnsureDir
EnsureDir checks and creates the directory.
func EnsureDirEnh
EnsureDirEnh checks and creates the directory, via sudo if necessary.
func FileExists
FileExists returns the existence of an directory or file
func GetCurrentDir
GetCurrentDir returns the current workingFlag directory it should be equal with os.Getenv("PWD")
func GetExecutableDir
GetExecutableDir returns the executable file directory
func GetExecutablePath
GetExecutablePath returns the executable file path
func IsDirectory
IsDirectory tests whether path
is a directory or not
func IsRegularFile
IsRegularFile tests whether path
is a normal regular file or not
func NormalizeDir
NormalizeDir make dir name normalized
func RemoveDirRecursive
RemoveDirRecursive removes a directory and any children it contains.
Debugger/Debugging 操作
func GetDebugMode
GetDebugMode returns the flag value of --debug
/-D
NOTE
func GetNoColorMode
GetNoColorMode return the flag value of --no-color
func GetQuietMode
GetQuietMode returns the flag value of --quiet
/-q
func GetStrictMode
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
GetTraceMode returns the flag value of --trace
/-tr
NOTE
func GetVerboseMode
GetVerboseMode returns the flag value of --verbose
/-v
func InDebugging
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:
For Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'
InDebugging() is a synonym to IsDebuggerAttached().
NOTE that isdelve
algor is mentioned here.
noinspection GoBoolExpressions
func InTesting
InTesting detects whether is running under go test mode
func IsDebuggerAttached
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:
For Goland, you can enable this under 'Run/Debug Configurations', by adding the following into 'Go tool arguments:'
IsDebuggerAttached() is a synonym to InDebugging().
NOTE that isdelve
algor is mentioned here.
noinspection GoBoolExpressions
配置文件操作 (Config file Operations)
func GetPredefinedLocations
GetPredefinedLocations return the searching locations for loading config files.
func GetUsedConfigFile
GetUsedConfigFile returns the main config filename (generally it's <appname>.yml
)
func GetUsedConfigSubDir
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
GetUsingConfigFiles returns all loaded config files, includes the main config file and children in sub-directory conf.d
.
func LoadConfigFile
LoadConfigFile loads a yaml config file and merge the settings into rxxtOptions
and load files in the conf.d
child directory too.
More
tool
子包中也包含一些工具函数。
🔚
How is this guide?
Last updated on