From ea786c454e6e5055d05ebb632e526ef85726780b Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Fri, 14 Jun 2024 09:48:09 +0200 Subject: [PATCH 1/2] Upgrade posener/complete to v2 This reverts commit b206b171f8380c97d94569410070622d30af019a. --- completion.go | 38 +++++++----------- completion_installer.go | 3 +- flag.go | 3 +- flags.go | 86 ++++++++++++++++++++--------------------- funcs.go | 4 +- go.mod | 6 +-- go.sum | 15 ++----- logging_flags.go | 18 ++++----- output_flags.go | 3 +- 9 files changed, 74 insertions(+), 102 deletions(-) diff --git a/completion.go b/completion.go index eee0a15..9428d6b 100644 --- a/completion.go +++ b/completion.go @@ -7,10 +7,7 @@ import ( "os" "runtime/debug" - "github.com/pkg/errors" - "github.com/posener/complete" - "github.com/rs/zerolog" - "github.com/symfony-cli/terminal" + "github.com/posener/complete/v2" ) func init() { @@ -50,15 +47,9 @@ func registerAutocompleteCommands(a *Application) { } func AutocompleteAppAction(c *Context) error { - // connect posener/complete logger to our logging facilities - logger := terminal.Logger.WithLevel(zerolog.DebugLevel) - complete.Log = func(format string, args ...interface{}) { - logger.Msgf("completion | "+format, args...) - } - cmd := complete.Command{ - GlobalFlags: make(complete.Flags), - Sub: make(complete.Commands), + Flags: map[string]complete.Predictor{}, + Sub: map[string]*complete.Command{}, } // transpose registered commands and flags to posener/complete equivalence @@ -66,13 +57,13 @@ func AutocompleteAppAction(c *Context) error { subCmd := command.convertToPosenerCompleteCommand(c) for _, name := range command.Names() { - cmd.Sub[name] = subCmd + cmd.Sub[name] = &subCmd } } for _, f := range c.App.VisibleFlags() { if vf, ok := f.(*verbosityFlag); ok { - vf.addToPosenerFlags(c, cmd.GlobalFlags) + vf.addToPosenerFlags(c, cmd.Flags) continue } @@ -80,20 +71,17 @@ func AutocompleteAppAction(c *Context) error { for _, name := range f.Names() { name = fmt.Sprintf("%s%s", prefixFor(name), name) - cmd.GlobalFlags[name] = predictor + cmd.Flags[name] = predictor } } - if !complete.New(c.App.HelpName, cmd).Complete() { - return errors.New("Could not run auto-completion") - } - + cmd.Complete(c.App.HelpName) return nil } func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command { command := complete.Command{ - Flags: make(complete.Flags, 0), + Flags: map[string]complete.Predictor{}, } for _, f := range c.VisibleFlags() { @@ -110,16 +98,16 @@ func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command return command } -func (c *Command) PredictArgs(ctx *Context, a complete.Args) []string { +func (c *Command) PredictArgs(ctx *Context, prefix string) []string { if c.ShellComplete != nil { - return c.ShellComplete(ctx, a) + return c.ShellComplete(ctx, prefix) } return nil } type Predictor interface { - PredictArgs(*Context, complete.Args) []string + PredictArgs(*Context, string) []string } // ContextPredictor determines what terms can follow a command or a flag @@ -131,6 +119,6 @@ type ContextPredictor struct { } // Predict invokes the predict function and implements the Predictor interface -func (p ContextPredictor) Predict(a complete.Args) []string { - return p.predictor.PredictArgs(p.ctx, a) +func (p ContextPredictor) Predict(prefix string) []string { + return p.predictor.PredictArgs(p.ctx, prefix) } diff --git a/completion_installer.go b/completion_installer.go index 873718b..d77f842 100644 --- a/completion_installer.go +++ b/completion_installer.go @@ -12,7 +12,6 @@ import ( "text/template" "github.com/pkg/errors" - "github.com/posener/complete" "github.com/symfony-cli/terminal" ) @@ -28,7 +27,7 @@ var shellAutoCompleteInstallCommand = &Command{ {Name: "completion"}, }, Usage: "Dumps the completion script for the current shell", - ShellComplete: func(context *Context, c complete.Args) []string { + ShellComplete: func(*Context, string) []string { return []string{"bash", "zsh", "fish"} }, Description: `The {{.HelpName}} command dumps the shell completion script required diff --git a/flag.go b/flag.go index d833de8..b4cf7cc 100644 --- a/flag.go +++ b/flag.go @@ -33,7 +33,6 @@ import ( "time" "github.com/pkg/errors" - "github.com/posener/complete" "github.com/symfony-cli/terminal" ) @@ -94,7 +93,7 @@ func (f FlagsByName) Swap(i, j int) { type Flag interface { fmt.Stringer - PredictArgs(*Context, complete.Args) []string + PredictArgs(*Context, string) []string Validate(*Context) error // Apply Flag settings to the given flag set Apply(*flag.FlagSet) diff --git a/flags.go b/flags.go index ed4fc24..90f72f8 100644 --- a/flags.go +++ b/flags.go @@ -23,8 +23,6 @@ import ( "flag" "strconv" "time" - - "github.com/posener/complete" ) // BoolFlag is a flag with type bool @@ -37,7 +35,7 @@ type BoolFlag struct { DefaultValue bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, bool) error Destination *bool } @@ -48,9 +46,9 @@ func (f *BoolFlag) String() string { return FlagStringer(f) } -func (f *BoolFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *BoolFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{"true", "false"} } @@ -98,7 +96,7 @@ type DurationFlag struct { DefaultValue time.Duration DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, time.Duration) error Destination *time.Duration } @@ -109,9 +107,9 @@ func (f *DurationFlag) String() string { return FlagStringer(f) } -func (f *DurationFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *DurationFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -159,7 +157,7 @@ type Float64Flag struct { DefaultValue float64 DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, float64) error Destination *float64 } @@ -170,9 +168,9 @@ func (f *Float64Flag) String() string { return FlagStringer(f) } -func (f *Float64Flag) PredictArgs(c *Context, a complete.Args) []string { +func (f *Float64Flag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -219,7 +217,7 @@ type GenericFlag struct { Hidden bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, interface{}) error Destination Generic } @@ -230,9 +228,9 @@ func (f *GenericFlag) String() string { return FlagStringer(f) } -func (f *GenericFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *GenericFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -280,7 +278,7 @@ type Int64Flag struct { DefaultValue int64 DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, int64) error Destination *int64 } @@ -291,9 +289,9 @@ func (f *Int64Flag) String() string { return FlagStringer(f) } -func (f *Int64Flag) PredictArgs(c *Context, a complete.Args) []string { +func (f *Int64Flag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -341,7 +339,7 @@ type IntFlag struct { DefaultValue int DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, int) error Destination *int } @@ -352,9 +350,9 @@ func (f *IntFlag) String() string { return FlagStringer(f) } -func (f *IntFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *IntFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -401,7 +399,7 @@ type IntSliceFlag struct { Hidden bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, []int) error Destination *IntSlice } @@ -412,9 +410,9 @@ func (f *IntSliceFlag) String() string { return FlagStringer(f) } -func (f *IntSliceFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *IntSliceFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -463,7 +461,7 @@ type Int64SliceFlag struct { Hidden bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, []int64) error Destination *Int64Slice } @@ -474,9 +472,9 @@ func (f *Int64SliceFlag) String() string { return FlagStringer(f) } -func (f *Int64SliceFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *Int64SliceFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -525,7 +523,7 @@ type Float64SliceFlag struct { Hidden bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, []float64) error Destination *Float64Slice } @@ -536,9 +534,9 @@ func (f *Float64SliceFlag) String() string { return FlagStringer(f) } -func (f *Float64SliceFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *Float64SliceFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -588,7 +586,7 @@ type StringFlag struct { DefaultValue string DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, string) error Destination *string } @@ -599,9 +597,9 @@ func (f *StringFlag) String() string { return FlagStringer(f) } -func (f *StringFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *StringFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -648,7 +646,7 @@ type StringSliceFlag struct { Hidden bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, []string) error Destination *StringSlice } @@ -659,9 +657,9 @@ func (f *StringSliceFlag) String() string { return FlagStringer(f) } -func (f *StringSliceFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *StringSliceFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -710,7 +708,7 @@ type StringMapFlag struct { Hidden bool DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, map[string]string) error Destination *StringMap } @@ -721,9 +719,9 @@ func (f *StringMapFlag) String() string { return FlagStringer(f) } -func (f *StringMapFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *StringMapFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -773,7 +771,7 @@ type Uint64Flag struct { DefaultValue uint64 DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, uint64) error Destination *uint64 } @@ -784,9 +782,9 @@ func (f *Uint64Flag) String() string { return FlagStringer(f) } -func (f *Uint64Flag) PredictArgs(c *Context, a complete.Args) []string { +func (f *Uint64Flag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } @@ -834,7 +832,7 @@ type UintFlag struct { DefaultValue uint DefaultText string Required bool - ArgsPredictor func(*Context, complete.Args) []string + ArgsPredictor func(*Context, string) []string Validator func(*Context, uint) error Destination *uint } @@ -845,9 +843,9 @@ func (f *UintFlag) String() string { return FlagStringer(f) } -func (f *UintFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *UintFlag) PredictArgs(c *Context, prefix string) []string { if f.ArgsPredictor != nil { - return f.ArgsPredictor(c, a) + return f.ArgsPredictor(c, prefix) } return []string{} } diff --git a/funcs.go b/funcs.go index 51504a3..e8b5672 100644 --- a/funcs.go +++ b/funcs.go @@ -19,10 +19,8 @@ package console -import "github.com/posener/complete" - // ShellCompleteFunc is an action to execute when the shell completion flag is set -type ShellCompleteFunc func(*Context, complete.Args) []string +type ShellCompleteFunc func(*Context, string) []string // BeforeFunc is an action to execute before any subcommands are run, but after // the context is ready if a non-nil error is returned, no subcommands are run diff --git a/go.mod b/go.mod index a66766a..007ac75 100644 --- a/go.mod +++ b/go.mod @@ -6,21 +6,19 @@ require ( github.com/agext/levenshtein v1.2.3 github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/errors v0.9.1 - github.com/posener/complete v1.2.3 + github.com/posener/complete/v2 v2.1.0 github.com/rs/zerolog v1.33.0 github.com/symfony-cli/terminal v1.0.7 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c ) require ( - github.com/hashicorp/errwrap v1.0.0 // indirect - github.com/hashicorp/go-multierror v1.0.0 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/posener/script v1.2.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect - github.com/stretchr/testify v1.8.4 // indirect golang.org/x/sys v0.21.0 // indirect golang.org/x/term v0.21.0 // indirect ) diff --git a/go.sum b/go.sum index 35668b4..f522bd7 100644 --- a/go.sum +++ b/go.sum @@ -2,14 +2,9 @@ github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7l github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= -github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= -github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= -github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -30,16 +25,16 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo= -github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= +github.com/posener/complete/v2 v2.1.0 h1:IpAWxMyiJ6zDSoq+QmEBF0thpOramC0kYuEFBTcQeTI= +github.com/posener/complete/v2 v2.1.0/go.mod h1:AkzsSVGx4ysH/4OhZf57dr4yszGXgFmXsP/VNwlaW7U= +github.com/posener/script v1.2.0 h1:DrZz0qFT8lCLkYNi1PleLDANFnKxJ2VmlNPJbAkVLsE= +github.com/posener/script v1.2.0/go.mod h1:s4sVvRXtdc/1aK6otTSeW2BVXndO8MsoOVUwK74zcg4= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/symfony-cli/terminal v1.0.7 h1:57L9PUTE2cHfQtP8Ti8dyiiPEYlQ1NBIDpMJ3RPEGPc= @@ -51,9 +46,7 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA= golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/logging_flags.go b/logging_flags.go index 0129469..441a107 100644 --- a/logging_flags.go +++ b/logging_flags.go @@ -26,7 +26,7 @@ import ( "strings" "github.com/pkg/errors" - "github.com/posener/complete" + "github.com/posener/complete/v2" "github.com/symfony-cli/terminal" ) @@ -100,7 +100,7 @@ func VerbosityFlag(name, alias, shortAlias string) *verbosityFlag { } } -func (f *verbosityFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *verbosityFlag) PredictArgs(c *Context, prefix string) []string { return []string{"1", "2", "3", "4"} } @@ -167,26 +167,26 @@ func (f *verbosityFlag) String() string { return fmt.Sprintf("%s\t%s", names, strings.TrimSpace(usage)) } -func (f *verbosityFlag) addToPosenerFlags(c *Context, flags complete.Flags) { +func (f *verbosityFlag) addToPosenerFlags(c *Context, flags map[string]complete.Predictor) { for i, n := 1, len(terminal.LogLevels)-2; i <= n; i++ { name := prefixFor(f.ShortAlias) name += strings.Repeat(f.ShortAlias, i) - flags[name] = complete.PredictFunc(func(a complete.Args) []string { - return f.PredictArgs(c, a) + flags[name] = complete.PredictFunc(func(prefix string) []string { + return f.PredictArgs(c, prefix) }) } for _, alias := range f.Aliases { if alias != "" { - flags[prefixFor(alias)+alias] = complete.PredictFunc(func(a complete.Args) []string { - return f.PredictArgs(c, a) + flags[prefixFor(alias)+alias] = complete.PredictFunc(func(prefix string) []string { + return f.PredictArgs(c, prefix) }) } } if f.Name != "" { - flags[prefixFor(f.Name)+f.Name] = complete.PredictFunc(func(a complete.Args) []string { - return f.PredictArgs(c, a) + flags[prefixFor(f.Name)+f.Name] = complete.PredictFunc(func(prefix string) []string { + return f.PredictArgs(c, prefix) }) } } diff --git a/output_flags.go b/output_flags.go index fd8e649..67a61e3 100644 --- a/output_flags.go +++ b/output_flags.go @@ -26,7 +26,6 @@ import ( "strconv" "github.com/pkg/errors" - "github.com/posener/complete" "github.com/symfony-cli/terminal" ) @@ -96,7 +95,7 @@ func (f *quietFlag) ForApp(app *Application) *quietFlag { } } -func (f *quietFlag) PredictArgs(c *Context, a complete.Args) []string { +func (f *quietFlag) PredictArgs(*Context, string) []string { return []string{"true", "false", ""} } From 6ea220bbc3031a021a8e44c0c196b33b4e86cb02 Mon Sep 17 00:00:00 2001 From: Tugdual Saunier Date: Wed, 12 Jun 2024 20:44:40 +0200 Subject: [PATCH 2/2] Autocomplete: try to fix panic --- resources/completion.fish | 1 + resources/completion.zsh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/completion.fish b/resources/completion.fish index c82fa9e..1b38d13 100644 --- a/resources/completion.fish +++ b/resources/completion.fish @@ -24,6 +24,7 @@ # function __complete_{{ .App.HelpName }} + set -lx COMP_POINT (commandline -C) set -lx COMP_LINE (commandline -cp) test -z (commandline -ct) and set COMP_LINE "$COMP_LINE " diff --git a/resources/completion.zsh b/resources/completion.zsh index 9abfeae..c97edb1 100644 --- a/resources/completion.zsh +++ b/resources/completion.zsh @@ -71,7 +71,7 @@ _complete_{{ .App.HelpName }}() { comp=${comp//:/\\:} completions+=${comp} fi - done < <(COMP_LINE="$words" ${words[0]} ${_SF_CMD:-${words[1]}} self:autocomplete) + done < <(COMP_LINE="$words" COMP_POINT="$((CURRENT-1))" ${words[0]} ${_SF_CMD:-${words[1]}} self:autocomplete) # Let inbuilt _describe handle completions eval _describe "completions" completions $flagPrefix