diff --git a/lib/cask/cli.rb b/lib/cask/cli.rb index b2c22a4d8..7252d16cf 100644 --- a/lib/cask/cli.rb +++ b/lib/cask/cli.rb @@ -38,17 +38,29 @@ class Cask::CLI } list.sort end - - def self.process_options(args) - allrgs = Shellwords.shellsplit(ENV['HOMEBREW_CASK_OPTS'] || "") + args - OptionParser.new do |opts| + + def self.parser + @parser ||= 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 end + def self.process_options(args) + all_args = Shellwords.shellsplit(ENV['HOMEBREW_CASK_OPTS'] || "") + args + remaining = [] + while !all_args.empty? + begin + head = all_args.shift + remaining.concat(parser.parse([head])) + rescue OptionParser::InvalidOption + remaining << head + retry + end + end + remaining + end class NullCommand def initialize(attempted_name) diff --git a/lib/cask/cli/edit.rb b/lib/cask/cli/edit.rb index 4d6095ee8..23d5de3a0 100644 --- a/lib/cask/cli/edit.rb +++ b/lib/cask/cli/edit.rb @@ -1,8 +1,10 @@ module Cask::CLI::Edit def self.run(*arguments) - cask_name, *_ = *arguments + cask_name, *args = *arguments cask_path = Cask.path(cask_name) - raise CaskUnavailableError, cask_name + ".rb" if cask_path.nil? || !cask_path.file? + unless cask_path.exist? || args.include?('--create') + raise CaskUnavailableError, "#{cask_name}, add --create to make a new cask with this name" + end exec_editor cask_path end diff --git a/test/cli/edit_test.rb b/test/cli/edit_test.rb index feb5c8357..0a00e56c6 100644 --- a/test/cli/edit_test.rb +++ b/test/cli/edit_test.rb @@ -36,4 +36,17 @@ describe Cask::CLI::Edit do [Cask.path('adium')] ] end + + it 'raises an exception when the cask doesnt exist' do + lambda { + Cask::CLI::Edit.run('notacask') + }.must_raise CaskUnavailableError + end + + it 'allows new casks to be created with the --create flag' do + Cask::CLI::Edit.run('brand-spankin-new', '--create') + Cask::CLI::Edit.editor_commands.must_equal [ + [Cask.tapspath.join(Cask.default_tap, 'Casks', 'brand-spankin-new.rb')] + ] + end end diff --git a/test/cli/options_test.rb b/test/cli/options_test.rb index aae62ed1f..0727fbc07 100644 --- a/test/cli/options_test.rb +++ b/test/cli/options_test.rb @@ -2,21 +2,24 @@ 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::CLI.process_options %w{help --appdir=/some/path/foo} - Cask.appdir.must_equal Pathname.new File.expand_path "/some/path" + Cask.appdir.must_equal Pathname('/some/path/foo') end it "supports setting the appdir from ENV" do - ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/some/path" + ENV['HOMEBREW_CASK_OPTS'] = "--appdir=/some/path/bar" - shutup do - Cask::CLI.process %w{help} - end + Cask::CLI.process_options %w{help} - Cask.appdir.must_equal Pathname.new File.expand_path "/some/path" + Cask.appdir.must_equal Pathname('/some/path/bar') + end + + it "allows additional options to be passed through" do + rest = Cask::CLI.process_options %w{edit foo --create --appdir=/some/path/qux} + + Cask.appdir.must_equal Pathname('/some/path/qux') + rest.must_equal %w{edit foo --create} end after do