Skip to content

Commit

Permalink
Run the help command when no argument is given
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosantoniodasilva committed Jan 16, 2011
1 parent f3f93d9 commit f820675
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
12 changes: 10 additions & 2 deletions lib/campfire/cli.rb
Expand Up @@ -6,12 +6,14 @@ class Cli

def initialize(argv)
@argv = argv
@options = { :command => :rooms }
@options = {}
parse_options
validate_options
validate_options if options?
end

def run
return options_parser.inspect unless options?

obj = manager.send(*Array(@options[:command]))
obj = obj.send(*@options[:subcommand]) if @options[:subcommand]
obj
Expand All @@ -34,6 +36,8 @@ def options_parser
@options[:token] = value
end

opt.separator ""

opt.on "--rooms", "List all rooms" do
@options[:command] = :rooms
end
Expand All @@ -56,6 +60,10 @@ def options_parser
end
end

def options?
!@options.empty?
end

def validate_options
[:subdomain, :token].each do |required_option|
next if options[required_option]
Expand Down
21 changes: 10 additions & 11 deletions test/campfire/cli_test.rb
Expand Up @@ -21,23 +21,12 @@ def test_invalid_option_raises_error
end
end

def test_blank_option_raises_error
assert_raises OptionParser::MissingArgument do
Campfire::Cli.new([])
end
end

def test_missing_required_option
assert_raises OptionParser::MissingArgument do
Campfire::Cli.new(%w(-s foo))
end
end

def test_default_command_options
cli = Campfire::Cli.new(%w[-s foo -t 123])
assert_equal :rooms, cli.options[:command]
end

def test_rooms_command_options
cli = Campfire::Cli.new(%w[-s foo -t 123 --rooms])
assert_equal :rooms, cli.options[:command]
Expand Down Expand Up @@ -82,4 +71,14 @@ def test_run_executes_the_given_command_with_subcommand
message = cli.run
assert_equal "hello world!", message.body
end

def test_help
cli = Campfire::Cli.new([])

help_message = cli.run
assert_match /Campfire Command Line Tool/, help_message
assert_match /--rooms/, help_message
assert_match /--search.*Search the given term/, help_message
assert_match /--speak/, help_message
end
end

0 comments on commit f820675

Please sign in to comment.