Created
October 3, 2022 01:53
-
-
Save MattSegal/d8396561cf5a4c676b7c9cb5d24a96ad to your computer and use it in GitHub Desktop.
a tiny bash function that turns a folder containing bash scripts into a CLI with auto complete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function matt { | |
if [[ ! -d "./matt "]]; then | |
echo "No matt folder found." | |
else | |
options=$(ls matt | grep ".*\.sh$" | cut -d. -f1) | |
if [[ -z "$1" ]]; then | |
echo -e "Options:\n\n$options\n" | |
else | |
echo "Running $1" | |
bash ./matt/$1.sh ${@:2} | |
fi | |
fi | |
} | |
function _matt_completions { | |
if [[ -d "./matt" ]]; then | |
if [ "${#COMP_WORDS[@]}" != "2" ]; then | |
return | |
fi | |
options=$(ls matt | grep ".*\.sh$" | cut -d. -f1) | |
COMPREPLY=($(compgen -W "$options" "${COMP_WORDS[1]}")) | |
fi | |
} | |
complete -F _matt_completions matt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment