class Fuelcell::Cli

Attributes

cmd_args_extractor[R]
parser[R]
root[R]
shell[R]

Public Class Methods

new(config = {}) click to toggle source

Initializes with a root command object

When nothing is given we default to the script name otherwise you choose the name of the root command or the command itself

# File lib/fuelcell/cli.rb, line 14
def initialize(config = {})
  @exit   = config.fetch(:exit)  { true }
  @exit   = @exit == false ? false : true
  @root   = config.fetch(:root)  { Action::Root.new }
  @shell  = config.fetch(:shell) { Shell.new }
  @parser = config.fetch(:parser) {
    Parser::ParsingStrategy.new
  }

  @cmd_args_extractor = config.fetch(:cmd_args_extractor) {
    Parser::CmdArgsStrategy.new
  }
end

Public Instance Methods

execute(context) click to toggle source

Executes the callable object in a command. All command callable object expect to be called the the options hash, arg hash and shell object.

@param context [Hash] @return [Integer]

# File lib/fuelcell/cli.rb, line 55
def execute(context)
  cmd       = context[:cmd]
  opts      = context[:opts] || {}
  args      = context[:args] || []
  cmd_args  = context[:cmd_args]
  cli_shell = context[:shell] || shell

  unless cmd.callable?
    return root['help'].call(opts, cmd_args, shell)
  end

  cmd = handle_callable_option(root, cmd)
  cmd.call(opts, args, cli_shell)

end
exit?() click to toggle source
# File lib/fuelcell/cli.rb, line 82
def exit?
  @exit
end
handle_callable_option(root, cmd) click to toggle source
# File lib/fuelcell/cli.rb, line 71
def handle_callable_option(root, cmd)
  opt_manager = cmd.opts
  opt = opt_manager.callable
  return cmd unless opt
  if opt.cmd_path?
    path = opt.cmd_path.split(' ')
    return root.locate(path)
  end
  opt
end
handle_exit(code) click to toggle source

Allows the system to by pass the exit call which is helpful in testing and when trying to manually control the system

@param code [Int] integer from 0 .. 255 representing the exit code @return [Int] when exit is false

# File lib/fuelcell/cli.rb, line 91
def handle_exit(code)
  shell.exit code if exit?
  code
end
parse(raw_args) click to toggle source

Delegates all parsing responsiblities to a series of handlers, returning a structured hash needed to execute a command. The command being executed is determined by the CmdArgsStrategy unless you override it, it extracts all args upto the first option or ignore. The RootCommand is used to find the command using the extracted args, it accounts for sub commands. The parser Parser::ParsingStategy handles processing opts, args and ignored args

@param raw_args [Array] cli args usually from ARGV @return [Hash] structured context for executing a command

# File lib/fuelcell/cli.rb, line 38
def parse(raw_args)
  cmd_args = cmd_args_extractor.call(raw_args)
  cmd      = root.locate(cmd_args, raw_args)
  root.add_global_options(cmd)
  begin
    parser.call(cmd, cmd_args, raw_args)
  rescue Exception => e
    shell.error e.message
    shell.failure_exit
  end
end