mirror of https://github.com/kdabir/has.git
add command options for help and version (#53)
* add command options for help and version * fix tests
This commit is contained in:
parent
1f28a53597
commit
868bc1aff6
|
@ -23,9 +23,13 @@ teardown() {
|
||||||
run $has
|
run $has
|
||||||
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[ "${lines[0]%% *}" = 'has' ]
|
[ "${lines[0]}" = 'Usage: has [OPTION] <command-names>...' ]
|
||||||
[ "${lines[1]%%:*}" = 'USAGE' ]
|
[ "${lines[1]}" = 'Has checks the presence of various command line tools on the PATH and reports their installed version.' ]
|
||||||
[ "${lines[2]}" = 'EXAMPLE: has git curl node' ]
|
[ "${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' ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "make install creates a valid installation" {
|
@test "make install creates a valid installation" {
|
||||||
|
@ -38,11 +42,15 @@ teardown() {
|
||||||
# has reads .hasrc from $PWD, so change anywhere else.
|
# has reads .hasrc from $PWD, so change anywhere else.
|
||||||
cd "${INSTALL_DIR}"
|
cd "${INSTALL_DIR}"
|
||||||
run "${INSTALL_DIR}/bin/has"
|
run "${INSTALL_DIR}/bin/has"
|
||||||
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[ "${lines[0]%% *}" = 'has' ]
|
[ "${lines[0]}" = 'Usage: has [OPTION] <command-names>...' ]
|
||||||
[ "${lines[1]%%:*}" = 'USAGE' ]
|
[ "${lines[1]}" = 'Has checks the presence of various command line tools on the PATH and reports their installed version.' ]
|
||||||
[ "${lines[2]}" = 'EXAMPLE: has git curl node' ]
|
[ "${lines[2]}" = 'Options:' ]
|
||||||
# [ "${lines[2]%%:*}" = 'EXAMPLE' ]
|
[ "${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' ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "..even if 'has' is missing from directory" {
|
@test "..even if 'has' is missing from directory" {
|
||||||
|
@ -189,11 +197,25 @@ teardown() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "quiet mode" {
|
@test "quiet mode" {
|
||||||
run $has -q
|
run $has -q git
|
||||||
|
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
[ -z "${output}" ]
|
[ -z "${output}" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "quiet mode should print usage when no commands or .hasrc file" {
|
||||||
|
run $has -q
|
||||||
|
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "${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' ]
|
||||||
|
}
|
||||||
|
|
||||||
@test "status code in quiet mode still equal to number of failed commands" {
|
@test "status code in quiet mode still equal to number of failed commands" {
|
||||||
HAS_ALLOW_UNSAFE=y run $has -q foobar bc git barbaz
|
HAS_ALLOW_UNSAFE=y run $has -q foobar bc git barbaz
|
||||||
|
|
||||||
|
|
73
has
73
has
|
@ -42,6 +42,24 @@ REGEX_SIMPLE_VERSION="([[:digit:]]+\.?){2,3}"
|
||||||
## name of RC file that can contain list of tools to be checked
|
## name of RC file that can contain list of tools to be checked
|
||||||
RC_FILE=".hasrc"
|
RC_FILE=".hasrc"
|
||||||
|
|
||||||
|
_usage() {
|
||||||
|
cat <<EOF
|
||||||
|
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
|
||||||
|
|
||||||
|
Examples: ${BINARY_NAME} git curl node
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
_version() {
|
||||||
|
printf '%s\n' "${VERSION}"
|
||||||
|
}
|
||||||
|
|
||||||
# try to extract version by executing "${1}" with "${2}" arg
|
# try to extract version by executing "${1}" with "${2}" arg
|
||||||
# command name to be executed is dynamically passed to this function as ${1}
|
# command name to be executed is dynamically passed to this function as ${1}
|
||||||
# arg to ${1} is passed in ${2}
|
# arg to ${1} is passed in ${2}
|
||||||
|
@ -250,15 +268,52 @@ __detect(){
|
||||||
OPTIND=1
|
OPTIND=1
|
||||||
OUTPUT=/dev/stdout
|
OUTPUT=/dev/stdout
|
||||||
|
|
||||||
while getopts "q" opt; do
|
while getopts ":qhV-" OPTION; do
|
||||||
case "$opt" in
|
case "$OPTION" in
|
||||||
q) QUIET="true"
|
q)
|
||||||
OUTPUT=/dev/null
|
QUIET="true"
|
||||||
;;
|
OUTPUT=/dev/null
|
||||||
|
;;
|
||||||
|
h)
|
||||||
|
_usage
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
V)
|
||||||
|
_version
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
-)
|
||||||
|
[ $OPTIND -ge 1 ] && optind=$(expr $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
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
printf '%s: unrecognized option '%s'\n' "${BINARY_NAME}" "${OPTARG}"
|
||||||
|
_usage
|
||||||
|
exit 2
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
OPTIND=1
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
?)
|
||||||
|
printf '%s: unrecognized option '%s'\n' "${BINARY_NAME}" "${OPTARG}"
|
||||||
|
_usage
|
||||||
|
exit 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
shift $((OPTIND-1))
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
if [ -s "${RC_FILE}" ]; then
|
if [ -s "${RC_FILE}" ]; then
|
||||||
HASRC="true"
|
HASRC="true"
|
||||||
|
@ -266,11 +321,7 @@ fi
|
||||||
|
|
||||||
# if HASRC is not set AND no arguments passed to script
|
# if HASRC is not set AND no arguments passed to script
|
||||||
if [[ -z "${HASRC}" ]] && [ "$#" -eq 0 ]; then
|
if [[ -z "${HASRC}" ]] && [ "$#" -eq 0 ]; then
|
||||||
# print help
|
_usage
|
||||||
printf '%s %s\n' "${BINARY_NAME}" "${VERSION}" > "$OUTPUT"
|
|
||||||
printf 'USAGE:\t %s [-q] <command-names>..\n' "${BINARY_NAME}" > "$OUTPUT"
|
|
||||||
printf 'EXAMPLE: %s git curl node\n' "${BINARY_NAME}" > "$OUTPUT"
|
|
||||||
|
|
||||||
else
|
else
|
||||||
# for each cli-arg
|
# for each cli-arg
|
||||||
for cmd in "$@"; do
|
for cmd in "$@"; do
|
||||||
|
|
Loading…
Reference in New Issue