Skip to content

Commit c0e00aa

Browse files
committed
Fix Bash commands not recognizing global options
Some Bash commands don't recognize global options like --help, --debug, etc. For --help, we fix this in brew.sh by redirecting to the Ruby code path when --help is detected. For the remaining global options, we add handling for them in the respective shell commands.
1 parent 3a5e3fc commit c0e00aa

File tree

5 files changed

+76
-52
lines changed

5 files changed

+76
-52
lines changed

Library/Homebrew/brew.sh

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,43 @@ then
114114
HOMEBREW_TEMP="${HOMEBREW_DEFAULT_TEMP}"
115115
fi
116116

117-
# commands that take a single or no arguments.
118-
# HOMEBREW_LIBRARY set by bin/brew
119-
# shellcheck disable=SC2154
120-
# doesn't need a default case as other arguments handled elsewhere.
121-
# shellcheck disable=SC2249
122-
case "$1" in
123-
formulae)
124-
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"
125-
homebrew-formulae
126-
exit 0
127-
;;
128-
casks)
129-
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/casks.sh"
130-
homebrew-casks
131-
exit 0
132-
;;
133-
shellenv)
134-
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"
135-
shift
136-
homebrew-shellenv "$1"
137-
exit 0
138-
;;
139-
esac
117+
for arg in "$@"
118+
do
119+
[[ "${arg}" == "--" ]] && break
120+
121+
if [[ "${arg}" == "--help" || "${arg}" == "-h" || "${arg}" == "--usage" || "${arg}" == "-?" ]]
122+
then
123+
export HOMEBREW_HELP="1"
124+
break
125+
fi
126+
done
127+
128+
if [[ -z "${HOMEBREW_HELP}" ]]
129+
then
130+
# commands that take a single or no arguments.
131+
# HOMEBREW_LIBRARY set by bin/brew
132+
# shellcheck disable=SC2154
133+
# doesn't need a default case as other arguments handled elsewhere.
134+
# shellcheck disable=SC2249
135+
case "$1" in
136+
formulae)
137+
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"
138+
homebrew-formulae
139+
exit 0
140+
;;
141+
casks)
142+
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/casks.sh"
143+
homebrew-casks
144+
exit 0
145+
;;
146+
shellenv)
147+
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"
148+
shift
149+
homebrew-shellenv "$1"
150+
exit 0
151+
;;
152+
esac
153+
fi
140154

141155
source "${HOMEBREW_LIBRARY}/Homebrew/help.sh"
142156

@@ -192,19 +206,22 @@ esac
192206
source "${HOMEBREW_LIBRARY}/Homebrew/utils/wrapper.sh"
193207
check-brew-wrapper "$1"
194208

195-
# commands that take a single or no arguments and need to write to HOMEBREW_PREFIX.
196-
# HOMEBREW_LIBRARY set by bin/brew
197-
# shellcheck disable=SC2154
198-
# doesn't need a default case as other arguments handled elsewhere.
199-
# shellcheck disable=SC2249
200-
case "$1" in
201-
setup-ruby)
202-
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/setup-ruby.sh"
203-
shift
204-
homebrew-setup-ruby "$1"
205-
exit 0
206-
;;
207-
esac
209+
if [[ -z "${HOMEBREW_HELP}" ]]
210+
then
211+
# commands that take a single or no arguments and need to write to HOMEBREW_PREFIX.
212+
# HOMEBREW_LIBRARY set by bin/brew
213+
# shellcheck disable=SC2154
214+
# doesn't need a default case as other arguments handled elsewhere.
215+
# shellcheck disable=SC2249
216+
case "$1" in
217+
setup-ruby)
218+
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/setup-ruby.sh"
219+
shift
220+
homebrew-setup-ruby "$1"
221+
exit 0
222+
;;
223+
esac
224+
fi
208225

209226
#####
210227
##### Next, define all other helper functions.
@@ -559,7 +576,7 @@ fi
559576
# commands that take a single or no arguments.
560577
# doesn't need a default case as other arguments handled elsewhere.
561578
# shellcheck disable=SC2249
562-
case "$1" in
579+
case "$@" in
563580
--version | -v)
564581
source "${HOMEBREW_LIBRARY}/Homebrew/cmd/--version.sh"
565582
homebrew-version
@@ -852,17 +869,6 @@ EOS
852869
fi
853870
fi
854871

855-
for arg in "$@"
856-
do
857-
[[ "${arg}" == "--" ]] && break
858-
859-
if [[ "${arg}" == "--help" || "${arg}" == "-h" || "${arg}" == "--usage" || "${arg}" == "-?" ]]
860-
then
861-
export HOMEBREW_HELP="1"
862-
break
863-
fi
864-
done
865-
866872
HOMEBREW_ARG_COUNT="$#"
867873
HOMEBREW_COMMAND="$1"
868874
shift
@@ -1077,7 +1083,7 @@ unset SUDO
10771083
# Remove internal variables
10781084
unset HOMEBREW_INTERNAL_ALLOW_PACKAGES_FROM_PATHS
10791085

1080-
if [[ -n "${HOMEBREW_BASH_COMMAND}" ]]
1086+
if [[ -n "${HOMEBREW_BASH_COMMAND}" ]] && [[ -z "${HOMEBREW_HELP}" ]]
10811087
then
10821088
# source rather than executing directly to ensure the entire file is read into
10831089
# memory before it is run. This makes running a Bash script behave more like

Library/Homebrew/cmd/--repository.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,27 @@ tap_path() {
3131

3232
homebrew---repository() {
3333
local tap
34+
local taps=()
3435

35-
if [[ "$#" -eq 0 ]]
36+
for arg in "$@"
37+
do
38+
case "${arg}" in
39+
-d | --debug | -q | --quiet | -v | --verbose) ;;
40+
*)
41+
taps+=("${arg}")
42+
;;
43+
esac
44+
done
45+
46+
if [[ "${#taps[@]}" -eq 0 ]]
3647
then
3748
echo "${HOMEBREW_REPOSITORY}"
3849
return
3950
fi
4051

4152
(
4253
shopt -s extglob
43-
for tap in "$@"
54+
for tap in "${taps[@]}"
4455
do
4556
tap_path "${tap}"
4657
done

Library/Homebrew/cmd/--version.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ homebrew-version() {
3636
echo "Homebrew/homebrew-cask $(version_string "${HOMEBREW_CASK_REPOSITORY}")"
3737
fi
3838
}
39+
40+
homebrew---version() {
41+
homebrew-version
42+
}

Library/Homebrew/cmd/which-formula.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ homebrew-which-formula() {
7474
while [[ "$#" -gt 0 ]]
7575
do
7676
case "$1" in
77+
-d | --debug | -q | --quiet | -v | --verbose)
78+
shift
79+
;;
7780
--explain)
7881
HOMEBREW_EXPLAIN=1
7982
shift

Library/Homebrew/utils/ruby.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ If there's no Homebrew Portable Ruby available for your processor:
119119

120120
unset HOMEBREW_RUBY_PATH
121121

122-
if [[ "${HOMEBREW_COMMAND}" == "vendor-install" ]]
122+
if [[ "${HOMEBREW_COMMAND}" == "vendor-install" && -z "${HOMEBREW_HELP}" ]]
123123
then
124124
return 0
125125
fi

0 commit comments

Comments
 (0)