Browse Source

Add options to the cli

Options can be passed on the command-line and/or using
the HOMEBREW_CASK_OPTS environment variable (which has
lowest priority). There is a single --appdir=PATH option
right now, but this commit enables future awesomeness!

Other minor changes:

* `brew cask help` now returns the same thing as `brew cask`
  instead of saying there was “no such command as help”.

* The HEREDOC block now uses Homebrew's #undent instead of the
  customed-rolled #gsub version. Cleaner and more flexible.

* `Cask.set_appdir` has been renamed to `Cask.appdir=`. This
  is more Rubyish, and of little consequence (the only place
  it was previously used was in the tests).
Félix Saparelli 12 years ago
parent
commit
5ad6796bcd
  1. 16
      README.md
  2. 4
      lib/cask.rb
  3. 18
      lib/cask/cli.rb
  4. 26
      test/cli/options_test.rb
  5. 2
      test/support/fake_appdir.rb

16
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 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. `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 # Alfred Integration
I've been using Casks along with Alfred to great effect. Just add I've been using Casks along with Alfred to great effect. Just add

4
lib/cask.rb

@ -26,8 +26,8 @@ class Cask
@appdir ||= Pathname.new(File.expand_path("~/Applications")) @appdir ||= Pathname.new(File.expand_path("~/Applications"))
end end
def self.set_appdir(canned_appdir) def self.appdir=(_appdir)
@appdir = canned_appdir @appdir = _appdir
end end
def self.init def self.init

18
lib/cask/cli.rb

@ -1,3 +1,6 @@
require 'optparse'
require 'shellwords'
class Cask::CLI class Cask::CLI
def self.commands def self.commands
Cask::CLI.constants - ["NullCommand"] Cask::CLI.constants - ["NullCommand"]
@ -14,6 +17,7 @@ class Cask::CLI
def self.process(arguments) def self.process(arguments)
Cask.init Cask.init
command, *rest = *arguments command, *rest = *arguments
rest = process_options(rest)
lookup_command(command).run(*rest) lookup_command(command).run(*rest)
end end
@ -34,6 +38,16 @@ class Cask::CLI
} }
list.sort list.sort
end 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 class NullCommand
@ -43,7 +57,7 @@ class Cask::CLI
def run(*args) def run(*args)
purpose purpose
if @attempted_name if @attempted_name and @attempted_name != "help"
puts "!! " puts "!! "
puts "!! no command with name: #{@attempted_name}" puts "!! no command with name: #{@attempted_name}"
puts "!! " puts "!! "
@ -52,7 +66,7 @@ class Cask::CLI
end end
def purpose def purpose
puts <<-PURPOSE.gsub(/^ {6}/, '') puts <<-PURPOSE.undent
{{ brew-cask }} {{ brew-cask }}
brew-cask provides a friendly homebrew-style CLI workflow for the brew-cask provides a friendly homebrew-style CLI workflow for the
administration Mac applications distributed as binaries administration Mac applications distributed as binaries

26
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

2
test/support/fake_appdir.rb

@ -1,6 +1,6 @@
# wire in a fake appdir for linkapps # wire in a fake appdir for linkapps
CANNED_APPDIR = (HOMEBREW_REPOSITORY/"Applications") CANNED_APPDIR = (HOMEBREW_REPOSITORY/"Applications")
Cask.set_appdir(CANNED_APPDIR) Cask.appdir = CANNED_APPDIR
module FakeAppdirHooks module FakeAppdirHooks
def before_setup def before_setup

Loading…
Cancel
Save