2018-01-11 22:19:25 +00:00
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SourceCLI is a Source which will parse configuration from the CLI.
|
|
|
|
//
|
|
|
|
// Possible CLI options are generated by joining the Path to a Param, and its
|
|
|
|
// name, with dashes. For example:
|
|
|
|
//
|
|
|
|
// cfg := mcfg.New().Child("foo").Child("bar")
|
|
|
|
// addr := cfg.ParamString("addr", "", "Some address")
|
|
|
|
// // the CLI option to fill addr will be "--foo-bar-addr"
|
|
|
|
//
|
|
|
|
// If the "-h" option is seen then a help page will be printed to
|
|
|
|
// stdout and the process will exit. Since all normally-defined parameters must
|
|
|
|
// being with double-dash ("--") they won't ever conflict with the help option.
|
|
|
|
//
|
2018-08-14 00:44:03 +00:00
|
|
|
// SourceCLI behaves a little differently with boolean parameters. Setting the
|
|
|
|
// value of a boolean parameter directly _must_ be done with an equals, for
|
|
|
|
// example: `--boolean-flag=1` or `--boolean-flag=false`. Using the
|
|
|
|
// space-separated format will not work. If a boolean has no equal-separated
|
|
|
|
// value it is assumed to be setting the value to `true`, as would be expected.
|
2018-01-11 22:19:25 +00:00
|
|
|
type SourceCLI struct {
|
|
|
|
Args []string // if nil then os.Args[1:] is used
|
|
|
|
|
|
|
|
DisableHelpPage bool
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
cliKeyJoin = "-"
|
|
|
|
cliKeyPrefix = "--"
|
|
|
|
cliValSep = "="
|
|
|
|
cliHelpArg = "-h"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse implements the method for the Source interface
|
|
|
|
func (cli SourceCLI) Parse(cfg *Cfg) ([]ParamValue, error) {
|
|
|
|
args := cli.Args
|
|
|
|
if cli.Args == nil {
|
|
|
|
args = os.Args[1:]
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
pM, err := cli.cliParams(cfg)
|
2018-01-11 22:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pvs := make([]ParamValue, 0, len(args))
|
|
|
|
var (
|
|
|
|
key string
|
|
|
|
pv ParamValue
|
|
|
|
pvOk bool
|
|
|
|
pvStrVal string
|
|
|
|
pvStrValOk bool
|
|
|
|
)
|
|
|
|
for _, arg := range args {
|
|
|
|
if pvOk {
|
|
|
|
pvStrVal = arg
|
|
|
|
pvStrValOk = true
|
|
|
|
} else if !cli.DisableHelpPage && arg == cliHelpArg {
|
2018-08-14 01:02:06 +00:00
|
|
|
cli.printHelp(os.Stdout, pM)
|
2018-01-11 22:19:25 +00:00
|
|
|
os.Stdout.Sync()
|
|
|
|
os.Exit(1)
|
|
|
|
} else {
|
2018-08-14 01:02:06 +00:00
|
|
|
for key, pv.Param = range pM {
|
2018-01-11 22:19:25 +00:00
|
|
|
if arg == key {
|
|
|
|
pvOk = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := key + cliValSep
|
|
|
|
if !strings.HasPrefix(arg, prefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pvOk = true
|
|
|
|
pvStrVal = strings.TrimPrefix(arg, prefix)
|
|
|
|
pvStrValOk = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !pvOk {
|
|
|
|
return nil, fmt.Errorf("unexpected config parameter %q", arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pvOk is always true at this point, and so pv is filled in
|
|
|
|
|
2018-08-14 00:44:03 +00:00
|
|
|
// As a special case for CLI, if a boolean has no value set it means it
|
|
|
|
// is true.
|
|
|
|
if pv.IsBool && !pvStrValOk {
|
|
|
|
pvStrVal = "true"
|
|
|
|
pvStrValOk = true
|
2018-01-11 22:19:25 +00:00
|
|
|
} else if !pvStrValOk {
|
|
|
|
// everything else should have a value. if pvStrVal isn't filled it
|
|
|
|
// means the next arg should be one. Continue the loop, it'll get
|
|
|
|
// filled with the next one (hopefully)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
pv.Value = pv.Param.fuzzyParse(pvStrVal)
|
2018-08-14 00:44:03 +00:00
|
|
|
|
2018-01-11 22:19:25 +00:00
|
|
|
pvs = append(pvs, pv)
|
|
|
|
key = ""
|
|
|
|
pv = ParamValue{}
|
|
|
|
pvOk = false
|
|
|
|
pvStrVal = ""
|
|
|
|
pvStrValOk = false
|
|
|
|
}
|
|
|
|
if pvOk && !pvStrValOk {
|
|
|
|
return nil, fmt.Errorf("param %q expected a value", key)
|
|
|
|
}
|
|
|
|
return pvs, nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
func (cli SourceCLI) cliParams(cfg *Cfg) (map[string]Param, error) {
|
|
|
|
m := map[string]Param{}
|
|
|
|
for _, p := range cfg.allParams() {
|
|
|
|
key := strings.Join(append(p.Path, p.Name), cliKeyJoin)
|
|
|
|
m[cliKeyPrefix+key] = p
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
func (cli SourceCLI) printHelp(w io.Writer, pM map[string]Param) {
|
|
|
|
type pEntry struct {
|
2018-01-11 22:19:25 +00:00
|
|
|
arg string
|
2018-08-14 01:02:06 +00:00
|
|
|
Param
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
pA := make([]pEntry, 0, len(pM))
|
|
|
|
for arg, p := range pM {
|
|
|
|
pA = append(pA, pEntry{arg: arg, Param: p})
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
sort.Slice(pA, func(i, j int) bool {
|
|
|
|
return pA[i].arg < pA[j].arg
|
2018-01-11 22:19:25 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
fmtDefaultVal := func(ptr interface{}) string {
|
|
|
|
if ptr == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
val := reflect.Indirect(reflect.ValueOf(ptr))
|
|
|
|
zero := reflect.Zero(val.Type())
|
|
|
|
if reflect.DeepEqual(val.Interface(), zero.Interface()) {
|
|
|
|
return ""
|
|
|
|
} else if val.Type().Kind() == reflect.String {
|
|
|
|
return fmt.Sprintf("%q", val.Interface())
|
|
|
|
}
|
|
|
|
return fmt.Sprint(val.Interface())
|
|
|
|
}
|
|
|
|
|
2018-08-14 01:02:06 +00:00
|
|
|
for _, p := range pA {
|
|
|
|
fmt.Fprintf(w, "\n%s", p.arg)
|
|
|
|
if p.IsBool {
|
2018-01-11 22:19:25 +00:00
|
|
|
fmt.Fprintf(w, " (Flag)")
|
2018-08-14 01:02:06 +00:00
|
|
|
} else if defVal := fmtDefaultVal(p.Into); defVal != "" {
|
2018-01-11 22:19:25 +00:00
|
|
|
fmt.Fprintf(w, " (Default: %s)", defVal)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\n")
|
2018-08-14 01:02:06 +00:00
|
|
|
if p.Usage != "" {
|
|
|
|
fmt.Fprintln(w, "\t"+p.Usage)
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "\n")
|
|
|
|
}
|