Skip to content

Commit

Permalink
Merge pull request Homebrew#117 from passcod/options-support
Browse files Browse the repository at this point in the history
Add options to the cli
  • Loading branch information
phinze committed Dec 20, 2012
2 parents 0d1f831 + 5ad6796 commit fca6c49
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def self.appdir
@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
Expand Down
18 changes: 16 additions & 2 deletions lib/cask/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'optparse'
require 'shellwords'

class Cask::CLI
def self.commands
Cask::CLI.constants - ["NullCommand"]
Expand All @@ -14,6 +17,7 @@ def self.lookup_command(command)
def self.process(arguments)
Cask.init
command, *rest = *arguments
rest = process_options(rest)
lookup_command(command).run(*rest)
end

Expand All @@ -34,6 +38,16 @@ def self.nice_listing(cask_list)
}
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
Expand All @@ -43,7 +57,7 @@ def initialize(attempted_name)

def run(*args)
purpose
if @attempted_name
if @attempted_name and @attempted_name != "help"
puts "!! "
puts "!! no command with name: #{@attempted_name}"
puts "!! "
Expand All @@ -52,7 +66,7 @@ def run(*args)
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
Expand Down
26 changes: 26 additions & 0 deletions test/cli/options_test.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion test/support/fake_appdir.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit fca6c49

Please sign in to comment.