Skip to content

Commit

Permalink
handle completions with spaces + case insensitive enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanndickson committed Aug 15, 2024
1 parent 64b9c1e commit 2b6ea89
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
18 changes: 9 additions & 9 deletions completion/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ func (b *bash) ProgramName() string {

const bashCompletionTemplate = `
_generate_{{.Name}}_completions() {
# Capture the line excluding the command, and everything after the current word
local args=("${COMP_WORDS[@]:1:COMP_CWORD}")
# Set COMPLETION_MODE and call the command with the arguments, capturing the output
local completions=$(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")
declare -a output
mapfile -t output < <(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")
# Use the command's output to generate completions for the current word
COMPREPLY=($(compgen -W "$completions" -- "${COMP_WORDS[COMP_CWORD]}"))
declare -a completions
mapfile -t completions < <( compgen -W "$(printf '%q ' "${output[@]}")" -- "$2" )
# Ensure no files are shown, even if there are no matches
if [ ${#COMPREPLY[@]} -eq 0 ]; then
COMPREPLY=()
fi
local comp
COMPREPLY=()
for comp in "${completions[@]}"; do
COMPREPLY+=("$(printf "%q" "$comp")")
done
}
# Setup Bash to use the function for completions for '{{.Name}}'
complete -F _generate_{{.Name}}_completions {{.Name}}
Expand Down
2 changes: 1 addition & 1 deletion completion/powershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $_{{.Name}}_completions = {
Invoke-Expression $Command | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object {
"$_" | _{{.Name}}_escapeStringWithSpecialChars
}
rm env:COMPLETION_MODE
$env:COMPLETION_MODE = ''
}
Register-ArgumentCompleter -CommandName {{.Name}} -ScriptBlock $_{{.Name}}_completions
`
2 changes: 1 addition & 1 deletion completion/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const zshCompletionTemplate = `
_{{.Name}}_completions() {
local -a args completions
args=("${words[@]:1:$#words}")
completions=($(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}"))
completions=(${(f)"$(COMPLETION_MODE=1 "{{.Name}}" "${args[@]}")"})
compadd -a completions
}
compdef _{{.Name}}_completions {{.Name}}
Expand Down
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (optSet OptionSet) ByFlag(flag string) *Option {
}
for i := range optSet {
opt := &optSet[i]
if opt.Flag == flag || opt.FlagShorthand == flag {
if opt.Flag == flag {
return opt
}
}
Expand Down
15 changes: 8 additions & 7 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func EnumOf(v *string, choices ...string) *Enum {

func (e *Enum) Set(v string) error {
for _, c := range e.Choices {
if v == c {
if strings.EqualFold(v, c) {
*e.Value = v
return nil
}
Expand Down Expand Up @@ -642,7 +642,7 @@ type EnumArray struct {

func (e *EnumArray) Append(s string) error {
for _, c := range e.Choices {
if s == c {
if strings.EqualFold(s, c) {
*e.Value = append(*e.Value, s)
return nil
}
Expand All @@ -658,7 +658,7 @@ func (e *EnumArray) Replace(ss []string) error {
for _, s := range ss {
found := false
for _, c := range e.Choices {
if s == c {
if strings.EqualFold(s, c) {
found = true
break
}
Expand All @@ -680,11 +680,12 @@ func (e *EnumArray) Set(v string) error {
if err != nil {
return err
}
err = e.Replace(ss)
if err != nil {
return err
for _, s := range ss {
err := e.Append(s)
if err != nil {
return err
}
}
*e.Value = append(*e.Value, ss...)
return nil
}

Expand Down

0 comments on commit 2b6ea89

Please # to comment.