@@ -291,7 +291,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
291
291
}
292
292
293
293
// keep track of the completed args
294
- completedArgs := make (map [string ]struct {} )
294
+ completedArgs := make (map [string ]string )
295
295
296
296
// keep track of the completed flags
297
297
completedFlags := make (map [string ]struct {})
@@ -307,12 +307,12 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
307
307
308
308
// handle arg=value
309
309
case isArg (word ):
310
- completedArgs [wordKey (word )+ "=" ] = struct {}{}
310
+ completedArgs [wordKey (word )+ "=" ] = wordValue ( word )
311
311
312
312
// handle boolean arg
313
313
default :
314
314
if _ , exist := node .Children [positionalValueNodeID ]; exist && i > nodeIndexInWords {
315
- completedArgs [word ] = struct {}{}
315
+ completedArgs [word ] = ""
316
316
}
317
317
}
318
318
}
@@ -324,7 +324,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
324
324
// We try to complete the value of an unknown arg
325
325
return & AutocompleteResponse {}
326
326
}
327
- suggestions := AutoCompleteArgValue (ctx , argNode .Command , argNode .ArgSpec , argValuePrefix )
327
+ suggestions := AutoCompleteArgValue (ctx , argNode .Command , argNode .ArgSpec , argValuePrefix , completedArgs )
328
328
329
329
// We need to prefix suggestions with the argName to enable the arg value auto-completion.
330
330
for k , s := range suggestions {
@@ -338,7 +338,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
338
338
suggestions := []string (nil )
339
339
for key , child := range node .Children {
340
340
if key == positionalValueNodeID {
341
- for _ , positionalSuggestion := range AutoCompleteArgValue (ctx , child .Command , child .ArgSpec , wordToComplete ) {
341
+ for _ , positionalSuggestion := range AutoCompleteArgValue (ctx , child .Command , child .ArgSpec , wordToComplete , completedArgs ) {
342
342
if _ , exists := completedArgs [positionalSuggestion ]; ! exists {
343
343
suggestions = append (suggestions , positionalSuggestion )
344
344
}
@@ -380,7 +380,7 @@ func AutoComplete(ctx context.Context, leftWords []string, wordToComplete string
380
380
// AutoCompleteArgValue returns suggestions for a (argument name, argument value prefix) pair.
381
381
// Priority is given to the AutoCompleteFunc from the ArgSpec, if it is set.
382
382
// Otherwise, we use EnumValues from the ArgSpec.
383
- func AutoCompleteArgValue (ctx context.Context , cmd * Command , argSpec * ArgSpec , argValuePrefix string ) []string {
383
+ func AutoCompleteArgValue (ctx context.Context , cmd * Command , argSpec * ArgSpec , argValuePrefix string , completedArgs map [ string ] string ) []string {
384
384
if argSpec == nil {
385
385
return nil
386
386
}
@@ -401,6 +401,12 @@ func AutoCompleteArgValue(ctx context.Context, cmd *Command, argSpec *ArgSpec, a
401
401
possibleValues = argSpec .EnumValues
402
402
}
403
403
404
+ // Complete arg value using list verb if possible
405
+ // "instance server get <tab>" completes "server-id" arg with "id" in instance server list
406
+ if len (possibleValues ) == 0 && ExtractBetaMode (ctx ) {
407
+ possibleValues = AutocompleteGetArg (ctx , cmd , argSpec , completedArgs )
408
+ }
409
+
404
410
suggestions := []string (nil )
405
411
for _ , value := range possibleValues {
406
412
if strings .HasPrefix (value , argValuePrefix ) {
@@ -424,6 +430,14 @@ func wordKey(word string) string {
424
430
return strings .SplitN (word , "=" , 2 )[0 ]
425
431
}
426
432
433
+ func wordValue (word string ) string {
434
+ words := strings .SplitN (word , "=" , 2 )
435
+ if len (words ) >= 2 {
436
+ return words [1 ]
437
+ }
438
+ return ""
439
+ }
440
+
427
441
func isArg (wordToComplete string ) bool {
428
442
return strings .Contains (wordToComplete , "=" )
429
443
}
@@ -460,7 +474,7 @@ func hasPrefix(key, wordToComplete string) bool {
460
474
461
475
// keySuggestion will suggest the next key available for the map (or array) argument.
462
476
// Keys are suggested in ascending order arg.0, arg.1, arg.2...
463
- func keySuggestion (key string , completedArg map [string ]struct {} , wordToComplete string ) []string {
477
+ func keySuggestion (key string , completedArg map [string ]string , wordToComplete string ) []string {
464
478
splitKey := strings .Split (key , "." )
465
479
splitWordToComplete := strings .Split (wordToComplete , "." )
466
480
0 commit comments