Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI options parser in 'has' + updated 'packages_all.sh' #83

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions .hastest.bats
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ teardown() {
[ "${lines[0]}" = 'Usage: has [OPTION] <command-names>...' ]
[ "${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" {
Expand All @@ -47,10 +48,11 @@ teardown() {
[ "${lines[0]}" = 'Usage: has [OPTION] <command-names>...' ]
[ "${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" {
Expand Down Expand Up @@ -210,10 +212,11 @@ teardown() {
[ "${lines[0]}" = 'Usage: has [OPTION] <command-names>...' ]
[ "${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" {
Expand Down
86 changes: 34 additions & 52 deletions has
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,9 +63,10 @@ Usage: ${BINARY_NAME} [OPTION] <command-names>...
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
Expand All @@ -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}"
}
Expand Down Expand Up @@ -338,58 +338,40 @@ __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: -- "$@")
# shellcheck disable=SC2181
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 true; 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
Expand Down
2 changes: 1 addition & 1 deletion tests/packages_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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