class Fission::CommandLineParser

def command

Returns nil if the parse command has not yet been run.
Returns an instance of the determined command if the arguments are valid.

@command_line_parser.command

Examples:

by running the parse method.
Internal: Accessor for an instance of the determined command. This is set
def command
  @command
end

def commands_help

Outputs the summary text for all known commands.

@cli.commands_help

Examples

Internal: Provides the help of all of the 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:"
  Hash[@command_names_and_summaries.sort].each_pair do |name, summary|
    ui.output_printf "%-#{longest_cmd.length}s      %s\n", name, summary
  end
end

def determine_command_provided

of the appropriate command class (assuming it is valid).
Returns nothing. This will set the @command instance variable to an instance

@cli.determine_command_to_execute

Examples:

be displayed and it will exit.
determined that an invalid command is provided, then the help/usage will
Internal: Determines the command that has been provided. If it is
def determine_command_provided
  if @commands.include? @args.first
    @command = Command.const_get(@args.first.capitalize).new @args.drop 1
  elsif is_snapshot_command?
    klass = @args.take(2).map {|c| c.capitalize}.join('')
    @command = Command.const_get(klass).new @args.drop 2
  else
    show_all_help
    exit 1
  end
end

def gather_commands_and_summaries

variable with the result.
Returns nothing. This will set the @command_names_and_summaries instance

# => { 'clone' => 'Clones a VM', 'stop' => 'Stops a VM' }
@cli.command_names_and_summaries

Examples

Internal: Determines all of the available commands and their summaries.
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

def initialize(args=ARGV)

Returns a Fission::CommandLineParser object.

CommandLineParser.new

CommandLineParser.new ['foo', 'bar']

Examples:

same format of ARGV (Array) (default: ARGV).
args - The command line arguments to parse. This is expected to be in the

Internal: Creates a new Fission::CommandLineParser object.
def initialize(args=ARGV)
  @args = args
  gather_commands_and_summaries
  setup_options_parser
end

def is_snapshot_command?

Returns a Boolean of whether a snapshot command was given or not.

# => true
@cli.is_snapshot_command? ['snapshot', 'list']

# => false
@cli.is_snapshot_command? ['foo', 'bar']

Examples

determination.
command. This will use the @args instance variable to make the
Internal: Determines if the provided command is a snapshot related
def is_snapshot_command?
  @args.first == 'snapshot' &&
  @args.count > 1 &&
  @commands.include?(@args.take(2).join(' '))
end

def parse

Returns nothing.

@command_line_parser.parse

Examples:

instance of the determined command class.
arguments are valid, then the @command variable will be set to a new
invalid, the appropriate help will be output and then will exit. If the
Internal: Parses the command line arguments. If the arguments are
def parse
  @options_parser.order! @args
  determine_command_provided
  self
rescue OptionParser::InvalidOption => e
  ui.output e
  show_all_help
  exit 1
end

def setup_options_parser

Returns nothing. This will set the @option_parser instance variable.

@cli.setup_option_parser

Examples:

Internal: Sets up the base option parser.
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

def show_all_help

Returns nothing.

# => 'fission options command arguments ....'
@cli.show_all_help

Examples

summaries.
Internal: Outputs the usage as well as the known commands and their
def show_all_help
  ui.output @options_parser
  commands_help
end

def ui

Returns a UI instance.

@cli.ui.output 'foo'

Examples

Internal: Helper method for outputting text to the ui
def ui
  @ui ||= UI.new
end