Skip to content

feat: add completion install api #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

@ethanndickson ethanndickson Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes shouldn't really need reviewing but it's worth mentioning pflag only lets you use the shorthand with a single hyphen, so I'm getting rid of the completion for now.

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
Loading