diff options
| author | Alex Flint <[email protected]> | 2022-06-11 09:06:03 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-06-11 09:06:03 -0400 |
| commit | ebd7a68a06bef58b87c1fd21c2e7db383adbcbf3 (patch) | |
| tree | e23f25119bde9ad8fd505bcccd3f78295000ed3a /parse.go | |
| parent | f0f44b65d1179ccedb4c56f493f97ec569a6654e (diff) | |
| parent | b48371a62f7beed42eadc9b719ea4f059aa24ef2 (diff) | |
Merge pull request #172 from SebastiaanPasterkamp/ignore-default-option
Add 'IgnoreDefault' option
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 10 |
1 files changed, 8 insertions, 2 deletions
@@ -121,6 +121,10 @@ type Config struct { // IgnoreEnv instructs the library not to read environment variables IgnoreEnv bool + + // IgnoreDefault instructs the library not to reset the variables to the + // default values, including pointers to sub commands + IgnoreDefault bool } // Parser represents a set of command line options with destination values @@ -527,7 +531,9 @@ func (p *Parser) process(args []string) error { // instantiate the field to point to a new struct v := p.val(subcmd.dest) - v.Set(reflect.New(v.Type().Elem())) // we already checked that all subcommands are struct pointers + if v.IsNil() { + v.Set(reflect.New(v.Type().Elem())) // we already checked that all subcommands are struct pointers + } // add the new options to the set of allowed options specs = append(specs, subcmd.specs...) @@ -659,7 +665,7 @@ func (p *Parser) process(args []string) error { } return errors.New(msg) } - if spec.defaultVal != "" { + if !p.config.IgnoreDefault && spec.defaultVal != "" { err := scalar.ParseValue(p.val(spec.dest), spec.defaultVal) if err != nil { return fmt.Errorf("error processing default value for %s: %v", name, err) |
