Repository: alecthomas/kong (go)

Issue:
Default help formatter appends environment variable even when ${env} is inside the help string
I have the following structure:

```go
type Config struct {
	Project   string `short:"p" env:"CI_PROJECT_ID" help:"Environment variable: ${env}"`
}
```

And the help message I get is:

```
Environment variable: CI_PROJECT_ID ($CI_PROJECT_ID)
```

But I would expect it to be:

```
Environment variable: CI_PROJECT_ID
```

This issue is a followup to #244.

Pull request:
Do not add environment variable to help it is already present
Fixes #246.

Patch:
diff --git a/build.go b/build.go
index 3e8be10..0b3e4ab 100644
--- a/build.go
+++ b/build.go
@@ -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,
diff --git a/help.go b/help.go
index ad7cb80..d65f91c 100644
--- a/help.go
+++ b/help.go
@@ -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 + ")"
diff --git a/interpolate.go b/interpolate.go
index dceeed0..e811632 100644
--- a/interpolate.go
+++ b/interpolate.go
@@ -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 := ""
diff --git a/kong.go b/kong.go
index 45ef8b8..a4d230e 100644
--- a/kong.go
+++ b/kong.go
@@ -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),
diff --git a/model.go b/model.go
index 2c2991f..5f7f80e 100644
--- a/model.go
+++ b/model.go
@@ -231,6 +231,7 @@ type Value struct {
 	Flag         *Flag // Nil if positional argument.
 	Name         string
 	Help         string
+	OrigHelp     string // Original help string, without interpolated variables.
 	Default      string
 	DefaultValue reflect.Value
 	Enum         string


Test patch:
diff --git a/interpolate_test.go b/interpolate_test.go
index 399d114..927be41 100644
--- a/interpolate_test.go
+++ b/interpolate_test.go
@@ -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)
+	}
 }
diff --git a/kong_test.go b/kong_test.go
index fc8c40b..4c9d5fb 100644
--- a/kong_test.go
+++ b/kong_test.go
@@ -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) {