Skip to content

Instantly share code, notes, and snippets.

@takuya
Last active December 25, 2017 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuya/6ec7829263fae7d20055f728040af1bb to your computer and use it in GitHub Desktop.
Save takuya/6ec7829263fae7d20055f728040af1bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
### 補完関数。
_desktop_change(){
curr="${COMP_WORDS[$COMP_CWORD-1]}"
cmd=${COMP_WORDS[0]}
case $COMP_CWORD in
1)
if [[ $cmd == $curr ]] ; then
opts=$( $cmd --help | $(which grep) -Po '(?<=\s\s)\-[a-z]' | sort | uniq )
COMPREPLY=( $(compgen -W "$opts" -- ${COMP_WORDS[COMP_CWORD]} ) )
return 0;
fi
;;
2)
case ${COMP_WORDS[1]} in
-c|-r)
COMPREPLY=( $(compgen -W "$( $cmd -l | xargs -I@ echo "'@'")" ${COMP_WORDS[COMP_CWORD]} ) )
return 0
;;
*)
;;
esac
;;
esac
}
complete -F _desktop_change desktop-change
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment