diff --git a/lib/campfire/cli.rb b/lib/campfire/cli.rb index 384613e..c0e10cc 100644 --- a/lib/campfire/cli.rb +++ b/lib/campfire/cli.rb @@ -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 @@ -34,6 +36,8 @@ def options_parser @options[:token] = value end + opt.separator "" + opt.on "--rooms", "List all rooms" do @options[:command] = :rooms end @@ -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] diff --git a/test/campfire/cli_test.rb b/test/campfire/cli_test.rb index 0ac71c6..74231d1 100644 --- a/test/campfire/cli_test.rb +++ b/test/campfire/cli_test.rb @@ -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] @@ -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