Skip to content

Commit deebf0b

Browse files
mitaralecthomas
authored andcommitted
Do not add environment variable to help it is already present.
Fixes #246.
1 parent 32b2f74 commit deebf0b

7 files changed

+30
-5
lines changed

build.go

+1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ func buildField(k *Kong, node *Node, v reflect.Value, ft reflect.StructField, fv
239239
value := &Value{
240240
Name: name,
241241
Help: tag.Help,
242+
OrigHelp: tag.Help,
242243
Default: tag.Default,
243244
DefaultValue: reflect.New(fv.Type()).Elem(),
244245
Mapper: mapper,

help.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type HelpValueFormatter func(value *Value) string
8686

8787
// DefaultHelpValueFormatter is the default HelpValueFormatter.
8888
func DefaultHelpValueFormatter(value *Value) string {
89-
if value.Tag.Env == "" {
89+
if value.Tag.Env == "" || HasInterpolatedVar(value.OrigHelp, "env") {
9090
return value.Help
9191
}
9292
suffix := "($" + value.Tag.Env + ")"

interpolate.go

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ import (
77

88
var interpolationRegex = regexp.MustCompile(`(\$\$)|((?:\${([[:alpha:]_][[:word:]]*))(?:=([^}]+))?})|(\$)|([^$]+)`)
99

10+
// HasInterpolatedVar returns true if the variable "v" is interpolated in "s".
11+
func HasInterpolatedVar(s string, v string) bool {
12+
matches := interpolationRegex.FindAllStringSubmatch(s, -1)
13+
for _, match := range matches {
14+
if name := match[3]; name == v {
15+
return true
16+
}
17+
}
18+
return false
19+
}
20+
1021
// Interpolate variables from vars into s for substrings in the form ${var} or ${var=default}.
1122
func interpolate(s string, vars Vars, updatedVars map[string]string) (string, error) {
1223
out := ""

interpolate_test.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@ import (
88

99
func TestInterpolate(t *testing.T) {
1010
vars := map[string]string{
11-
"age": "35",
11+
"age": "35",
12+
"city": "Melbourne",
1213
}
1314
updatedVars := map[string]string{
1415
"height": "180",
1516
}
16-
actual, err := interpolate("${name=Bobby Brown} is ${age} years old and ${height} cm tall and likes $${AUD}", vars, updatedVars)
17+
actual, err := interpolate("${name=Bobby Brown} is ${age} years old, ${height} cm tall, lives in ${city=<unknown>}, and likes $${AUD}", vars, updatedVars)
1718
require.NoError(t, err)
18-
require.Equal(t, `Bobby Brown is 35 years old and 180 cm tall and likes ${AUD}`, actual)
19+
require.Equal(t, `Bobby Brown is 35 years old, 180 cm tall, lives in Melbourne, and likes ${AUD}`, actual)
20+
}
21+
22+
func TestHasInterpolatedVar(t *testing.T) {
23+
for _, tag := range []string{"name", "age", "height", "city"} {
24+
require.True(t, HasInterpolatedVar("${name=Bobby Brown} is ${age} years old, ${height} cm tall, lives in ${city=<unknown>}, and likes $${AUD}", tag), tag)
25+
}
26+
27+
for _, tag := range []string{"name", "age", "height", "AUD"} {
28+
require.False(t, HasInterpolatedVar("$name $$age {height} $${AUD}", tag), tag)
29+
}
1930
}

kong.go

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func (k *Kong) extraFlags() []*Flag {
223223
Value: &Value{
224224
Name: "help",
225225
Help: "Show context-sensitive help.",
226+
OrigHelp: "Show context-sensitive help.",
226227
Target: value,
227228
Tag: &Tag{},
228229
Mapper: k.registry.ForValue(value),

kong_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ func TestIssue244(t *testing.T) {
675675
k := mustNew(t, &Config{}, kong.Exit(func(int) {}), kong.Writers(w, w))
676676
_, err := k.Parse([]string{"--help"})
677677
require.NoError(t, err)
678-
require.Contains(t, w.String(), `Environment variable: CI_PROJECT_ID ($CI_PROJECT_ID)`)
678+
require.Contains(t, w.String(), `Environment variable: CI_PROJECT_ID`)
679679
}
680680

681681
func TestErrorMissingArgs(t *testing.T) {

model.go

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ type Value struct {
231231
Flag *Flag // Nil if positional argument.
232232
Name string
233233
Help string
234+
OrigHelp string // Original help string, without interpolated variables.
234235
Default string
235236
DefaultValue reflect.Value
236237
Enum string

0 commit comments

Comments
 (0)