Do not add environment variable to help it is already present.
Fixes #246.
This commit is contained in:
@@ -239,6 +239,7 @@ func buildField(k *Kong, node *Node, v reflect.Value, ft reflect.StructField, fv
|
||||
value := &Value{
|
||||
Name: name,
|
||||
Help: tag.Help,
|
||||
OrigHelp: tag.Help,
|
||||
Default: tag.Default,
|
||||
DefaultValue: reflect.New(fv.Type()).Elem(),
|
||||
Mapper: mapper,
|
||||
|
||||
@@ -86,7 +86,7 @@ type HelpValueFormatter func(value *Value) string
|
||||
|
||||
// DefaultHelpValueFormatter is the default HelpValueFormatter.
|
||||
func DefaultHelpValueFormatter(value *Value) string {
|
||||
if value.Tag.Env == "" {
|
||||
if value.Tag.Env == "" || HasInterpolatedVar(value.OrigHelp, "env") {
|
||||
return value.Help
|
||||
}
|
||||
suffix := "($" + value.Tag.Env + ")"
|
||||
|
||||
@@ -7,6 +7,17 @@ import (
|
||||
|
||||
var interpolationRegex = regexp.MustCompile(`(\$\$)|((?:\${([[:alpha:]_][[:word:]]*))(?:=([^}]+))?})|(\$)|([^$]+)`)
|
||||
|
||||
// HasInterpolatedVar returns true if the variable "v" is interpolated in "s".
|
||||
func HasInterpolatedVar(s string, v string) bool {
|
||||
matches := interpolationRegex.FindAllStringSubmatch(s, -1)
|
||||
for _, match := range matches {
|
||||
if name := match[3]; 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 Vars, updatedVars map[string]string) (string, error) {
|
||||
out := ""
|
||||
|
||||
+14
-3
@@ -8,12 +8,23 @@ import (
|
||||
|
||||
func TestInterpolate(t *testing.T) {
|
||||
vars := map[string]string{
|
||||
"age": "35",
|
||||
"age": "35",
|
||||
"city": "Melbourne",
|
||||
}
|
||||
updatedVars := map[string]string{
|
||||
"height": "180",
|
||||
}
|
||||
actual, err := interpolate("${name=Bobby Brown} is ${age} years old and ${height} cm tall and likes $${AUD}", vars, updatedVars)
|
||||
actual, err := interpolate("${name=Bobby Brown} is ${age} years old, ${height} cm tall, lives in ${city=<unknown>}, and likes $${AUD}", vars, updatedVars)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `Bobby Brown is 35 years old and 180 cm tall and likes ${AUD}`, actual)
|
||||
require.Equal(t, `Bobby Brown is 35 years old, 180 cm tall, lives in Melbourne, and likes ${AUD}`, actual)
|
||||
}
|
||||
|
||||
func TestHasInterpolatedVar(t *testing.T) {
|
||||
for _, tag := range []string{"name", "age", "height", "city"} {
|
||||
require.True(t, HasInterpolatedVar("${name=Bobby Brown} is ${age} years old, ${height} cm tall, lives in ${city=<unknown>}, and likes $${AUD}", tag), tag)
|
||||
}
|
||||
|
||||
for _, tag := range []string{"name", "age", "height", "AUD"} {
|
||||
require.False(t, HasInterpolatedVar("$name $$age {height} $${AUD}", tag), tag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +223,7 @@ func (k *Kong) extraFlags() []*Flag {
|
||||
Value: &Value{
|
||||
Name: "help",
|
||||
Help: "Show context-sensitive help.",
|
||||
OrigHelp: "Show context-sensitive help.",
|
||||
Target: value,
|
||||
Tag: &Tag{},
|
||||
Mapper: k.registry.ForValue(value),
|
||||
|
||||
+1
-1
@@ -675,7 +675,7 @@ func TestIssue244(t *testing.T) {
|
||||
k := mustNew(t, &Config{}, kong.Exit(func(int) {}), kong.Writers(w, w))
|
||||
_, err := k.Parse([]string{"--help"})
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, w.String(), `Environment variable: CI_PROJECT_ID ($CI_PROJECT_ID)`)
|
||||
require.Contains(t, w.String(), `Environment variable: CI_PROJECT_ID`)
|
||||
}
|
||||
|
||||
func TestErrorMissingArgs(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user