summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
authorSebastiaan Pasterkamp <[email protected]>2022-01-02 15:06:37 +0100
committerSebastiaan Pasterkamp <[email protected]>2022-01-02 15:17:09 +0100
commita87d80089a78b707d9e4fbd7061e54e7e834688d (patch)
treed0cf8e9ccc77b6ef99a1684117c1f233f781c823 /parse.go
parentbf32f082479c88da7bcf4c4765a70e3a357d9dfc (diff)
Add 'IgnoreDefault' option
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go10
1 files changed, 8 insertions, 2 deletions
diff --git a/parse.go b/parse.go
index 7588dfb..9f93502 100644
--- a/parse.go
+++ b/parse.go
@@ -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 !p.config.IgnoreDefault || 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)