Last active
April 28, 2019 12:11
-
-
Save hauke96/d069bcf32ec35c66412e9ee50b0295a8 to your computer and use it in GitHub Desktop.
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 | |
# Global variables | |
package= | |
command= | |
# Some have default values | |
parsing_succeeded=true | |
# Parses the install specific arguments | |
# Needed parameter: $@ | |
function parse_install_args(){ | |
for (( i=1; i<=$#; i++ )) | |
do | |
arg=${@:$i:1} # Gets the string i | |
val=${@:$i+1:1} # Gets the string i+1 | |
case $arg in | |
-h|--help) | |
echo "Show help text for the install command here" | |
exit 0 | |
;; | |
-p) | |
package=$val | |
# The parse the next argument and not this value | |
((i++)) | |
;; | |
--package=*) | |
# split at = char and remove the shortest match from beginning | |
package=${arg#*=} | |
;; | |
*) | |
echo "Unknown argument number $i: '$arg'" | |
parsing_succeeded=false | |
;; | |
esac | |
done | |
} | |
# Parses the list specific arguments | |
# Needed parameter: $@ | |
function parse_list_args(){ | |
for (( i=1; i<=$#; i++ )) | |
do | |
arg=${@:$i:1} # Gets the string i | |
val=${@:$i+1:1} # Gets the string i+1 | |
case $arg in | |
-h|--help) | |
echo "Show help text for the list command here" | |
exit 0 | |
;; | |
*) | |
echo "Unknown argument number $i: '$arg'" | |
parsing_succeeded=false | |
;; | |
esac | |
done | |
} | |
arg=${@:1:1} | |
# To parse the next argument and not the command again | |
shift | |
# First we accept a command or the help-flag | |
case $arg in | |
-h|--help) | |
echo "Show help text here" | |
exit 0 | |
;; | |
install) | |
command=install | |
parse_install_args $@ | |
;; | |
list) | |
command=list | |
parse_list_args $@ | |
;; | |
*) | |
echo "Unknown command '$arg'." | |
exit 1 | |
;; | |
esac | |
# Only if parsing succeeded, execute the actual application logic | |
# (This might not always be wanted, but change it as you like) | |
if [ "$parsing_succeeded" == "true" ] | |
then | |
case $command in | |
install) | |
echo "Install package '$package'" | |
# Call install-routine | |
;; | |
list) | |
echo "List all packages:" | |
# Call list-routine here | |
;; | |
"") | |
echo "Missing command" | |
exit 1 | |
;; | |
*) | |
echo "Unknown command" | |
exit 1 | |
;; | |
esac | |
else | |
echo "Error during parsing. Exit application." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment