build.sh: improve command line parsing

The previous code was simple and worked but it didn't scale.

With one --with-*-path argument, we have only one elif clause. With 2
--with-*-path arguments we end up with 4 elif clauses. And with 3
--with-*-path arguments we end up with 13 elif clauses which is way
too much.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
Acked-by: Adrien 'neox' Bourmault <neox@a-lec.org>
This commit is contained in:
Denis 'GNUtoo' Carikli 2023-04-03 02:33:26 +02:00
parent e4fe5e0e56
commit eb5845b8b1
Signed by: GNUtoo
GPG Key ID: 5F5DFCC14177E263
1 changed files with 38 additions and 18 deletions

View File

@ -61,25 +61,45 @@ sync_repo()
fi
}
if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" == "--help" ] ; then
help_missing_arg()
{
printf "Error: Argument of %s is missing.\n\n" "$1"
help
}
i=1
while [ $i -le $# ] ; do
opt="$(eval echo \$$i)"
case "${opt}" in
-h|--help)
help
exit 0
elif [ $# -eq 2 ] && [ "$1" = "--with-lbwww-path" ] ; then
lbwww_path="$(realpath $2)"
elif [ $# -eq 2 ] && [ "$1" = "--with-lbwww-img-path" ] ; then
lbwww_img_path="$(realpath $2)"
elif [ $# -eq 4 ] && [ "$1" = "--with-lbwww-path" ] && \
[ "$3" = "--with-lbwww-img-path" ] ; then
lbwww_path="$(realpath $2)"
lbwww_img_path="$(realpath $4)"
elif [ $# -eq 4 ] && [ "$1" = "--with-lbwww-img-path" ] && \
[ "$3" = "--with-lbwww-path" ] ; then
lbwww_img_path="$(realpath $2)"
lbwww_path="$(realpath $4)"
elif [ $# -ne 0 ] ; then
;;
--with-lbwww-path)
if [ $i -ge $# ] ; then
help_missing_arg "--with-lbwww-path"
exit ${EX_USAGE}
fi
lbwww_path="$(eval echo \$$(expr $i + 1))"
i="$(expr $i + 1)"
;;
--with-lbwww-img-path)
if [ $i -ge $# ] ; then
help_missing_arg "--with-lbwww-img-path"
exit ${EX_USAGE}
fi
lbwww_img_path="$(eval echo \$$(expr $i + 1))"
i="$(expr $i + 1)"
;;
*)
help
exit ${EX_USAGE}
fi
;;
esac
i="$(expr $i + 1)"
done
set -e