Documentation ¶
Overview ¶
Package config to merge run options: command line arguments and ini-file content. Command line arguments take precedence over ini-file
Index ¶
- Constants
- func New(encodingKey string, optFs []FullShort) (*RunOptions, *LogOptions, []string, error)
- func NewIni(iniPath string, encodingName string) (map[string]string, error)
- type FullShort
- type LogOptions
- type RunOptions
- func (opts *RunOptions) Bool(key string) bool
- func (opts *RunOptions) Float(key string, defaultValue float64) float64
- func (opts *RunOptions) Int(key string, defaultValue int) int
- func (opts *RunOptions) Int64(key string, defaultValue int64) int64
- func (opts *RunOptions) IsExist(key string) bool
- func (opts *RunOptions) String(key string) string
- func (opts *RunOptions) StringExist(key string) (val string, isExist, isDefaultArg bool)
- func (opts *RunOptions) Uint64(key string, defaultValue uint64) uint64
Constants ¶
const ( IniFile = "OpenM.IniFile" // ini-file path IniFileShort = "ini" // ini-file path (short form) )
Standard config keys to get values from ini-file or command line arguments
const ( LogToConsoleArgKey = "OpenM.LogToConsole" // if true then log to standard output LogToConsoleShortKey = "v" // if true then log to standard output (short form) LogToFileArgKey = "OpenM.LogToFile" // if true then log to file LogFilePathArgKey = "OpenM.LogFilePath" // log file path, default = current/dir/exeName.log LogUseTsArgKey = "OpenM.LogUseTimeStamp" // if true then use time-stamp in log file name LogUsePidArgKey = "OpenM.LogUsePidStamp" // if true then use pid-stamp in log file name LogUseDailyArgKey = "OpenM.LogUseDailyStamp" // if true then use daily-stamp in log file name LogSqlArgKey = "OpenM.LogSql" // if true then log sql statements into log file )
Log config keys. Log can be enabled/disabled for two independent streams:
console => standard output stream log file => log file, truncated on every run, (optional) unique "stamped" name
"Stamped" file name produced by adding time-stamp and/or pid-stamp, i.e.:
exeName.log => exeName.2012_08_17_16_04_59_148.123.log
Variables ¶
This section is empty.
Functions ¶
func New ¶
func New(encodingKey string, optFs []FullShort) (*RunOptions, *LogOptions, []string, error)
New combines command-line arguments and ini-file options.
encodingKey, if not empty, is a name of command-line option to specify encoding (code page) of source text files, for example: -dbcopy.CodePage=windows-1252. If encoding value specified then ini-file and csv files converted from such encoding to utf-8. If encoding not specified then auto-detection and default values are used (see helper.FileToUtf8())
Return 1. *RunOptions: is merge of command line key=value and ini-file section.key=value options. 2. *LogOptions: openM++ log file settings, also merge of command line and ini-file. 3. Args []string: remaining command line arguments, after last recognized key=value 4. error or nil on success
func NewIni ¶
NewIni read ini-file content into map of (section.key)=>value.
It is very light and able to parse:
dsn = "DSN='server'; UID='user'; PWD='pas#word';" ; comments are # here
Section and key are trimmed and cannot contain comments ; or # chars inside. Key and values trimmed and "unquoted". Key or value escaped with "double" or 'single' quotes can include spaces or ; or # chars
Example:
; comments can start from ; or # from # and empty lines are skipped [section] ; section comment val = no comment rem = ; comment only and empty value nul = dsn = "DSN='server'; UID='user'; PWD='pas#word';" ; quoted value t w = the "# quick #" brown 'fox ; jumps' over ; escaped: ; and # chars " key "" 'quoted' here " = some value [multi-line] trim = Aname, \ ; multi-line value joined with spaces trimmed Bname, \ ; result is: CName ; Aname,Bname,Cname ; multi-line value started with " quote or ' apostrophe ; right spaces before \ is not trimmed ; result is: ; Multi line text with spaces ; keep = "\ Multi line \ text with spaces\ "
Types ¶
type LogOptions ¶
type LogOptions struct { LogPath string // path to log file IsConsole bool // if true then log to standard output, default: true IsFile bool // if true then log to file IsLogSql bool // if true then log sql statements TimeStamp string // log timestamp string, ie: 2012_08_17_16_04_59_148 IsDaily bool // if true then use daily log file names, ie: exeName.20120817.log }
LogOptions for console and log file output
type RunOptions ¶
type RunOptions struct { KeyValue map[string]string // (key=>value) from command line arguments and ini-file DefaultKeyValue map[string]string // default (key=>value), if non-empty default for command line argument // contains filtered or unexported fields }
RunOptions is (key,value) map of command line arguments and ini-file. For ini-file options key is combined as section.key
func FromIni ¶ added in v1.11.0
func FromIni(iniPath string, encodingName string) (*RunOptions, error)
FromIni read ini-file options.
encodingName, if not empty, is a "code page" to convert source file into utf-8, for example: windows-1252
Return 1. *RunOptions: is (key, value) pairs from ini-file: section.key=value 2. error or nil on success
func (*RunOptions) Bool ¶
func (opts *RunOptions) Bool(key string) bool
Bool return boolean value by key. If value not defined by command line argument or ini-file option or cannot be converted to boolean (see strconv.ParseBool) then return false
func (*RunOptions) Float ¶
func (opts *RunOptions) Float(key string, defaultValue float64) float64
Float return 64 bit float value by key. If value not defined by command line argument or ini-file option or cannot be converted to float64 then default is returned
func (*RunOptions) Int ¶
func (opts *RunOptions) Int(key string, defaultValue int) int
Int return integer value by key. If value not defined by command line argument or ini-file option or cannot be converted to integer then default is returned
func (*RunOptions) Int64 ¶
func (opts *RunOptions) Int64(key string, defaultValue int64) int64
Int64 return 64 bit integer value by key. If value not defined by command line argument or ini-file option or cannot be converted to int64 then default is returned
func (*RunOptions) IsExist ¶
func (opts *RunOptions) IsExist(key string) bool
IsExist return true if key is defined as command line argument or ini-file option.
func (*RunOptions) String ¶
func (opts *RunOptions) String(key string) string
String return value by key. It can be defined as command line argument or ini-file option or command line default
func (*RunOptions) StringExist ¶
func (opts *RunOptions) StringExist(key string) (val string, isExist, isDefaultArg bool)
StringExist return value by key and boolean flags: isExist=true if value defined as command line argument or ini-file option, isDefault=true if value defined as non-empty default for command line argument.