cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Completion does not work correctly if there is a @ symbol in the string

Open Bernigend opened this issue 9 months ago • 2 comments

Summary

If the flag can take values that contain the "@" character, then value substitution stops working after entering this character.

Examples

All available values

[$] clddev systemctl status2 --unit
group1@subunit   group2@subunit2  unit1            unit1_long

Value which starts with group

[$] clddev systemctl status2 --unit group
group1@subunit   group2@subunit2

Value with symbol @

[$] clddev systemctl status2 --unit group1@sub

And it is empty

How to reproduce

package status2

import (
	"github.com/spf13/cobra"
)

func Cmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:  "status2",
		RunE: Handler,
	}

	cmd.Flags().String("unit", "", "systemctl status unit")
	cmd.RegisterFlagCompletionFunc("unit", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		return []string{
			"unit1",
			"unit1_long",
			"group1@subunit",
			"group2@subunit2",
		}, 0
	})

	return cmd
}

func Handler(cmd *cobra.Command, args []string) error {
	return nil
}

Log

========= starting completion logic ==========
cur is , words[*] is clddev systemctl status2 --unit , #words[@] is 5, cword is 4
Truncated words[*]: clddev systemctl status2 --unit ,
lastParam , lastChar
Adding extra empty parameter
Calling clddev __complete systemctl status2 --unit  ''
The completion directive is: 0
The completions are: unit1
unit1_long
group1@subunit
group2@subunit2

__clddev_handle_completion_types: COMP_TYPE is 63

========= starting completion logic ==========
cur is group, words[*] is clddev systemctl status2 --unit group, #words[@] is 5, cword is 4
Truncated words[*]: clddev systemctl status2 --unit group,
lastParam group, lastChar p
Calling clddev __complete systemctl status2 --unit group
The completion directive is: 0
The completions are: unit1
unit1_long
group1@subunit
group2@subunit2

__clddev_handle_completion_types: COMP_TYPE is 9

========= starting completion logic ==========
cur is group, words[*] is clddev systemctl status2 --unit group, #words[@] is 5, cword is 4
Truncated words[*]: clddev systemctl status2 --unit group,
lastParam group, lastChar p
Calling clddev __complete systemctl status2 --unit group
The completion directive is: 0
The completions are: unit1
unit1_long
group1@subunit
group2@subunit2

__clddev_handle_completion_types: COMP_TYPE is 63

========= starting completion logic ==========
cur is @, words[*] is clddev systemctl status2 --unit group1 @, #words[@] is 6, cword is 5
Truncated words[*]: clddev systemctl status2 --unit group1 @,
lastParam @, lastChar @
Calling clddev __complete systemctl status2 --unit group1 @
The completion directive is: 0
The completions are:
__clddev_handle_completion_types: COMP_TYPE is 9

========= starting completion logic ==========
cur is @, words[*] is clddev systemctl status2 --unit group1 @, #words[@] is 6, cword is 5
Truncated words[*]: clddev systemctl status2 --unit group1 @,
lastParam @, lastChar @
Calling clddev __complete systemctl status2 --unit group1 @
The completion directive is: 0
The completions are:
__clddev_handle_completion_types: COMP_TYPE is 63

========= starting completion logic ==========
cur is sub, words[*] is clddev systemctl status2 --unit group1 @ sub, #words[@] is 7, cword is 6
Truncated words[*]: clddev systemctl status2 --unit group1 @ sub,
lastParam sub, lastChar b
Calling clddev __complete systemctl status2 --unit group1 @ sub
The completion directive is: 0
The completions are:
__clddev_handle_completion_types: COMP_TYPE is 9

========= starting completion logic ==========
cur is sub, words[*] is clddev systemctl status2 --unit group1 @ sub, #words[@] is 7, cword is 6
Truncated words[*]: clddev systemctl status2 --unit group1 @ sub,
lastParam sub, lastChar b
Calling clddev __complete systemctl status2 --unit group1 @ sub
The completion directive is: 0
The completions are:
__clddev_handle_completion_types: COMP_TYPE is 63

Bernigend avatar May 07 '24 16:05 Bernigend