fixed tests

This commit is contained in:
Pablo Diaz 2022-05-21 17:44:32 +02:00 committed by Ilja Neumann
parent c3cac76438
commit 5f10667949
4 changed files with 20 additions and 10 deletions

View File

@ -504,7 +504,7 @@ func Example_envVarOnly() {
defer os.Unsetenv("AUTH_KEY")
var args struct {
AuthKey string `arg:"-,--,env:AUTH_KEY"`
AuthKey string `arg:"--,env:AUTH_KEY"`
}
MustParse(&args)
@ -517,7 +517,7 @@ func Example_envVarOnlyShouldIgnoreFlag() {
os.Args = split("./example --=my_key")
var args struct {
AuthKey string `arg:"-,--,env:AUTH_KEY"`
AuthKey string `arg:"--,env:AUTH_KEY"`
}
err := Parse(&args)
@ -530,7 +530,7 @@ func Example_envVarOnlyShouldIgnoreShortFlag() {
os.Args = split("./example -=my_key")
var args struct {
AuthKey string `arg:"-,--,env:AUTH_KEY"`
AuthKey string `arg:"--,env:AUTH_KEY"`
}
err := Parse(&args)

View File

@ -360,6 +360,11 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
case strings.HasPrefix(key, "--"):
spec.long = key[2:]
case strings.HasPrefix(key, "-"):
if len(key) != 2 {
errs = append(errs, fmt.Sprintf("%s.%s: short arguments must be one character only",
t.Name(), field.Name))
return false
}
spec.short = key[1:]
case key == "required":
spec.required = true

View File

@ -84,9 +84,10 @@ func (p *Parser) writeUsageForSubcommand(w io.Writer, cmd *command) {
ancestors = append(ancestors, ancestor.name)
ancestor = ancestor.parent
}
// Print environment only variables
for _, spec := range cmd.specs {
if spec.short == "" && spec.long == "" {
ancestors = append(ancestors, spec.env+"="+strings.ToLower(spec.env)+"_value")
ancestors = append(ancestors, "["+spec.env+"="+strings.ToLower(spec.env)+"_value"+"]")
}
}
@ -283,7 +284,7 @@ func (p *Parser) writeHelpForSubcommand(w io.Writer, cmd *command) {
}
// write the list of environment only variables
if len(shortOptions)+len(longOptions) > 0 || cmd.parent == nil {
if len(envOnlyOptions) > 0 {
fmt.Fprint(w, "\nEnvironment variables:\n")
for _, spec := range envOnlyOptions {
p.printEnvOnlyVar(w, spec)

View File

@ -544,14 +544,18 @@ Options:
}
func TestUsageWithEnvOptions(t *testing.T) {
expectedUsage := "Usage: example [-s SHORT]"
expectedUsage := "Usage: [CUSTOM=custom_value] [ENVONLY=envonly_value] example [-s SHORT]"
expectedHelp := `
Usage: example [-s SHORT]
Usage: [CUSTOM=custom_value] [ENVONLY=envonly_value] example [-s SHORT]
Options:
-s SHORT [env: SHORT]
--help, -h display this help and exit
Environment variables:
ENVONLY
CUSTOM
`
var args struct {
Short string `arg:"--,-s,env"`
@ -648,10 +652,10 @@ Options:
}
func TestFailEnvOnly(t *testing.T) {
expectedUsage := "Usage: AUTH_KEY=auth_key_value example [--arg ARG]"
expectedUsage := "Usage: [AUTH_KEY=auth_key_value] example [--arg ARG]"
expectedHelp := `
Usage: AUTH_KEY=auth_key_value example [--arg ARG]
Usage: [AUTH_KEY=auth_key_value] example [--arg ARG]
Options:
--arg ARG, -a ARG [env: MY_ARG]
@ -662,7 +666,7 @@ Environment variables:
`
var args struct {
ArgParam string `arg:"-a,--arg,env:MY_ARG"`
AuthKey string `arg:"-,--,env:AUTH_KEY"`
AuthKey string `arg:"--,env:AUTH_KEY"`
}
p, err := NewParser(Config{Program: "example"}, &args)
assert.NoError(t, err)