8000 feat: Placeholder string interpolation. by cwize1 · Pull Request #510 · alecthomas/kong · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Placeholder string interpolation. #510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ also supports dynamically adding commands via `kong.DynamicCommand()`.

## Variable interpolation

Kong supports limited variable interpolation into help strings, enum lists and
default values.
Kong supports limited variable interpolation into help strings, placeholder strings,
enum lists and default values.

Variables are in the form:

Expand Down
5 changes: 5 additions & 0 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ func (k *Kong) interpolateValue(value *Value, vars Vars) (err error) {
if len(value.Flag.Envs) != 0 {
updatedVars["env"] = value.Flag.Envs[0]
}

value.Flag.PlaceHolder, err = interpolate(value.Flag.PlaceHolder, vars, updatedVars)
if err != nil {
return fmt.Errorf("placeholder value for %s: %s", value.Summary(), err)
}
}
value.Help, err = interpolate(value.Help, vars, updatedVars)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ func TestPassesThroughOriginalCommandError(t *testing.T) {

func TestInterpolationIntoModel(t *testing.T) {
var cli struct {
Flag string `default:"${default_value}" help:"Help, I need ${somebody}" enum:"${enum}"`
EnumRef string `enum:"a,b" required:"" help:"One of ${enum}"`
Flag string `default:"${default_value}" help:"Help, I need ${somebody}" enum:"${enum}" placeholder:"${enum}"`
EnumRef string `enum:"a,b" required:"" help:"One of ${enum}" placeholder:"${enum}"`
EnvRef string `env:"${env}" help:"God ${env}"`
}
_, err := kong.New(&cli)
Expand All @@ -787,7 +787,9 @@ func TestInterpolationIntoModel(t *testing.T) {
assert.Equal(t, "Help, I need chickens!", flag.Help)
assert.Equal(t, map[string]bool{"a": true, "b": true, "c": true, "d": true}, flag.EnumMap())
assert.Equal(t, []string{"a", "b", "c", "d"}, flag.EnumSlice())
assert.Equal(t, "a,b,c,d", flag.PlaceHolder)
assert.Equal(t, "One of a,b", flag2.Help)
assert.Equal(t, "a,b", flag2.PlaceHolder)
assert.Equal(t, []string{"SAVE_THE_QUEEN"}, flag3.Envs)
assert.Equal(t, "God SAVE_THE_QUEEN", flag3.Help)
}
Expand Down
Loading
0