diff --git a/README.md b/README.md index 850fde829..6d33e950f 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,22 @@ packages, so the community part is important! Every little bit counts. You can add Casks to your existing (or new) taps: just create a directory named `Casks` inside your tap, put your Casks there, and everything will just work. +# Options + +You can set options on the command-line and/or using the `HOMEBREW_CASK_OPTS` +environment variable, e.g.: + +```bash +# This probably should happen in your ~/.{ba|z}shrc +$ export HOMEBREW_CASK_OPTS="/Applications" + +# Installs to /Applications +$ brew cask install a-cask + +# Trumps the ENV and installs to ~/Applications +$ brew cask install --appdir="~/Applications" a-cask +``` + # Alfred Integration I've been using Casks along with Alfred to great effect. Just add diff --git a/lib/cask.rb b/lib/cask.rb index 8219358fb..247725331 100644 --- a/lib/cask.rb +++ b/lib/cask.rb @@ -26,8 +26,8 @@ class Cask @appdir ||= Pathname.new(File.expand_path("~/Applications")) end - def self.set_appdir(canned_appdir) - @appdir = canned_appdir + def self.appdir=(_appdir) + @appdir = _appdir end def self.init diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index 66c20f81f..c67d40cdc 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -1,3 +1,6 @@ +require 'optparse' +require 'shellwords' + class Cask::CLI def self.commands Cask::CLI.constants - ["NullCommand"] @@ -14,6 +17,7 @@ class Cask::CLI def self.process(arguments) Cask.init command, *rest = *arguments + rest = process_options(rest) lookup_command(command).run(*rest) end @@ -34,6 +38,16 @@ class Cask::CLI } list.sort end + + def self.process_options(args) + allrgs = Shellwords.shellsplit(ENV['HOMEBREW_CASK_OPTS'] || "") + args + OptionParser.new do |opts| + opts.on("--appdir=MANDATORY") do |v| + Cask.appdir = Pathname.new File.expand_path(v) + end + end.parse!(allrgs) + return allrgs + end class NullCommand @@ -43,7 +57,7 @@ class Cask::CLI def run(*args) purpose - if @attempted_name + if @attempted_name and @attempted_name != "help" puts "!! " puts "!! no command with name: #{@attempted_name}" puts "!! " @@ -52,7 +66,7 @@ class Cask::CLI end def purpose - puts <<-PURPOSE.gsub(/^ {6}/, '') + puts <<-PURPOSE.undent {{ brew-cask }} brew-cask provides a friendly homebrew-style CLI workflow for the administration Mac applications distributed as binaries diff --git a/test/cli/options_test.rb b/test/cli/options_test.rb new file mode 100644 index 000000000..aae62ed1f --- /dev/null +++ b/test/cli/options_test.rb @@ -0,0 +1,26 @@ +require 'test_helper' + +describe Cask::CLI do + it "supports setting the appdir" do + shutup do + Cask::CLI.process %w{help --appdir=/some/path} + end + + Cask.appdir.must_equal Pathname.new File.expand_path "/some/path" + end + + it "supports setting the appdir from ENV" do + ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/some/path" + + shutup do + Cask::CLI.process %w{help} + end + + Cask.appdir.must_equal Pathname.new File.expand_path "/some/path" + end + + after do + ENV['HOMEBREW_CASK_OPTS'] = nil + Cask.appdir = CANNED_APPDIR + end +end diff --git a/test/support/fake_appdir.rb b/test/support/fake_appdir.rb index 46001dca6..92a207a56 100644 --- a/test/support/fake_appdir.rb +++ b/test/support/fake_appdir.rb @@ -1,6 +1,6 @@ # wire in a fake appdir for linkapps CANNED_APPDIR = (HOMEBREW_REPOSITORY/"Applications") -Cask.set_appdir(CANNED_APPDIR) +Cask.appdir = CANNED_APPDIR module FakeAppdirHooks def before_setup