@@ -7,7 +7,10 @@ import (
77 "os"
88 "runtime/debug"
99
10- "github.com/posener/complete/v2"
10+ "github.com/pkg/errors"
11+ "github.com/posener/complete"
12+ "github.com/rs/zerolog"
13+ "github.com/symfony-cli/terminal"
1114)
1215
1316func init () {
@@ -47,41 +50,50 @@ func registerAutocompleteCommands(a *Application) {
4750}
4851
4952func AutocompleteAppAction (c * Context ) error {
53+ // connect posener/complete logger to our logging facilities
54+ logger := terminal .Logger .WithLevel (zerolog .DebugLevel )
55+ complete .Log = func (format string , args ... interface {}) {
56+ logger .Msgf ("completion | " + format , args ... )
57+ }
58+
5059 cmd := complete.Command {
51- Flags : map [ string ] complete.Predictor {} ,
52- Sub : map [ string ] * complete.Command {} ,
60+ GlobalFlags : make ( complete.Flags ) ,
61+ Sub : make ( complete.Commands ) ,
5362 }
5463
5564 // transpose registered commands and flags to posener/complete equivalence
5665 for _ , command := range c .App .VisibleCommands () {
5766 subCmd := command .convertToPosenerCompleteCommand (c )
5867
5968 for _ , name := range command .Names () {
60- cmd .Sub [name ] = & subCmd
69+ cmd .Sub [name ] = subCmd
6170 }
6271 }
6372
6473 for _ , f := range c .App .VisibleFlags () {
6574 if vf , ok := f .(* verbosityFlag ); ok {
66- vf .addToPosenerFlags (c , cmd .Flags )
75+ vf .addToPosenerFlags (c , cmd .GlobalFlags )
6776 continue
6877 }
6978
7079 predictor := ContextPredictor {f , c }
7180
7281 for _ , name := range f .Names () {
7382 name = fmt .Sprintf ("%s%s" , prefixFor (name ), name )
74- cmd .Flags [name ] = predictor
83+ cmd .GlobalFlags [name ] = predictor
7584 }
7685 }
7786
78- cmd .Complete (c .App .HelpName )
87+ if ! complete .New (c .App .HelpName , cmd ).Complete () {
88+ return errors .New ("Could not run auto-completion" )
89+ }
90+
7991 return nil
8092}
8193
8294func (c * Command ) convertToPosenerCompleteCommand (ctx * Context ) complete.Command {
8395 command := complete.Command {
84- Flags : map [ string ] complete.Predictor {} ,
96+ Flags : make ( complete.Flags , 0 ) ,
8597 }
8698
8799 for _ , f := range c .VisibleFlags () {
@@ -98,16 +110,16 @@ func (c *Command) convertToPosenerCompleteCommand(ctx *Context) complete.Command
98110 return command
99111}
100112
101- func (c * Command ) PredictArgs (ctx * Context , prefix string ) []string {
113+ func (c * Command ) PredictArgs (ctx * Context , a complete. Args ) []string {
102114 if c .ShellComplete != nil {
103- return c .ShellComplete (ctx , prefix )
115+ return c .ShellComplete (ctx , a )
104116 }
105117
106118 return nil
107119}
108120
109121type Predictor interface {
110- PredictArgs (* Context , string ) []string
122+ PredictArgs (* Context , complete. Args ) []string
111123}
112124
113125// ContextPredictor determines what terms can follow a command or a flag
@@ -119,6 +131,6 @@ type ContextPredictor struct {
119131}
120132
121133// Predict invokes the predict function and implements the Predictor interface
122- func (p ContextPredictor ) Predict (prefix string ) []string {
123- return p .predictor .PredictArgs (p .ctx , prefix )
134+ func (p ContextPredictor ) Predict (a complete. Args ) []string {
135+ return p .predictor .PredictArgs (p .ctx , a )
124136}
0 commit comments