fixed tests
This commit is contained in:
parent
c3cac76438
commit
5f10667949
|
@ -504,7 +504,7 @@ func Example_envVarOnly() {
|
||||||
defer os.Unsetenv("AUTH_KEY")
|
defer os.Unsetenv("AUTH_KEY")
|
||||||
|
|
||||||
var args struct {
|
var args struct {
|
||||||
AuthKey string `arg:"-,--,env:AUTH_KEY"`
|
AuthKey string `arg:"--,env:AUTH_KEY"`
|
||||||
}
|
}
|
||||||
|
|
||||||
MustParse(&args)
|
MustParse(&args)
|
||||||
|
@ -517,7 +517,7 @@ func Example_envVarOnlyShouldIgnoreFlag() {
|
||||||
os.Args = split("./example --=my_key")
|
os.Args = split("./example --=my_key")
|
||||||
|
|
||||||
var args struct {
|
var args struct {
|
||||||
AuthKey string `arg:"-,--,env:AUTH_KEY"`
|
AuthKey string `arg:"--,env:AUTH_KEY"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := Parse(&args)
|
err := Parse(&args)
|
||||||
|
@ -530,7 +530,7 @@ func Example_envVarOnlyShouldIgnoreShortFlag() {
|
||||||
os.Args = split("./example -=my_key")
|
os.Args = split("./example -=my_key")
|
||||||
|
|
||||||
var args struct {
|
var args struct {
|
||||||
AuthKey string `arg:"-,--,env:AUTH_KEY"`
|
AuthKey string `arg:"--,env:AUTH_KEY"`
|
||||||
}
|
}
|
||||||
|
|
||||||
err := Parse(&args)
|
err := Parse(&args)
|
||||||
|
|
5
parse.go
5
parse.go
|
@ -360,6 +360,11 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
|
||||||
case strings.HasPrefix(key, "--"):
|
case strings.HasPrefix(key, "--"):
|
||||||
spec.long = key[2:]
|
spec.long = key[2:]
|
||||||
case strings.HasPrefix(key, "-"):
|
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:]
|
spec.short = key[1:]
|
||||||
case key == "required":
|
case key == "required":
|
||||||
spec.required = true
|
spec.required = true
|
||||||
|
|
5
usage.go
5
usage.go
|
@ -84,9 +84,10 @@ func (p *Parser) writeUsageForSubcommand(w io.Writer, cmd *command) {
|
||||||
ancestors = append(ancestors, ancestor.name)
|
ancestors = append(ancestors, ancestor.name)
|
||||||
ancestor = ancestor.parent
|
ancestor = ancestor.parent
|
||||||
}
|
}
|
||||||
|
// Print environment only variables
|
||||||
for _, spec := range cmd.specs {
|
for _, spec := range cmd.specs {
|
||||||
if spec.short == "" && spec.long == "" {
|
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
|
// 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")
|
fmt.Fprint(w, "\nEnvironment variables:\n")
|
||||||
for _, spec := range envOnlyOptions {
|
for _, spec := range envOnlyOptions {
|
||||||
p.printEnvOnlyVar(w, spec)
|
p.printEnvOnlyVar(w, spec)
|
||||||
|
|
|
@ -544,14 +544,18 @@ Options:
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUsageWithEnvOptions(t *testing.T) {
|
func TestUsageWithEnvOptions(t *testing.T) {
|
||||||
expectedUsage := "Usage: example [-s SHORT]"
|
expectedUsage := "Usage: [CUSTOM=custom_value] [ENVONLY=envonly_value] example [-s SHORT]"
|
||||||
|
|
||||||
expectedHelp := `
|
expectedHelp := `
|
||||||
Usage: example [-s SHORT]
|
Usage: [CUSTOM=custom_value] [ENVONLY=envonly_value] example [-s SHORT]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-s SHORT [env: SHORT]
|
-s SHORT [env: SHORT]
|
||||||
--help, -h display this help and exit
|
--help, -h display this help and exit
|
||||||
|
|
||||||
|
Environment variables:
|
||||||
|
ENVONLY
|
||||||
|
CUSTOM
|
||||||
`
|
`
|
||||||
var args struct {
|
var args struct {
|
||||||
Short string `arg:"--,-s,env"`
|
Short string `arg:"--,-s,env"`
|
||||||
|
@ -648,10 +652,10 @@ Options:
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFailEnvOnly(t *testing.T) {
|
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 := `
|
expectedHelp := `
|
||||||
Usage: AUTH_KEY=auth_key_value example [--arg ARG]
|
Usage: [AUTH_KEY=auth_key_value] example [--arg ARG]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--arg ARG, -a ARG [env: MY_ARG]
|
--arg ARG, -a ARG [env: MY_ARG]
|
||||||
|
@ -662,7 +666,7 @@ Environment variables:
|
||||||
`
|
`
|
||||||
var args struct {
|
var args struct {
|
||||||
ArgParam string `arg:"-a,--arg,env:MY_ARG"`
|
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)
|
p, err := NewParser(Config{Program: "example"}, &args)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
Loading…
Reference in New Issue