list.sh: improve arg parsing, support brew ls

This commit is contained in:
Bo Anderson 2024-09-03 00:03:18 +01:00
parent 1981abd959
commit d9a339b231
No known key found for this signature in database
3 changed files with 23 additions and 14 deletions

View File

@ -168,7 +168,7 @@ case "$@" in
homebrew-command-path "$@" && exit 0 homebrew-command-path "$@" && exit 0
;; ;;
# falls back to cmd/list.rb on a non-zero return # falls back to cmd/list.rb on a non-zero return
list*) list* | ls*)
source "${HOMEBREW_LIBRARY}/Homebrew/list.sh" source "${HOMEBREW_LIBRARY}/Homebrew/list.sh"
homebrew-list "$@" && exit 0 homebrew-list "$@" && exit 0
;; ;;

View File

@ -4,8 +4,8 @@
homebrew-list() { homebrew-list() {
case "$1" in case "$1" in
# check we actually have list and not e.g. listsomething # check we actually have list and not e.g. listsomething
list) ;; list | ls) ;;
list*) return 1 ;; list* | ls*) return 1 ;;
*) ;; *) ;;
esac esac
@ -24,23 +24,30 @@ homebrew-list() {
local formula="" local formula=""
local cask="" local cask=""
local first_arg # `OPTIND` is used internally by `getopts` to track parsing position
for arg in "$@" local OPTIND=2 # skip $1 (and localise OPTIND to this function)
while getopts ":1lrt-:" arg
do do
if [[ -z "${first_arg}" ]]
then
first_arg=1
[[ "${arg}" == "list" ]] && continue
fi
case "${arg}" in case "${arg}" in
# check for flags passed to ls # check for flags passed to ls
-1 | -l | -r | -t) ls_args+=("${arg}") ;; 1 | l | r | t) ls_args+=("-${arg}") ;;
--formula | --formulae) formula=1 ;; -)
--cask | --casks) cask=1 ;; 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 # reject all other flags
-* | *) return 1 ;; *) return 1 ;;
esac esac
done 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}" ]] if [[ -z "${cask}" && -d "${HOMEBREW_CELLAR}" ]]
then then

View File

@ -18,4 +18,6 @@ RSpec.describe Homebrew::Cmd::List do
.and not_to_output.to_stderr .and not_to_output.to_stderr
.and be_a_success .and be_a_success
end end
# TODO: add a test for the shell fast-path (`brew_sh`)
end end