From a93e9a51ec3adfa7ef799924e1b715ac28e1e57a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= <89578893+eric-glb@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:48:17 +0200 Subject: [PATCH 1/8] clean packages_all.sh output --- tests/packages_all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/packages_all.sh b/tests/packages_all.sh index a312ed7..55a8bf6 100644 --- a/tests/packages_all.sh +++ b/tests/packages_all.sh @@ -4,6 +4,6 @@ set -euo pipefail pushd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null -grep -o "^ \\+[a-zA-Z0-9_|-]\\+)" ../has | grep -o "[a-zA-Z0-9_|-]\\+" | tr "|" "\\n" | sort -f +grep -o "^ \\+[a-zA-Z0-9][a-zA-Z0-9_|-]\\+)" ../has | grep -o "[a-zA-Z0-9_|-]\\+" | tr "|" "\\n" | sort -f popd >/dev/null From add236a7de76eedd0378b9ca1b20ae97ef72cb39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= <89578893+eric-glb@users.noreply.github.com> Date: Wed, 16 Oct 2024 14:54:59 +0200 Subject: [PATCH 2/8] fix small typo in usage() --- has | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/has b/has index ba30923..730c1ff 100755 --- a/has +++ b/has @@ -66,7 +66,7 @@ Has checks the presence of various command line tools on the PATH and reports th Options: -q Silent mode -h, --help Display this help text and quit - -V, --version Show version number and quit + -v, --version Show version number and quit Examples: ${BINARY_NAME} git curl node EOF From 97677d2f9fa30fb7e99e2dc79350cce3ef0ca3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= <89578893+eric-glb@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:01:09 +0200 Subject: [PATCH 3/8] fix small typo in usage() --- has | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/has b/has index ba30923..730c1ff 100755 --- a/has +++ b/has @@ -66,7 +66,7 @@ Has checks the presence of various command line tools on the PATH and reports th Options: -q Silent mode -h, --help Display this help text and quit - -V, --version Show version number and quit + -v, --version Show version number and quit Examples: ${BINARY_NAME} git curl node EOF From bc4ec91dde7323f7ecf0020af7f96ffc0342ec92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= Date: Wed, 16 Oct 2024 16:23:54 +0200 Subject: [PATCH 4/8] fix options parser (getopt instead of getopts) --- .hasrc | 6 ----- has | 85 +++++++++++++++++++++++----------------------------------- 2 files changed, 33 insertions(+), 58 deletions(-) diff --git a/.hasrc b/.hasrc index eb2d487..e69de29 100644 --- a/.hasrc +++ b/.hasrc @@ -1,6 +0,0 @@ -# make to install/test the project -make -curl -git -# bats for testing -bats diff --git a/has b/has index ba30923..477bfe5 100755 --- a/has +++ b/has @@ -44,9 +44,8 @@ readonly FAIL="${txtbold}${txtred}${fancyx}${txtreset}" COLOR_AUTO="auto" COLOR_NEVER="never" COLOR_ALWAYS="always" -COLOR_OPTS=("${COLOR_AUTO} ${COLOR_NEVER} ${COLOR_ALWAYS}") +COLOR_OPTS=("${COLOR_AUTO}" "${COLOR_NEVER}" "${COLOR_ALWAYS}") COLOR="${COLOR_AUTO}" -COLOR_PREFIX="--color" ## These variables are used to keep track of passed and failed commands OK=0 @@ -64,9 +63,10 @@ Usage: ${BINARY_NAME} [OPTION] ... Has checks the presence of various command line tools on the PATH and reports their installed version. Options: - -q Silent mode - -h, --help Display this help text and quit - -V, --version Show version number and quit + -q Silent mode + -h, --help Display this help text and quit + -V, --version Show version number and quit + --color [auto|never|always] Use colors (default: auto) Examples: ${BINARY_NAME} git curl node EOF @@ -80,7 +80,7 @@ _version() { _set_color() { local found=0; for opt in "${COLOR_OPTS[@]}"; do - [ "${1}" == "${COLOR_PREFIX}-${opt}" ] && COLOR="${opt}" && found=1 && break + [ "${1}" == "${opt}" ] && COLOR="${opt}" && found=1 && break done [ ${found} -eq 1 ] || >&2 echo "Error: wrong flag ${1}" } @@ -338,58 +338,39 @@ __detect(){ fi } #end __detect -OPTIND=1 OUTPUT=/dev/stdout -while getopts ":qhv-" OPTION; do - case "$OPTION" in - q) - OUTPUT=/dev/null - ;; - h) +OPTS=$(getopt -o qvh --long quiet,version,help,color: -- "$@") +if [[ $? -ne 0 ]]; then _usage - exit 0 - ;; - v) - _version - exit 0 - ;; - -) - [ $OPTIND -ge 1 ] && optind=$((OPTIND - 1)) || optind=$OPTIND - eval OPTION="\$$optind" - OPTARG=$(echo "$OPTION" | cut -d'=' -f2) - OPTION=$(echo "$OPTION" | cut -d'=' -f1) - case $OPTION in - --version) - _version - exit 0 - ;; - --help) - _usage - exit 0 - ;; - ${COLOR_PREFIX}*) - _set_color "${OPTION}" - ;; - *) - printf '%s: unrecognized option '%s'\n' "${BINARY_NAME}" "${OPTARG}" - _usage - exit 2 - ;; - esac - OPTIND=1 - shift - ;; - ?) - printf '%s: unrecognized option '%s'\n' "${BINARY_NAME}" "${OPTARG}" - _usage - exit 2 - ;; + exit 1; +fi + +eval set -- "$OPTS" +while [ : ]; do + case "$1" in + -q | --quiet) + OUTPUT=/dev/null + ;; + -v | --version) + _version + exit 0 + ;; + -h | --help) + _usage + exit 0 + ;; + --color) + shift + _set_color "$1" + ;; + --) shift + break + ;; esac + shift done -shift $((OPTIND - 1)) - if [ -s "${RC_FILE}" ]; then HASRC="true" fi From 74fefb5f31defd7cbd9c87f0d824f2b21e1e5cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= Date: Wed, 16 Oct 2024 16:46:34 +0200 Subject: [PATCH 5/8] restored .hasrc --- .hasrc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.hasrc b/.hasrc index e69de29..eb2d487 100644 --- a/.hasrc +++ b/.hasrc @@ -0,0 +1,6 @@ +# make to install/test the project +make +curl +git +# bats for testing +bats From 28b993d9334ccaabfd7345a2d55288a8ad525a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= Date: Wed, 16 Oct 2024 16:46:46 +0200 Subject: [PATCH 6/8] updated tests --- .hastest.bats | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/.hastest.bats b/.hastest.bats index c79ded3..2c972d3 100644 --- a/.hastest.bats +++ b/.hastest.bats @@ -26,10 +26,11 @@ teardown() { [ "${lines[0]}" = 'Usage: has [OPTION] ...' ] [ "${lines[1]}" = 'Has checks the presence of various command line tools on the PATH and reports their installed version.' ] [ "${lines[2]}" = 'Options:' ] - [ "${lines[3]}" = ' -q Silent mode' ] - [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] - [ "${lines[5]}" = ' -V, --version Show version number and quit' ] - [ "${lines[6]}" = 'Examples: has git curl node' ] + [ "${lines[3]}" = ' -q Silent mode' ] + [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] + [ "${lines[5]}" = ' -V, --version Show version number and quit' ] + [ "${lines[6]}" = ' --color [auto|never|always] Use colors (default: auto)' ] + [ "${lines[7]}" = 'Examples: has git curl node' ] } @test "make install creates a valid installation" { @@ -47,10 +48,11 @@ teardown() { [ "${lines[0]}" = 'Usage: has [OPTION] ...' ] [ "${lines[1]}" = 'Has checks the presence of various command line tools on the PATH and reports their installed version.' ] [ "${lines[2]}" = 'Options:' ] - [ "${lines[3]}" = ' -q Silent mode' ] - [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] - [ "${lines[5]}" = ' -V, --version Show version number and quit' ] - [ "${lines[6]}" = 'Examples: has git curl node' ] + [ "${lines[3]}" = ' -q Silent mode' ] + [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] + [ "${lines[5]}" = ' -V, --version Show version number and quit' ] + [ "${lines[6]}" = ' --color [auto|never|always] Use colors (default: auto)' ] + [ "${lines[7]}" = 'Examples: has git curl node' ] } @test "..even if 'has' is missing from directory" { @@ -210,10 +212,11 @@ teardown() { [ "${lines[0]}" = 'Usage: has [OPTION] ...' ] [ "${lines[1]}" = 'Has checks the presence of various command line tools on the PATH and reports their installed version.' ] [ "${lines[2]}" = 'Options:' ] - [ "${lines[3]}" = ' -q Silent mode' ] - [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] - [ "${lines[5]}" = ' -V, --version Show version number and quit' ] - [ "${lines[6]}" = 'Examples: has git curl node' ] + [ "${lines[3]}" = ' -q Silent mode' ] + [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] + [ "${lines[5]}" = ' -V, --version Show version number and quit' ] + [ "${lines[6]}" = ' --color [auto|never|always] Use colors (default: auto)' ] + [ "${lines[7]}" = 'Examples: has git curl node' ] } @test "status code in quiet mode still equal to number of failed commands" { From 2898fc5064b93a599a40488f74b872548a9826bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= Date: Wed, 16 Oct 2024 16:52:20 +0200 Subject: [PATCH 7/8] forgot typo in _usage() --- .hastest.bats | 6 +++--- has | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.hastest.bats b/.hastest.bats index 2c972d3..656ef2a 100644 --- a/.hastest.bats +++ b/.hastest.bats @@ -28,7 +28,7 @@ teardown() { [ "${lines[2]}" = 'Options:' ] [ "${lines[3]}" = ' -q Silent mode' ] [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] - [ "${lines[5]}" = ' -V, --version Show version number and quit' ] + [ "${lines[5]}" = ' -v, --version Show version number and quit' ] [ "${lines[6]}" = ' --color [auto|never|always] Use colors (default: auto)' ] [ "${lines[7]}" = 'Examples: has git curl node' ] } @@ -50,7 +50,7 @@ teardown() { [ "${lines[2]}" = 'Options:' ] [ "${lines[3]}" = ' -q Silent mode' ] [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] - [ "${lines[5]}" = ' -V, --version Show version number and quit' ] + [ "${lines[5]}" = ' -v, --version Show version number and quit' ] [ "${lines[6]}" = ' --color [auto|never|always] Use colors (default: auto)' ] [ "${lines[7]}" = 'Examples: has git curl node' ] } @@ -214,7 +214,7 @@ teardown() { [ "${lines[2]}" = 'Options:' ] [ "${lines[3]}" = ' -q Silent mode' ] [ "${lines[4]}" = ' -h, --help Display this help text and quit' ] - [ "${lines[5]}" = ' -V, --version Show version number and quit' ] + [ "${lines[5]}" = ' -v, --version Show version number and quit' ] [ "${lines[6]}" = ' --color [auto|never|always] Use colors (default: auto)' ] [ "${lines[7]}" = 'Examples: has git curl node' ] } diff --git a/has b/has index 477bfe5..abe93ec 100755 --- a/has +++ b/has @@ -65,7 +65,7 @@ Has checks the presence of various command line tools on the PATH and reports th Options: -q Silent mode -h, --help Display this help text and quit - -V, --version Show version number and quit + -v, --version Show version number and quit --color [auto|never|always] Use colors (default: auto) Examples: ${BINARY_NAME} git curl node From 928915cab7cf4608293e4d1235650b2db4b621bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric?= Date: Wed, 16 Oct 2024 17:09:15 +0200 Subject: [PATCH 8/8] fix shellcheck issues --- has | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/has b/has index abe93ec..3cbe110 100755 --- a/has +++ b/has @@ -341,13 +341,14 @@ __detect(){ OUTPUT=/dev/stdout OPTS=$(getopt -o qvh --long quiet,version,help,color: -- "$@") +# shellcheck disable=SC2181 if [[ $? -ne 0 ]]; then _usage exit 1; fi eval set -- "$OPTS" -while [ : ]; do +while true; do case "$1" in -q | --quiet) OUTPUT=/dev/null