From 1b4646dee462f5303acbfbc6353db678471c6472 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 24 Dec 2020 12:57:01 +0000 Subject: [PATCH 1/4] cmd/list: Limit `-lrt` options to being passed with `--formula` - These are documented as only working on formulae, but users expect the same options (long format, reverse order or sort by modified time) to be passed to both formulae and casks in the default `brew list`. The output looks weird as they're not. Hence, constrain these to be `--formula`-only. - This was added originally in 5adb76a5babdccd2c4edfb8752ac979ed14716ca, but then disappeared. --- Library/Homebrew/cmd/list.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb index a906de4b09..1506cc0f92 100644 --- a/Library/Homebrew/cmd/list.rb +++ b/Library/Homebrew/cmd/list.rb @@ -47,11 +47,14 @@ module Homebrew description: "Force output to be one entry per line. " \ "This is the default when output is not to a terminal." switch "-l", + depends_on: "--formula", description: "List formulae in long format. If the output is to a terminal, "\ "a total sum for all the file sizes is printed before the long listing." switch "-r", + depends_on: "--formula", description: "Reverse the order of the formulae sort to list the oldest entries first." switch "-t", + depends_on: "--formula", description: "Sort formulae by time modified, listing most recently modified first." conflicts "--formula", "--cask" From 570a6607588e5aa2c5dca29d5c2e1328e906b6c3 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 24 Dec 2020 13:19:02 +0000 Subject: [PATCH 2/4] cli/parser: Only prefix short options with one dash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - This avoids error messages like: ``` ➜ brew list -l Error: Invalid usage: `--l` cannot be passed without `--formula`. ``` --- Library/Homebrew/cli/parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 907b4cd17b..5937d66a1f 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -533,8 +533,8 @@ module Homebrew class OptionConstraintError < UsageError def initialize(arg1, arg2, missing: false) - arg1 = "--#{arg1.tr("_", "-")}" - arg2 = "--#{arg2.tr("_", "-")}" + arg1 = arg1.length > 1 ? "--#{arg1.tr("_", "-")}" : "-#{arg1.tr("_", "-")}" + arg2 = arg2.length > 1 ? "--#{arg2.tr("_", "-")}" : "-#{arg2.tr("_", "-")}" message = if missing "`#{arg2}` cannot be passed without `#{arg1}`." From 27afcf577989f9cbbc518bad3ebc15818555b328 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 24 Dec 2020 15:53:13 +0000 Subject: [PATCH 3/4] cli/parser: Improve single or multi "-" detection - This reads nicer (to me). --- Library/Homebrew/cli/parser.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 5937d66a1f..2cbafcbcc8 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -533,8 +533,8 @@ module Homebrew class OptionConstraintError < UsageError def initialize(arg1, arg2, missing: false) - arg1 = arg1.length > 1 ? "--#{arg1.tr("_", "-")}" : "-#{arg1.tr("_", "-")}" - arg2 = arg2.length > 1 ? "--#{arg2.tr("_", "-")}" : "-#{arg2.tr("_", "-")}" + arg1 = dashes(arg1) + arg1.tr("_", "-") + arg2 = dashes(arg2) + arg2.tr("_", "-") message = if missing "`#{arg2}` cannot be passed without `#{arg1}`." @@ -543,6 +543,10 @@ module Homebrew end super message end + + def dashes(arg) + arg.length > 1 ? "--" : "-" + end end class OptionConflictError < UsageError From 531cae4b8c18689212474a4235f9061471ca3b40 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 24 Dec 2020 16:29:44 +0000 Subject: [PATCH 4/4] cli/parser: Option-ify arg names when raising `OptionConstraintError` - There's already a method on `CLI::Parser`, we don't need to hand-roll the "number of dashes" detection. Co-authored-by: Rylan Polster --- Library/Homebrew/cli/parser.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 2cbafcbcc8..0db260c0a4 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -432,6 +432,10 @@ module Homebrew @constraints.each do |primary, secondary, constraint_type| primary_passed = option_passed?(primary) secondary_passed = option_passed?(secondary) + + primary = name_to_option(primary) + secondary = name_to_option(secondary) + if :mandatory.equal?(constraint_type) && primary_passed && !secondary_passed raise OptionConstraintError.new(primary, secondary) end @@ -533,9 +537,6 @@ module Homebrew class OptionConstraintError < UsageError def initialize(arg1, arg2, missing: false) - arg1 = dashes(arg1) + arg1.tr("_", "-") - arg2 = dashes(arg2) + arg2.tr("_", "-") - message = if missing "`#{arg2}` cannot be passed without `#{arg1}`." else @@ -543,10 +544,6 @@ module Homebrew end super message end - - def dashes(arg) - arg.length > 1 ? "--" : "-" - end end class OptionConflictError < UsageError