Skip to content

Instantly share code, notes, and snippets.

@pawndev
Created July 23, 2018 11:53
Show Gist options
  • Select an option

  • Save pawndev/5ce416a3e40e44bd126d129fbc06bfc5 to your computer and use it in GitHub Desktop.

Select an option

Save pawndev/5ce416a3e40e44bd126d129fbc06bfc5 to your computer and use it in GitHub Desktop.
bash cli argument example
#!/bin/bash
params="$(getopt -o hviuc: -l help,version,install,update,clean: --name "$0" -- "$@")"
eval set -- "$params"
. ./lib/echos.sh
usage() {
shrug "Dotfiles Installer"
echo "Usage:"
echo " --help -h -? Display this help message."
echo " --install -i Install a Python package."
echo " --update -u Update a Python package."
echo " --clean -c Clean a Python package."
exit 0
}
execute=""
specific_package=""
while [[ $# -gt 0 ]] ; do
case $1 in
-h|-\?|--help)
usage
exit
;;
-v|--version)
if [ -n "$2" ]; then
echo "version: <$2>"
specific_package=$2
shift
fi
;;
-i|--install)
execute="INSTALL"
if [ -n "$2" ]; then
echo "install: <$2>"
specific_package=$2
shift
fi
;;
-u|--update)
execute="UPDATE"
if [ -n "$2" ]; then
specific_package=$2
echo "update: <$2>"
shift
fi
;;
-c|--clean)
execute="CLEAN"
if [ -n "$2" ]; then
specific_package=$2
echo "clean: <$2>"
shift
fi
;;
esac
shift
done
case $execute in
"INSTALL")
echo "INSTALL $specific_package"
;;
"UPDATE")
echo "UPDATE $specific_package"
;;
"CLEAN")
echo "CLEAN $specific_package"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment