Skip to content

Commit

Permalink
update completion scripts for urfave/cli/v3
Browse files Browse the repository at this point in the history
follow up of cab383f
  • Loading branch information
bartekpacia committed Oct 30, 2024
1 parent 964e31d commit ffd3b9e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 45 deletions.
21 changes: 11 additions & 10 deletions autocomplete/bash_autocomplete_fhome
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#! /bin/bash

: ${PROG:=fhome}
#!/usr/bin/env bash

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
__fallback_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
__start-fhome() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
__fallback_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-bash-completion"
requestComp="${words[*]} ${cur} --generate-shell-completion"
else
requestComp="${words[*]} --generate-bash-completion"
requestComp="${words[*]} --generate-shell-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start-fhome fhome
else
complete -o default -o nospace -F __start-fhome fhome
fi
21 changes: 11 additions & 10 deletions autocomplete/bash_autocomplete_fhomed
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
#! /bin/bash

: ${PROG:=fhomed}
#!/usr/bin/env bash

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
__fallback_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
__start-fhomed() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
__fallback_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-bash-completion"
requestComp="${words[*]} ${cur} --generate-shell-completion"
else
requestComp="${words[*]} --generate-bash-completion"
requestComp="${words[*]} --generate-shell-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG
if [[ $(type -t compopt) = "builtin" ]]; then
complete -o default -F __start-fhomed fhomed
else
complete -o default -o nospace -F __start-fhomed fhomed
fi
34 changes: 21 additions & 13 deletions autocomplete/zsh_autocomplete_fhome
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
#compdef fhome
compdef _fhome fhome

local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
_fhome() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}

# Don't run the completion function when being source-ed or eval-ed.
# See https://github.com/urfave/cli/issues/1874 for discussion.
if [ "$funcstack[1]" = "_fhome" ]; then
_fhome
fi
33 changes: 21 additions & 12 deletions autocomplete/zsh_autocomplete_fhomed
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#compdef fhomed
compdef _fhomed fhomed

local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
_fhomed() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-shell-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-shell-completion)}")
fi

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
# Don't run the completion function when being source-ed or eval-ed.
# See https://github.com/urfave/cli/issues/1874 for discussion.
if [ "$funcstack[1]" = "_fhomed" ]; then
_fhomed
fi

0 comments on commit ffd3b9e

Please sign in to comment.