scripts: Parse options with getopts builtin

This commit is contained in:
Albert Ou
2019-08-31 01:14:39 -07:00
parent ab888d32a7
commit 40a94dd08e

View File

@@ -12,12 +12,16 @@ RDIR=$(pwd)
PRECOMPILED_REPO_HASH=56a40961c98db5e8f904f15dc6efd0870bfefd9e PRECOMPILED_REPO_HASH=56a40961c98db5e8f904f15dc6efd0870bfefd9e
function usage usage() {
{ echo "usage: ${0} [riscv-tools | esp-tools | ec2fast]"
echo "usage: ./scripts/build-toolchains.sh [riscv-tools] [esp-tools] [ec2fast | --ec2fast] "
echo " riscv: if set, builds the riscv toolchain (this is also the default)" echo " riscv: if set, builds the riscv toolchain (this is also the default)"
echo " hwacha: if set, builds esp-tools toolchain" echo " hwacha: if set, builds esp-tools toolchain"
echo " ec2fast: if set, pulls in a pre-compiled RISC-V toolchain for an EC2 manager instance" echo " ec2fast: if set, pulls in a pre-compiled RISC-V toolchain for an EC2 manager instance"
exit "$1"
}
error() {
echo "${0##*/}: ${1}" >&2
} }
#taken from riscv-tools to check for open-ocd autoconf versions #taken from riscv-tools to check for open-ocd autoconf versions
@@ -28,42 +32,38 @@ check_version() {
) )
} }
if [ "$1" == "--help" -o "$1" == "-h" -o "$1" == "-H" ]; then
usage
exit 3
fi
TOOLCHAIN="riscv-tools" TOOLCHAIN="riscv-tools"
EC2FASTINSTALL="false" EC2FASTINSTALL="false"
FASTINSTALL="false" FASTINSTALL="false"
while test $# -gt 0
do while getopts 'hH-:' opt ; do
case "$1" in case $opt in
riscv-tools) h|H)
TOOLCHAIN="riscv-tools" usage 3 ;;
;; -)
esp-tools) case $OPTARG in
TOOLCHAIN="esp-tools" help)
;; usage 3 ;;
ec2fast | --ec2fast) # I don't want to break this api ec2fast) # Preserve compatibility
EC2FASTINSTALL=true EC2FASTINSTALL=true ;;
;; *)
-h | -H | --help) error "invalid option: --${OPTARG}"
usage usage 1 ;;
exit 3 esac ;;
;; *)
--*) echo "ERROR: bad option $1" error "invalid option: -${opt}"
usage usage 1 ;;
exit 1
;;
*) echo "ERROR: bad argument $1"
usage
exit 2
;;
esac esac
shift
done done
shift $((OPTIND - 1))
if [ "$1" = ec2fast ] ; then
EC2FASTINSTALL=true
elif [ -z "$1" ] ; then
TOOLCHAIN="$1"
fi
if [ "$EC2FASTINSTALL" = "true" ]; then if [ "$EC2FASTINSTALL" = "true" ]; then
if [ "$TOOLCHAIN" = "riscv-tools" ]; then if [ "$TOOLCHAIN" = "riscv-tools" ]; then