chore: optionally allow parsing of hyphen-prefixied flag parameters

This allows for eg. `foo --number -10`, `foo --flag -bar`.

Fixes #478, #315.
This commit is contained in:
Alec Thomas
2025-05-15 19:31:29 +10:00
parent 8e03dbeaf6
commit 9bc3bf9925
5 changed files with 69 additions and 22 deletions
+36 -9
View File
@@ -1043,15 +1043,6 @@ func TestParentBindings(t *testing.T) {
assert.Equal(t, "foo", cli.Command.value)
}
func TestNumericParamErrors(t *testing.T) {
var cli struct {
Name string
}
parser := mustNew(t, &cli)
_, err := parser.Parse([]string{"--name", "-10"})
assert.EqualError(t, err, `--name: expected string value but got "-10" (short flag); perhaps try --name="-10"?`)
}
func TestDefaultValueIsHyphen(t *testing.T) {
var cli struct {
Flag string `default:"-"`
@@ -2677,3 +2668,39 @@ func TestProviderWithoutError(t *testing.T) {
err = kctx.Run()
assert.NoError(t, err)
}
func TestParseHyphenParameter(t *testing.T) {
type shortFlag struct {
Flag string `short:"f"`
Other string `short:"o"`
Numeric int `short:"n"`
}
t.Run("ShortFlag", func(t *testing.T) {
actual := &shortFlag{}
_, err := mustNew(t, actual, kong.WithHyphenPrefixedParameters(true)).Parse([]string{"-f", "-foo"})
assert.NoError(t, err)
assert.Equal(t, &shortFlag{Flag: "-foo"}, actual)
})
t.Run("LongFlag", func(t *testing.T) {
actual := &shortFlag{}
_, err := mustNew(t, actual, kong.WithHyphenPrefixedParameters(true)).Parse([]string{"--flag", "-foo"})
assert.NoError(t, err)
assert.Equal(t, &shortFlag{Flag: "-foo"}, actual)
})
t.Run("ParamMatchesFlag", func(t *testing.T) {
actual := &shortFlag{}
_, err := mustNew(t, actual, kong.WithHyphenPrefixedParameters(true)).Parse([]string{"--flag", "-oo"})
assert.NoError(t, err)
assert.Equal(t, &shortFlag{Flag: "-oo"}, actual)
})
t.Run("NegativeNumber", func(t *testing.T) {
actual := &shortFlag{}
_, err := mustNew(t, actual, kong.WithHyphenPrefixedParameters(true)).Parse([]string{"--numeric", "-10"})
assert.NoError(t, err)
assert.Equal(t, &shortFlag{Numeric: -10}, actual)
})
}