From 79597cad1ef87cbd234ff36ddd9b5b5cbd38d448 Mon Sep 17 00:00:00 2001 From: Bo Anderson Date: Tue, 3 Sep 2024 00:03:18 +0100 Subject: [PATCH] list.sh: improve arg parsing, support `brew ls` --- Library/Homebrew/brew.sh | 2 +- Library/Homebrew/list.sh | 31 +++++++++++++++----------- Library/Homebrew/test/cmd/list_spec.rb | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 136da511f963f7..e32042971a2a30 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -161,7 +161,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 ;; diff --git a/Library/Homebrew/list.sh b/Library/Homebrew/list.sh index 6184a44ad854e8..26ca7fd8b073a1 100644 --- a/Library/Homebrew/list.sh +++ b/Library/Homebrew/list.sh @@ -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 @@ -13,23 +13,28 @@ homebrew-list() { local formula="" local cask="" - local first_arg - for arg in "$@" + 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}") ;; + -) + case "${OPTARG}" 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 local tty if [[ -t 1 ]] diff --git a/Library/Homebrew/test/cmd/list_spec.rb b/Library/Homebrew/test/cmd/list_spec.rb index b5b4a52902edd3..eed7d3ea798a70 100644 --- a/Library/Homebrew/test/cmd/list_spec.rb +++ b/Library/Homebrew/test/cmd/list_spec.rb @@ -13,7 +13,7 @@ (HOMEBREW_CELLAR/f/"1.0/somedir").mkpath end - expect { brew "list", "--formula" } + expect { brew_sh "list", "--formula" } .to output("#{formulae.join("\n")}\n").to_stdout .and not_to_output.to_stderr .and be_a_success