put name of environment variable in error message
This commit is contained in:
parent
a4afd6a849
commit
b47d6e3da6
6
parse.go
6
parse.go
|
@ -653,7 +653,11 @@ func (p *Parser) process(args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if spec.required {
|
if spec.required {
|
||||||
return fmt.Errorf("%s is required", name)
|
msg := fmt.Sprintf("%s is required", name)
|
||||||
|
if spec.env != "" {
|
||||||
|
msg += " (or environment variable " + spec.env + ")"
|
||||||
|
}
|
||||||
|
return errors.New(msg)
|
||||||
}
|
}
|
||||||
if spec.defaultVal != "" {
|
if spec.defaultVal != "" {
|
||||||
err := scalar.ParseValue(p.val(spec.dest), spec.defaultVal)
|
err := scalar.ParseValue(p.val(spec.dest), spec.defaultVal)
|
||||||
|
|
|
@ -203,6 +203,14 @@ func TestRequired(t *testing.T) {
|
||||||
require.Error(t, err, "--foo is required")
|
require.Error(t, err, "--foo is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRequiredWithEnv(t *testing.T) {
|
||||||
|
var args struct {
|
||||||
|
Foo string `arg:"required,env:FOO"`
|
||||||
|
}
|
||||||
|
err := parse("", &args)
|
||||||
|
require.Error(t, err, "--foo is required (or environment variable FOO)")
|
||||||
|
}
|
||||||
|
|
||||||
func TestShortFlag(t *testing.T) {
|
func TestShortFlag(t *testing.T) {
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo string `arg:"-f"`
|
Foo string `arg:"-f"`
|
||||||
|
|
Loading…
Reference in New Issue