From 33b5ac78adfa10304a73c8beeb28763294b4c1eb Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Fri, 30 Mar 2018 15:01:41 +0100 Subject: [PATCH] cmd/switch: improve usability. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ensure that we output the available versions where relevant - Use `` to be consistent with other commands’ help - General code cleanup --- Library/Homebrew/cmd/switch.rb | 33 +++++++++++++++--------- Library/Homebrew/test/cmd/switch_spec.rb | 4 +-- docs/Manpage.md | 4 +-- manpages/brew.1 | 4 +-- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Library/Homebrew/cmd/switch.rb b/Library/Homebrew/cmd/switch.rb index 8a378aed79..b0ac32626e 100644 --- a/Library/Homebrew/cmd/switch.rb +++ b/Library/Homebrew/cmd/switch.rb @@ -1,5 +1,5 @@ -#: * `switch` : -#: Symlink all of the specific of 's install to Homebrew prefix. +#: * `switch` : +#: Symlink all of the specific of 's install to Homebrew prefix. require "formula" require "keg" @@ -9,14 +9,15 @@ module Homebrew module_function def switch - if ARGV.named.length != 2 - onoe "Usage: brew switch " + name = ARGV.first + + usage = "Usage: brew switch " + + unless name + onoe usage exit 1 end - name = ARGV.shift - version = ARGV.shift - rack = Formulary.to_rack(name) unless rack.directory? @@ -24,13 +25,21 @@ module Homebrew exit 2 end - # Does the target version exist? + versions = rack.subdirs + .map { |d| Keg.new(d).version } + .sort + .join(", ") + version = ARGV[1] + + if !version || ARGV.named.length > 2 + onoe usage + puts "#{name} installed versions: #{versions}" + exit 1 + end + unless (rack/version).directory? onoe "#{name} does not have a version \"#{version}\" in the Cellar." - - versions = rack.subdirs.map { |d| Keg.new(d).version }.sort - puts "Versions available: #{versions.join(", ")}" - + puts "#{name} installed versions: #{versions}" exit 3 end diff --git a/Library/Homebrew/test/cmd/switch_spec.rb b/Library/Homebrew/test/cmd/switch_spec.rb index 48d7dcdd08..fbf44810b9 100644 --- a/Library/Homebrew/test/cmd/switch_spec.rb +++ b/Library/Homebrew/test/cmd/switch_spec.rb @@ -1,7 +1,7 @@ describe "brew switch", :integration_test do it "allows switching between Formula versions" do expect { brew "switch" } - .to output(/Usage: brew switch /).to_stderr + .to output(/Usage: brew switch /).to_stderr .and not_to_output.to_stdout .and be_a_failure @@ -25,7 +25,7 @@ describe "brew switch", :integration_test do .and be_a_success expect { brew "switch", "testball", "0.3" } - .to output("Versions available: 0.1, 0.2\n").to_stdout + .to output("testball installed versions: 0.1, 0.2\n").to_stdout .and output(/testball does not have a version "0.3"/).to_stderr .and be_a_failure end diff --git a/docs/Manpage.md b/docs/Manpage.md index 3479ec771d..1a89c8df90 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -444,8 +444,8 @@ With `--verbose` or `-v`, many commands print extra debugging information. Note Exits with a non-zero status if any style violations are found. - * `switch` `name` `version`: - Symlink all of the specific `version` of `name`'s install to Homebrew prefix. + * `switch` `formula` `version`: + Symlink all of the specific `version` of `formula`'s install to Homebrew prefix. * `tap`: List all installed taps. diff --git a/manpages/brew.1 b/manpages/brew.1 index ca8c6f5f14..b9e9531dac 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -455,8 +455,8 @@ Passing \fB\-\-only\-cops=\fR\fIcops\fR will check for violations of only the li Exits with a non\-zero status if any style violations are found\. . .TP -\fBswitch\fR \fIname\fR \fIversion\fR -Symlink all of the specific \fIversion\fR of \fIname\fR\'s install to Homebrew prefix\. +\fBswitch\fR \fIformula\fR \fIversion\fR +Symlink all of the specific \fIversion\fR of \fIformula\fR\'s install to Homebrew prefix\. . .TP \fBtap\fR