cli: simplify option model per pike review
- appendList: one home for the List newline-join rule (was duplicated across accumulate and set) - fold the redundant Int64 getter into Int; drop the truthy helper for the shared boolWord vocabulary - add Opt.Metavar so help derives the value placeholder from data instead of branching on the option name (seed-time) No behavior change; --help output is byte-identical.
This commit is contained in:
30
cli/parse.go
30
cli/parse.go
@@ -41,7 +41,7 @@ func Parse(args []string) (*Result, error) {
|
||||
opts := newOptions()
|
||||
applyDefaults(opts)
|
||||
|
||||
if !truthy(cmdVals["no-conf"]) {
|
||||
if noConf, _ := boolWord(cmdVals["no-conf"]); !noConf {
|
||||
explicit := cmdVals["conf-path"]
|
||||
path, fromUser := confPath(explicit)
|
||||
if err := applyConfig(opts, path, fromUser); err != nil {
|
||||
@@ -138,14 +138,23 @@ func parseArgs(args []string) (vals map[string]string, uris []string, action Act
|
||||
// accumulate stores a value, joining repeatable List options with newlines.
|
||||
func accumulate(vals map[string]string, o *Opt, val string) {
|
||||
if o.Kind == List {
|
||||
if prev, ok := vals[o.Long]; ok {
|
||||
vals[o.Long] = prev + "\n" + val
|
||||
return
|
||||
}
|
||||
vals[o.Long] = appendList(vals[o.Long], val)
|
||||
return
|
||||
}
|
||||
vals[o.Long] = val
|
||||
}
|
||||
|
||||
// appendList is the single home of the List newline-join rule, shared by the
|
||||
// raw-parse layer (accumulate) and the resolved layer (set). An empty prev (the
|
||||
// option not yet seen) yields val unchanged, so the first value carries no
|
||||
// leading newline.
|
||||
func appendList(prev, val string) string {
|
||||
if prev == "" {
|
||||
return val
|
||||
}
|
||||
return prev + "\n" + val
|
||||
}
|
||||
|
||||
func applyDefaults(o *Options) {
|
||||
for i := range options {
|
||||
opt := &options[i]
|
||||
@@ -215,10 +224,8 @@ func set(o *Options, name, val string) error {
|
||||
if err := validate(opt, val); err != nil {
|
||||
return err
|
||||
}
|
||||
if opt.Kind == List {
|
||||
if prev, ok := o.vals[name]; ok && o.set[name] {
|
||||
val = prev + "\n" + val
|
||||
}
|
||||
if opt.Kind == List && o.set[name] {
|
||||
val = appendList(o.vals[name], val)
|
||||
}
|
||||
o.vals[name] = val
|
||||
o.set[name] = true
|
||||
@@ -304,8 +311,3 @@ func confPath(explicit string) (path string, fromUser bool) {
|
||||
}
|
||||
return filepath.Join(home, ".config", "got", "got.conf"), false
|
||||
}
|
||||
|
||||
func truthy(s string) bool {
|
||||
val, _ := boolWord(s)
|
||||
return val
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user