libreboot-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lbwww-build][PATCH v1 4/5] build.sh: improve command line parsing


From: Adrien Bourmault
Subject: Re: [lbwww-build][PATCH v1 4/5] build.sh: improve command line parsing
Date: Tue, 4 Apr 2023 14:49:11 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1

Acked-by: Adrien 'neox' Bourmault <neox@a-lec.org>

Le 03/04/2023 à 17:21, Denis 'GNUtoo' Carikli a écrit :
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>
---
  build.sh | 56 ++++++++++++++++++++++++++++++++++++++------------------
  1 file changed, 38 insertions(+), 18 deletions(-)

diff --git a/build.sh b/build.sh
index 0e1eb41..bc56d41 100755
--- a/build.sh
+++ b/build.sh
@@ -61,25 +61,45 @@ sync_repo()
        fi
  }
-if [ $# -eq 1 ] && [ "$1" = "-h" -o "$1" == "--help" ] ; then
-       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
+help_missing_arg()
+{
+       printf "Error: Argument of %s is missing.\n\n" "$1"
        help
-       exit ${EX_USAGE}
-fi
+}
+
+i=1
+while [ $i -le $# ] ; do
+       opt="$(eval echo \$$i)"
+
+       case "${opt}" in
+               -h|--help)
+                       help
+                       exit 0
+                       ;;
+               --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}
+                       ;;
+       esac
+
+       i="$(expr $i + 1)"
+done
set -e



Attachment: OpenPGP_0xAAD6B069819E6979.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]