Class: Fuelcell::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/fuelcell/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Cli

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



14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#cmd_args_extractorObject (readonly)

Returns the value of attribute cmd_args_extractor.



7
8
9
# File 'lib/fuelcell/cli.rb', line 7

def cmd_args_extractor
  @cmd_args_extractor
end

#parserObject (readonly)

Returns the value of attribute parser.



7
8
9
# File 'lib/fuelcell/cli.rb', line 7

def parser
  @parser
end

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/fuelcell/cli.rb', line 7

def root
  @root
end

#shellObject (readonly)

Returns the value of attribute shell.



7
8
9
# File 'lib/fuelcell/cli.rb', line 7

def shell
  @shell
end

Instance Method Details

#execute(context) ⇒ Integer

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

Parameters:

  • context (Hash)

Returns:

  • (Integer)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/fuelcell/cli.rb', line 82

def exit?
  @exit
end

#handle_callable_option(root, cmd) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Int

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

Parameters:

  • code (Int)

    integer from 0 .. 255 representing the exit code

Returns:

  • (Int)

    when exit is false



91
92
93
94
# File 'lib/fuelcell/cli.rb', line 91

def handle_exit(code)
  shell.exit code if exit?
  code
end

#parse(raw_args) ⇒ Hash

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

Parameters:

  • raw_args (Array)

    cli args usually from ARGV

Returns:

  • (Hash)

    structured context for executing a command



38
39
40
41
42
43
44
45
46
47
48
# 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