diff options
| author | Alex Flint <[email protected]> | 2021-04-19 21:27:53 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-04-19 21:27:53 -0700 |
| commit | a0937d1b588cbb39c0fb9c250ebf21dce42b7dab (patch) | |
| tree | bb658d951719fb3d0b4ed90df9aded48d4bbaeb1 /parse.go | |
| parent | 6a01a15f75472271568c732c1191e9d33a5fc54c (diff) | |
| parent | 01a9fab8d768fbcb11e51d4df3c8fda6230462da (diff) | |
Merge pull request #150 from alexflint/push-coverage-up-more
Push test coverage up to 100%
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 31 |
1 files changed, 4 insertions, 27 deletions
@@ -13,9 +13,6 @@ import ( scalar "github.com/alexflint/go-scalar" ) -// to enable monkey-patching during tests -var osExit = os.Exit - // path represents a sequence of steps to find the output location for an // argument or subcommand in the final destination struct type path struct { @@ -80,7 +77,7 @@ var ErrVersion = errors.New("version requested by user") func MustParse(dest ...interface{}) *Parser { p, err := NewParser(Config{}, dest...) if err != nil { - fmt.Println(err) + fmt.Fprintln(stdout, err) osExit(-1) return nil // just in case osExit was monkey-patched } @@ -88,10 +85,10 @@ func MustParse(dest ...interface{}) *Parser { err = p.Parse(flags()) switch { case err == ErrHelp: - p.writeHelpForCommand(os.Stdout, p.lastCmd) + p.writeHelpForCommand(stdout, p.lastCmd) osExit(0) case err == ErrVersion: - fmt.Println(p.version) + fmt.Fprintln(stdout, p.version) osExit(0) case err != nil: p.failWithCommand(err.Error(), p.lastCmd) @@ -688,15 +685,7 @@ func (p *Parser) val(dest path) reflect.Value { v = v.Elem() } - next := v.FieldByIndex(field.Index) - if !next.IsValid() { - // it is appropriate to panic here because this can only happen due to - // an internal bug in this library (since we construct the path ourselves - // by reflecting on the same struct) - panic(fmt.Errorf("error resolving path %v: %v has no field named %v", - dest.fields, v.Type(), field)) - } - v = next + v = v.FieldByIndex(field.Index) } return v } @@ -723,15 +712,3 @@ func findSubcommand(cmds []*command, name string) *command { } return nil } - -// isZero returns true if v contains the zero value for its type -func isZero(v reflect.Value) bool { - t := v.Type() - if t.Kind() == reflect.Slice || t.Kind() == reflect.Map { - return v.IsNil() - } - if !t.Comparable() { - return false - } - return v.Interface() == reflect.Zero(t).Interface() -} |
