Skip to content

Commit

Permalink
Merge pull request #18234 from Homebrew/list.sh-args
Browse files Browse the repository at this point in the history
list.sh: improve arg parsing, support `brew ls`
  • Loading branch information
Bo98 committed Sep 3, 2024
2 parents 1981abd + d9a339b commit 8f00fef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Library/Homebrew/brew.sh
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ case "$@" in
homebrew-command-path "$@" && exit 0
;;
# falls back to cmd/list.rb on a non-zero return
list*)
list* | ls*)
source "${HOMEBREW_LIBRARY}/Homebrew/list.sh"
homebrew-list "$@" && exit 0
;;
Expand Down
33 changes: 20 additions & 13 deletions Library/Homebrew/list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
homebrew-list() {
case "$1" in
# check we actually have list and not e.g. listsomething
list) ;;
list*) return 1 ;;
list | ls) ;;
list* | ls*) return 1 ;;
*) ;;
esac

Expand All @@ -24,23 +24,30 @@ homebrew-list() {
local formula=""
local cask=""

local first_arg
for arg in "$@"
# `OPTIND` is used internally by `getopts` to track parsing position
local OPTIND=2 # skip $1 (and localise OPTIND to this function)
while getopts ":1lrt-:" arg
do
if [[ -z "${first_arg}" ]]
then
first_arg=1
[[ "${arg}" == "list" ]] && continue
fi
case "${arg}" in
# check for flags passed to ls
-1 | -l | -r | -t) ls_args+=("${arg}") ;;
--formula | --formulae) formula=1 ;;
--cask | --casks) cask=1 ;;
1 | l | r | t) ls_args+=("-${arg}") ;;
-)
local parsed_index=$((OPTIND - 1)) # Parse full arg to reject e.g. -r-formula
case "${!parsed_index}" in
--formula | --formulae) formula=1 ;;
--cask | --casks) cask=1 ;;
*) return 1 ;;
esac
;;
# reject all other flags
-* | *) return 1 ;;
*) return 1 ;;
esac
done
# If we haven't reached the end of the arg list, we have named args.
if ((OPTIND - 1 != $#))
then
return 1
fi

if [[ -z "${cask}" && -d "${HOMEBREW_CELLAR}" ]]
then
Expand Down
2 changes: 2 additions & 0 deletions Library/Homebrew/test/cmd/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
.and not_to_output.to_stderr
.and be_a_success
end

# TODO: add a test for the shell fast-path (`brew_sh`)
end

0 comments on commit 8f00fef

Please sign in to comment.