Skip to content

Commit 2b6ea89

Browse files
committed
handle completions with spaces + case insensitive enums
1 parent 64b9c1e commit 2b6ea89

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

completion/bash.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ func (b *bash) ProgramName() string {
4343

4444
const bashCompletionTemplate = `
4545
_generate_{{.Name}}_completions() {
46-
# Capture the line excluding the command, and everything after the current word
4746
local args=("${COMP_WORDS[@]:1:COMP_CWORD}")
4847
49-
# Set COMPLETION_MODE and call the command with the arguments, capturing the output
50-
local completions=$(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")
48+
declare -a output
49+
mapfile -t output < <(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")
5150
52-
# Use the command's output to generate completions for the current word
53-
COMPREPLY=($(compgen -W "$completions" -- "${COMP_WORDS[COMP_CWORD]}"))
51+
declare -a completions
52+
mapfile -t completions < <( compgen -W "$(printf '%q ' "${output[@]}")" -- "$2" )
5453
55-
# Ensure no files are shown, even if there are no matches
56-
if [ ${#COMPREPLY[@]} -eq 0 ]; then
57-
COMPREPLY=()
58-
fi
54+
local comp
55+
COMPREPLY=()
56+
for comp in "${completions[@]}"; do
57+
COMPREPLY+=("$(printf "%q" "$comp")")
58+
done
5959
}
6060
# Setup Bash to use the function for completions for '{{.Name}}'
6161
complete -F _generate_{{.Name}}_completions {{.Name}}

completion/powershell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ $_{{.Name}}_completions = {
8080
Invoke-Expression $Command | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
8181
"$_" | _{{.Name}}_escapeStringWithSpecialChars
8282
}
83-
rm env:COMPLETION_MODE
83+
$env:COMPLETION_MODE = ''
8484
}
8585
Register-ArgumentCompleter -CommandName {{.Name}} -ScriptBlock $_{{.Name}}_completions
8686
`

completion/zsh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const zshCompletionTemplate = `
4242
_{{.Name}}_completions() {
4343
local -a args completions
4444
args=("${words[@]:1:$#words}")
45-
completions=($(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}"))
45+
completions=(${(f)"$(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")"})
4646
compadd -a completions
4747
}
4848
compdef _{{.Name}}_completions {{.Name}}

option.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ func (optSet OptionSet) ByFlag(flag string) *Option {
352352
}
353353
for i := range optSet {
354354
opt := &optSet[i]
355-
if opt.Flag == flag || opt.FlagShorthand == flag {
355+
if opt.Flag == flag {
356356
return opt
357357
}
358358
}

values.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func EnumOf(v *string, choices ...string) *Enum {
530530

531531
func (e *Enum) Set(v string) error {
532532
for _, c := range e.Choices {
533-
if v == c {
533+
if strings.EqualFold(v, c) {
534534
*e.Value = v
535535
return nil
536536
}
@@ -642,7 +642,7 @@ type EnumArray struct {
642642

643643
func (e *EnumArray) Append(s string) error {
644644
for _, c := range e.Choices {
645-
if s == c {
645+
if strings.EqualFold(s, c) {
646646
*e.Value = append(*e.Value, s)
647647
return nil
648648
}
@@ -658,7 +658,7 @@ func (e *EnumArray) Replace(ss []string) error {
658658
for _, s := range ss {
659659
found := false
660660
for _, c := range e.Choices {
661-
if s == c {
661+
if strings.EqualFold(s, c) {
662662
found = true
663663
break
664664
}
@@ -680,11 +680,12 @@ func (e *EnumArray) Set(v string) error {
680680
if err != nil {
681681
return err
682682
}
683-
err = e.Replace(ss)
684-
if err != nil {
685-
return err
683+
for _, s := range ss {
684+
err := e.Append(s)
685+
if err != nil {
686+
return err
687+
}
686688
}
687-
*e.Value = append(*e.Value, ss...)
688689
return nil
689690
}
690691

0 commit comments

Comments
 (0)