Permalink
Browse files

refactoring of cli and command_line_parser classes

  • Loading branch information...
1 parent df0d76f commit 9dd94182dfa0c35db294abb2bb237cbc433c2cb7 @thbishop committed Feb 3, 2012
Showing with 238 additions and 345 deletions.
  1. +9 −144 lib/fission/cli.rb
  2. +20 −4 lib/fission/command_line_parser.rb
  3. +29 −193 spec/fission/cli_spec.rb
  4. +180 −4 spec/fission/command_line_parser_spec.rb
View
@@ -10,16 +10,12 @@ class CLI
# Fission::CLI.new
#
# Returns a Fission::CLI object.
- def initialize(args=ARGV)
- @args = args
+ def initialize(args=ARGV, parser=CommandLineParser)
+ @args = args ||= ARGV
- setup_options_parser
+ @parser = parser.new @args
- gather_commands_and_summaries
-
- parse_options
-
- determine_command_to_execute
+ parse_arguments
end
# Internal: Execute the determined command.
@@ -34,147 +30,16 @@ def execute
end
private
- # Internal: Determines the command that has been provided. If it is
- # determined that an invalid command is provided, then the help/usage will
- # be displayed and it will exit.
+ # Internal: Parses the arguments using the parser.
#
# Examples:
#
- # @cli.determine_command_to_execute
- #
- # Returns nothing. This will set the @cmd instance variable to an instance
- # of the appropriate command class (assuming it is valid).
- def determine_command_to_execute
- if @commands.include? @args.first
- @cmd = Command.const_get(@args.first.capitalize).new @args.drop 1
- elsif is_snapshot_command?
- klass = @args.take(2).map {|c| c.capitalize}.join('')
- @cmd = Command.const_get(klass).new @args.drop 2
- else
- show_all_help
- exit 1
- end
- end
-
- # Internal: Sets up the base option parser.
- #
- # Examples:
- #
- # @cli.setup_option_parser
- #
- # Returns nothing. This will set the @option_parser instance variable.
- def setup_options_parser
- @options_parser = OptionParser.new do |opts|
- opts.banner = "\nUsage: fission [options] COMMAND [arguments]"
-
- opts.on_head('-v', '--version', 'Output the version of fission') do
- ui.output VERSION
- exit 0
- end
-
- opts.on_head('-h', '--help', 'Displays this message') do
- show_all_help
- exit 0
- end
- end
- end
-
- # Internal: Parse the options.
- #
- # Examples:
- #
- # @cli.parse_options
- #
- # Returns nothing. This will display an error, the help/usage, and exit if
- # there are any invalid options found.
- def parse_options
- @options_parser.order! @args
- rescue OptionParser::InvalidOption => e
- ui.output e
- show_all_help
- exit 1
- end
-
- # Internal: Determines if the provided command is a snapshot related
- # command. This will use the @args instance variable to make the
- # determination.
- #
- # Examples
- #
- # @cli.is_snapshot_command? ['foo', 'bar']
- # # => false
- #
- # @cli.is_snapshot_command? ['snapshot', 'list']
- # # => true
- #
- # Returns a Boolean of whether a snapshot command was given or not.
- def is_snapshot_command?
- @args.first == 'snapshot' &&
- @args.count > 1 &&
- @commands.include?(@args.take(2).join(' '))
- end
-
- # Internal: Provides the help of all of the known commands.
- #
- # Examples
- #
- # @cli.commands_help
- #
- # Outputs the summary text for all known commands.
- def commands_help
- longest_cmd = @commands.inject do |longest, cmd_name|
- longest.length > cmd_name.length ? longest : cmd_name
- end
-
- ui.output "\nCommands:"
-
- @command_names_and_summaries.each_pair do |name, summary|
- ui.output_printf "%-#{longest_cmd.length}s %s\n", name, summary
- end
- end
-
- # Internal: Determines all of the available commands and their summaries.
- #
- # Examples
- #
- # @cli.command_names_and_summaries
- # # => { 'clone' => 'Clones a VM', 'stop' => 'Stops a VM' }
- #
- # Returns nothing. This will set the @command_names_and_summaries instance
- # variable with the result.
- def gather_commands_and_summaries
- @command_names_and_summaries = Command.descendants.inject({}) do |result, klass|
- cmd = klass.new
- result[cmd.command_name] = cmd.summary
- result
- end
-
- @commands = @command_names_and_summaries.keys.sort
- end
-
- # Internal: Outputs the usage as well as the known commands and their
- # summaries.
- #
- # Examples
- #
- # @cli.show_all_help
- # # => 'fission options command arguments ....'
+ # @cli.parse_arguments
#
# Returns nothing.
- def show_all_help
- ui.output @options_parser
- commands_help
- end
-
- # Internal: Helper method for outputting text to the ui
- #
- # Examples
- #
- # @cli.ui.output 'foo'
- #
- # Returns a UI instance.
- def ui
- @ui ||= UI.new
+ def parse_arguments
+ @parser.parse
+ @cmd = @parser.command
end
end
@@ -23,7 +23,9 @@ def initialize(args=ARGV)
end
# Internal: Parses the command line arguments. If the arguments are
- # invalid, the appropriate help will be output and then will exit.
+ # invalid, the appropriate help will be output and then will exit. If the
+ # arguments are valid, then the @command variable will be set to a new
+ # instance of the determined command class.
#
# Examples:
#
@@ -34,12 +36,26 @@ def parse
@options_parser.order! @args
determine_command_provided
+ self
rescue OptionParser::InvalidOption => e
ui.output e
show_all_help
exit 1
end
+ # Internal: Accessor for an instance of the determined command. This is set
+ # by running the parse method.
+ #
+ # Examples:
+ #
+ # @command_line_parser.command
+ #
+ # Returns an instance of the determined command if the arguments are valid.
+ # Returns nil if the parse command has not yet been run.
+ def command
+ @command
+ end
+
private
# Internal: Sets up the base option parser.
#
@@ -110,14 +126,14 @@ def gather_commands_and_summaries
#
# @cli.determine_command_to_execute
#
- # Returns nothing. This will set the @cmd instance variable to an instance
+ # Returns nothing. This will set the @command instance variable to an instance
# of the appropriate command class (assuming it is valid).
def determine_command_provided
if @commands.include? @args.first
- @cmd = Command.const_get(@args.first.capitalize).new @args.drop 1
+ @command = Command.const_get(@args.first.capitalize).new @args.drop 1
elsif is_snapshot_command?
klass = @args.take(2).map {|c| c.capitalize}.join('')
- @cmd = Command.const_get(klass).new @args.drop 2
+ @command = Command.const_get(klass).new @args.drop 2
else
show_all_help
exit 1
Oops, something went wrong.

0 comments on commit 9dd9418

Please sign in to comment.