@@ -117,6 +117,10 @@ eg.
|
||||
-f, --force Force removal.
|
||||
-r, --recursive Recursively remove files.
|
||||
|
||||
For flags with associated environment variables, the variable `${env}` can be
|
||||
interpolated into the help string. In the absence of this variable in the help,
|
||||
|
||||
|
||||
<a id="markdown-command-handling" name="command-handling"></a>
|
||||
## Command handling
|
||||
|
||||
@@ -459,6 +463,7 @@ are defined from the value itself:
|
||||
|
||||
${default}
|
||||
${enum}
|
||||
${env}
|
||||
|
||||
eg.
|
||||
|
||||
|
||||
@@ -7,6 +7,17 @@ import (
|
||||
|
||||
var interpolationRegex = regexp.MustCompile(`((?:\${([[:alpha:]_][[:word:]]*))(?:=([^}]+))?})|(\$)|([^$]+)`)
|
||||
|
||||
// Returns true if the variable "v" is interpolated in "s".
|
||||
func interpolationHasVar(s string, v string) bool {
|
||||
matches := interpolationRegex.FindAllStringSubmatch(s, -1)
|
||||
for _, match := range matches {
|
||||
if name := match[2]; name == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Interpolate variables from vars into s for substrings in the form ${var} or ${var=default}.
|
||||
func interpolate(s string, vars map[string]string) (string, error) {
|
||||
out := ""
|
||||
|
||||
@@ -155,6 +155,19 @@ func (k *Kong) interpolateValue(value *Value, vars Vars) (err error) {
|
||||
})
|
||||
if value.Tag.Env != "" {
|
||||
vars["env"] = value.Tag.Env
|
||||
if !interpolationHasVar(value.Help, "env") {
|
||||
suffix := "($" + value.Tag.Env + ")"
|
||||
switch {
|
||||
case strings.HasSuffix(value.Help, "."):
|
||||
value.Help = value.Help[:len(value.Help)-1] + " " + suffix + "."
|
||||
|
||||
case value.Help == "":
|
||||
value.Help += suffix
|
||||
|
||||
default:
|
||||
value.Help += " " + suffix
|
||||
}
|
||||
}
|
||||
}
|
||||
if value.Help, err = interpolate(value.Help, vars); err != nil {
|
||||
return fmt.Errorf("help for %s: %s", value.Summary(), err)
|
||||
|
||||
@@ -750,6 +750,17 @@ func TestEnvarEnumValidated(t *testing.T) {
|
||||
require.EqualError(t, err, "--flag must be one of \"valid\" but got \"invalid\"")
|
||||
}
|
||||
|
||||
func TestEnvarAutoHelp(t *testing.T) {
|
||||
var cli struct {
|
||||
Flag string `env:"FLAG" help:"A flag."`
|
||||
}
|
||||
w := &strings.Builder{}
|
||||
p := mustNew(t, &cli, kong.Writers(w, w), kong.Exit(func(int) {}))
|
||||
_, err := p.Parse([]string{"--help"})
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, w.String(), "A flag ($FLAG).")
|
||||
}
|
||||
|
||||
func TestXor(t *testing.T) {
|
||||
var cli struct {
|
||||
Hello bool `xor:"another"`
|
||||
|
||||
Reference in New Issue
Block a user