Skip to content

Commit

Permalink
Added Env in help, increased coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vbogretsov committed Dec 26, 2020
1 parent 4553db7 commit 21af326
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions argparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,31 @@ func TestIntFromEnv(t *testing.T) {
}
}

func TestIntFromEnvFailed(t *testing.T) {
testArgs := []string{"progname"}

envName := "PROGNAME_FLAG_ARG1"
envValue := "xx10"

p := NewParser("", "descriptiom")
p.Int("f", "flag-arg1", &Options{
Required: true,
Env: Env{Name: envName},
})

if err := os.Setenv(envName, envValue); err != nil {
t.Errorf("Test %s failed. Unable to set env %s=%s: %v", t.Name(), envName, envValue, err)
return
}
defer os.Unsetenv(envName)

err := p.Parse(testArgs)

if err == nil {
t.Errorf("Test %s expected error but no error occured", t.Name())
}
}

func TestIntFailSimple1(t *testing.T) {
testArgs := []string{"progname", "--flag-arg1", "string"}

Expand Down
3 changes: 3 additions & 0 deletions argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ func (o *arg) getHelpMessage() string {
if !o.opts.Required && o.opts.Default != nil {
message += fmt.Sprintf(". Default: %v", o.opts.Default)
}
if o.opts.Env.Name != "" {
message += fmt.Sprintf(". Env: %v", o.opts.Env.Name)
}
}
return message
}
Expand Down

0 comments on commit 21af326

Please sign in to comment.