Skip to content

Commit

Permalink
Add bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
csutorasa committed Oct 4, 2024
1 parent 3a6c19e commit 85a798d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
__icon_metrics_bash_completion() {
# Flags from the application
local flags=("--config")
local cur="${COMP_WORDS[$COMP_CWORD]}"
local isflag="$(($COMP_CWORD % 2))"
# Flag
if [ "$isflag" -eq "1" ]
then
# flags
local remaining=()
for flag in "${flags[@]}"
do
local found=0
for arg in "${COMP_WORDS[@]}"
do
if [ "$(($COMP_CWORD % 2))" -eq 1 ] && [ "$flag" = "$arg" ]
then
found=1
break
fi
done
if [ "$found" -eq "0" ]
then
remaining+=("$flag")
fi
done
COMPREPLY=($(compgen -W "$(echo "${remaining[@]}")" -- "$cur"))
return
fi
# Flag value
if [ "$isflag" -eq "0" ]
then
# Based on the flag
local flag_index=$(($COMP_CWORD - 1))
case "${COMP_WORDS[$flag_index]}" in
--config)
compopt -o nospace
if [ "${cur: -1}" = "/" ]
then
COMPREPLY=($(compgen -d -S "/" "$cur") $(compgen -W "$(compgen -G "$cur*.yml")" -- "$b") $(compgen -W "$(compgen -G "$cur*.yaml")" -- "$b"))
return
fi
local d=$(dirname "$cur")
local b=$(basename "$cur")
if [ "$d" = "." ]
then
COMPREPLY=($(compgen -d -S "/" "$cur") $(compgen -W "$(compgen -G "*.yml")" -- "$b") $(compgen -W "$(compgen -G "*.yaml")" -- "$b"))
return
else
COMPREPLY=($(compgen -d -S "/" "$cur") $(compgen -W "$(compgen -G "$d/*.yml")" -- "$b") $(compgen -W "$(compgen -G "$d/*.yaml")" -- "$b"))
return
fi
;;
*)
# Unexpected flag
COMPREPLY=()
return
;;
esac
fi
# Unexpected number of arguments
COMPREPLY=()
return
}

complete -F __icon_metrics_bash_completion icon-metrics
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next

Bump go version to 1.23.
New bash-completion is available.

## 1.3.0

Expand Down

0 comments on commit 85a798d

Please sign in to comment.