Skip to content

Commit

Permalink
fix(deps): update module github.com/alecthomas/kong to v1 (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
renovate[bot] authored Sep 12, 2024
1 parent 900b811 commit 83da8d2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
11 changes: 9 additions & 2 deletions dumpmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,15 @@ func (cmd cliDumper) dumpFlag(flag *kong.Flag) {

// long flag
cmd.print("`--")
if flag.IsBool() && flag.Tag.Negatable {
cmd.print("[no-]")
if flag.IsBool() && flag.Tag.Negatable != "" {
// Value is "_" for "--no-<flag>",
// and anything else for "--<neg>".
if flag.Tag.Negatable == "_" {
cmd.print("[no-]")
} else {
// Don't need this yet, so don't bother.
panic("not yet implemented")
}
}
cmd.print(name)
// =value
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module go.abhg.dev/gs
go 1.23

require (
github.com/alecthomas/kong v0.9.0
github.com/alecthomas/kong v1.2.1
github.com/buildkite/shellwords v0.0.0-20180315110454-59467a9b8e10
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.1.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZP/GkPY=
github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/kong v1.2.1 h1:E8jH4Tsgv6wCRX2nGrdPyHDUCSG83WH2qE4XLACD33Q=
github.com/alecthomas/kong v1.2.1/go.mod h1:rKTSFhbdp3Ryefn8x5MOEprnRFQ7nlmMC01GKhehhBM=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
Expand Down
21 changes: 17 additions & 4 deletions internal/komplete/komplete.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,8 @@ func (k *kongPredictor) matchFlag(flags []*kong.Flag, scan *kong.Scanner, arg st
matched = "-"+string(flag.Short) == arg
}

if !matched && flag.Tag.Negatable {
matched = "--no-"+flag.Name == arg
if negFlag := negatableFlagName(flag); !matched && negFlag != "" {
matched = negFlag == arg
}

if !matched {
Expand Down Expand Up @@ -632,8 +632,8 @@ func (p *flagsPredictor) Predict(cargs Args) (predictions []string) {
}

predictions = append(predictions, "--"+flag.Name)
if flag.Tag.Negatable {
predictions = append(predictions, "--no-"+flag.Name)
if negFlag := negatableFlagName(flag); negFlag != "" {
predictions = append(predictions, negFlag)
}

// Include aliases only if the user has typed a prefix.
Expand Down Expand Up @@ -664,3 +664,16 @@ func (p *prefixPredictor) Predict(cargs Args) (predictions []string) {
}
return newPredictions
}

func negatableFlagName(f *kong.Flag) string {
// Kong uses "_" as a placeholder for "--no-<flag>".
// Any other value means the flag is "--<negation>".
switch f.Tag.Negatable {
case "":
return ""
case "_":
return "--no-" + f.Name
default:
return "--" + f.Tag.Negatable
}
}
38 changes: 38 additions & 0 deletions internal/komplete/komplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,44 @@ func TestKongPredictor(t *testing.T) {
},
},
},
{
name: "negation/default",
grammar: &struct {
Spicy bool `negatable:""` // --spicy, --no-spicy
}{},
cases: []completeCase{
{
want: []string{"--help", "--spicy", "--no-spicy"},
},
{
give: compLine("--s"),
want: []string{"--spicy"},
},
{
give: compLine("--n"),
want: []string{"--no-spicy"},
},
},
},
{
name: "negation/custom",
grammar: &struct {
Spicy bool `negatable:"mild"` // --spicy, --mild
}{},
cases: []completeCase{
{
want: []string{"--help", "--spicy", "--mild"},
},
{
give: compLine("--s"),
want: []string{"--spicy"},
},
{
give: compLine("--m"),
want: []string{"--mild"},
},
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 83da8d2

Please sign in to comment.