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

Optionally hijacks -h/--help options #254

Open
wants to merge 2 commits into
base: main
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
87 changes: 50 additions & 37 deletions go-core.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
# https://mike-bland.com/
# https://github.com/mbland

if [[ "${BASH_VERSINFO[0]}" -lt '3' ||
( "${BASH_VERSINFO[0]}" -eq '3' && "${BASH_VERSINFO[1]}" -lt '2' ) ]]; then
if [[ "${BASH_VERSINFO[0]}" -lt '3' || ("${BASH_VERSINFO[0]}" -eq '3' && "${BASH_VERSINFO[1]}" -lt '2') ]]; then
printf "This module requires bash version 3.2 or greater:\n %s %s\n" \
"$BASH" "$BASH_VERSION"
exit 1
Expand Down Expand Up @@ -222,8 +221,8 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
skip_callers=0
fi

for ((i=$skip_callers + 1; i != ${#FUNCNAME[@]}; ++i)); do
printf ' %s:%s %s\n' "${BASH_SOURCE[$i]}" "${BASH_LINENO[$((i-1))]}" \
for ((i = $skip_callers + 1; i != ${#FUNCNAME[@]}; ++i)); do
printf ' %s:%s %s\n' "${BASH_SOURCE[$i]}" "${BASH_LINENO[$((i - 1))]}" \
"${FUNCNAME[$i]}"
done
return "$result"
Expand Down Expand Up @@ -283,39 +282,53 @@ declare _GO_INJECT_MODULE_PATH="$_GO_INJECT_MODULE_PATH"
# $1: name of the command to invoke
# $2..$#: arguments to the specified command
@go() {
local cmd="$1"
shift
# Flag to enable/disable hijacking of the '-h' or '--help' options and print
# the help for the command given.
declare _GO_HELP_HIJACK="${_GO_HELP_HIJACK:-false}"

case "$cmd" in
'')
[email protected]_builtin 'help' 1>&2
return 1
;;
-h|-help|--help)
if [[ "$_GO_HELP_HIJACK" == 'true' ]] \
&& [[ " $* " == *' -h '* || " $* " == *' --help '* || " $* " == *' -help '* ]]; then
cmd='help'
;;
-*)
@go.printf "Unknown flag: $cmd\n\n"
[email protected]_builtin 'help' 1>&2
return 1
;;
edit)
if [[ -z "$EDITOR" ]]; then
echo "Cannot edit $@: \$EDITOR not defined."
return 1
if [[ "${1-}" == @(-h|-help|--help) && "$#" -gt 0 ]]; then
shift
fi
"$EDITOR" "$@"
return
;;
run)
"$@"
return
;;
cd|pushd|unenv)
@go.printf "$cmd is only available after using \"$_GO_CMD env\" %s\n" \
"to set up your shell environment." >&2
return 1
;;
else
local cmd="${1-}"
if [[ "$#" -gt 0 ]]; then
shift
fi
fi

case "$cmd" in
'')
[email protected]_builtin 'help' 1>&2
return 1
;;
-h | -help | --help)
cmd='help'
;;
-*)
@go.printf "Unknown flag: $cmd\n\n"
[email protected]_builtin 'help' 1>&2
return 1
;;
edit)
if [[ -z "$EDITOR" ]]; then
echo "Cannot edit $@: \$EDITOR not defined."
return 1
fi
"$EDITOR" "$@"
return
;;
run)
"$@"
return
;;
cd | pushd | unenv)
@go.printf "$cmd is only available after using \"$_GO_CMD env\" %s\n" \
"to set up your shell environment." >&2
return 1
;;
esac

if [email protected]_builtin 'aliases' --exists "$cmd"; then
Expand Down Expand Up @@ -360,7 +373,7 @@ [email protected]_command_script() {
shift

local interpreter
read -r interpreter < "$cmd_path"
read -r interpreter <"$cmd_path"

if [[ "${interpreter:0:2}" != '#!' ]]; then
@go.printf \
Expand Down Expand Up @@ -422,8 +435,8 @@ elif [[ -z "$COLUMNS" ]]; then
# On Travis, $TERM is set to 'dumb', but `tput cols` still fails.
if command -v tput >/dev/null && tput cols >/dev/null 2>&1; then
COLUMNS="$(tput cols)"
elif command -v mode.com >/dev/null &&
[[ "$(mode.com 'con')" =~ Columns:\ +([0-9]+) ]]; then
elif command -v mode.com >/dev/null \
&& [[ "$(mode.com 'con')" =~ Columns:\ +([0-9]+) ]]; then
COLUMNS="${BASH_REMATCH[1]}"
fi
export COLUMNS="${COLUMNS:-80}"
Expand Down
23 changes: 23 additions & 0 deletions tests/help.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@ teardown() {
@go.remove_test_go_rootdir
}

@test "$SUITE: do not hijack -h/--help by default" {
@go.create_test_command_script 'foo' '# Does foo stuff' 'echo foo'
run "$TEST_GO_SCRIPT" foo -h

assert_success
assert_output_matches 'foo'
}

@test "$SUITE: hijack -h/--help when _GO_HELP_HIJACK is enabled" {
@go.create_test_command_script 'foo' '# Does foo stuff' 'echo foo'

export _GO_HELP_HIJACK=true
run "$TEST_GO_SCRIPT" foo -h

assert_success
assert_output_matches 'Does foo stuff'

run "$TEST_GO_SCRIPT" foo --help

assert_success
assert_output_matches 'Does foo stuff'
}

@test "$SUITE: tab completion" {
local subcommands=('plugh' 'quux' 'xyzzy')
@go.create_parent_and_subcommands foo "${subcommands[@]}"
Expand Down
2 changes: 2 additions & 0 deletions tests/vars.bats
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ quotify_expected() {
"declare -rx _GO_CORE_URL=\"$_GO_CORE_URL\""
"declare -rx _GO_CORE_VERSION=\"$_GO_CORE_VERSION\""
"declare -x _GO_COVERALLS_URL=\"$_GO_COVERALLS_URL\""
'declare -- _GO_HELP_HIJACK="false"'
'declare -a _GO_IMPORTED_MODULES=()'
'declare -a _GO_IMPORTED_MODULE_CALLERS=()'
'declare -a _GO_IMPORTED_MODULE_FILES=()'
Expand Down Expand Up @@ -118,6 +119,7 @@ quotify_expected() {
"declare -rx _GO_CORE_URL=\"$_GO_CORE_URL\""
"declare -rx _GO_CORE_VERSION=\"$_GO_CORE_VERSION\""
"declare -x _GO_COVERALLS_URL=\"$_GO_COVERALLS_URL\""
'declare -- _GO_HELP_HIJACK="false"'
"declare -a _GO_IMPORTED_MODULES=(${expected_modules[*]})"
"declare -a _GO_IMPORTED_MODULE_CALLERS=(${expected_module_callers[*]})"
"declare -a _GO_IMPORTED_MODULE_FILES=(${expected_module_files[*]})"
Expand Down