Skip to content

Commit

Permalink
feat: add FlagNamer option
Browse files Browse the repository at this point in the history
Fixes #347
  • Loading branch information
alecthomas committed Jan 13, 2023
1 parent 919e701 commit 95a465b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ MAIN:
tag := field.tag
name := tag.Name
if name == "" {
name = tag.Prefix + strings.ToLower(dashedString(ft.Name))
name = tag.Prefix + k.flagNamer(ft.Name)
} else {
name = tag.Prefix + name
}
Expand Down
4 changes: 4 additions & 0 deletions kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Kong struct {
helpFlag *Flag
groups []Group
vars Vars
flagNamer func(string) string

// Set temporarily by Options. These are applied after build().
postBuildOptions []Option
Expand All @@ -85,6 +86,9 @@ func New(grammar interface{}, options ...Option) (*Kong, error) {
bindings: bindings{},
helpFormatter: DefaultHelpValueFormatter,
ignoreFields: make([]*regexp.Regexp, 0),
flagNamer: func(s string) string {
return strings.ToLower(dashedString(s))
},
}

options = append(options, Bind(k))
Expand Down
10 changes: 5 additions & 5 deletions kong_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,17 +642,17 @@ func TestRun(t *testing.T) {

func TestInterpolationIntoModel(t *testing.T) {
var cli struct {
Flag string `default:"${default}" help:"Help, I need ${somebody}" enum:"${enum}"`
Flag string `default:"${default_value}" help:"Help, I need ${somebody}" enum:"${enum}"`
EnumRef string `enum:"a,b" required:"" help:"One of ${enum}"`
EnvRef string `env:"${env}" help:"God ${env}"`
}
_, err := kong.New(&cli)
assert.Error(t, err)
p, err := kong.New(&cli, kong.Vars{
"default": "Some default value.",
"somebody": "chickens!",
"enum": "a,b,c,d",
"env": "SAVE_THE_QUEEN",
"default_value": "Some default value.",
"somebody": "chickens!",
"enum": "a,b,c,d",
"env": "SAVE_THE_QUEEN",
})
assert.NoError(t, err)
assert.Equal(t, 4, len(p.Model.Flags))
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,11 @@ func DefaultEnvars(prefix string) Option {
return nil
})
}

// FlagNamer allows you to override the default kebab-case automated flag name generation.
func FlagNamer(namer func(fieldName string) string) Option {
return OptionFunc(func(k *Kong) error {
k.flagNamer = namer
return nil
})
}
10 changes: 10 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kong

import (
"reflect"
"strings"
"testing"

"github.com/alecthomas/assert/v2"
Expand Down Expand Up @@ -112,3 +113,12 @@ func TestBindToProvider(t *testing.T) {
assert.NoError(t, err)
assert.True(t, cli.Called)
}

func TestFlagNamer(t *testing.T) {
var cli struct {
SomeFlag string
}
app, err := New(&cli, FlagNamer(strings.ToUpper))
assert.NoError(t, err)
assert.Equal(t, "SOMEFLAG", app.Model.Flags[1].Name)
}

0 comments on commit 95a465b

Please sign in to comment.