Created
May 10, 2018 14:23
-
-
Save pablordoricaw/9c3f89a66d654fdbdcf762eafb048842 to your computer and use it in GitHub Desktop.
Code example using getopts in bash with short options and a subcommand with it's own short options
This file contains hidden or 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
#!/bin/bash | |
### | |
# This is a code example showing how to use getops to parse short options and a | |
# subcommand with short options of it's own. Based on Kevin Sookocheff's post: | |
# https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/ | |
# | |
# Please feel free to use it and modify it as you see fit. Any questions and/or | |
# recommendations are more than welcome. | |
### | |
# get the scripts name for the usage message | |
_filename=$0 | |
# verbose mode deactivated | |
verbose=false | |
# argument variables | |
a="" | |
f="" | |
# Display usage message function | |
usage(){ | |
echo "Usage:" | |
echo -e "\t$_filename -h Display usage message" | |
echo -e "\t$_filename [-v] pow -a <arg> -f <arg> Options with required arg" | |
} | |
if [ ! 0 == $# ] # If options provided then | |
then | |
while getopts ":hv" opt; do # Go through the options | |
case $opt in | |
h ) # Help | |
usage | |
exit 0 # Exit correctly | |
;; | |
v ) # Debug | |
echo "Read verbose flag" | |
verbose=true | |
;; | |
? ) # Invalid option | |
echo "[ERROR]: Invalid option: -${OPTARG}" | |
usage | |
exit 1 # Exit with erro | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
subcommand=$1; shift # Get subcommand and shift to next option | |
case "$subcommand" in | |
pow ) # pow chicka pow pow | |
unset OPTIND # in order to make -v pow -a <arg> -f <arg> work -> https://stackoverflow.com/questions/2189281/how-to-call-getopts-in-bash-multiple-times | |
if [ $verbose == true ]; then echo "Read pow subcommand"; fi | |
if [ ! 0 == $# ] # if options provided | |
then | |
if [ $verbose == true ]; then echo "Remaining args are: <${@}>"; fi | |
while getopts ":a:f:" opt; do | |
case $opt in | |
a ) # option -a with required argument | |
echo "Read option -a with argument ${OPTARG}" | |
a=$OPTARG | |
;; | |
f ) # option -f with required argument | |
echo "Read option -f with argument ${OPTARG}" | |
f=$OPTARG | |
;; | |
: ) # catch no argument provided | |
echo "[ERROR]: option -${OPTARG} requires an argument" | |
usage | |
exit 1 | |
;; | |
? ) # Invalid option | |
echo "[ERROR]: Invalid option: -${OPTARG}" | |
usage | |
exit 1 # Exit with erro | |
;; | |
esac | |
done | |
if [ -z $a ] || [ -z $f ] # if a or f aren't set | |
then | |
echo "[ERROR]: Both -a & -f are required" # throw error | |
usage | |
exit 1 | |
fi | |
shift $((OPTIND-1)) | |
else | |
usage | |
exit 1 | |
fi | |
;; | |
* ) # Invalid subcommand | |
if [ ! -z $subcommand ]; then # Don't show if no subcommand provided | |
echo "Invalid subcommand: $subcommand" | |
fi | |
usage | |
exit 1 # Exit with error | |
;; | |
esac | |
else # else if no options provided throw error | |
usage | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment