Class: Fuelcell::Cli
- Inherits:
-
Object
- Object
- Fuelcell::Cli
- Defined in:
- lib/fuelcell/cli.rb
Instance Attribute Summary collapse
-
#cmd_args_extractor ⇒ Object
readonly
Returns the value of attribute cmd_args_extractor.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#shell ⇒ Object
readonly
Returns the value of attribute shell.
Instance Method Summary collapse
-
#execute(context) ⇒ Integer
Executes the callable object in a command.
- #exit? ⇒ Boolean
- #handle_callable_option(root, cmd) ⇒ Object
-
#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.
-
#initialize(config = {}) ⇒ Cli
constructor
Initializes with a root command object.
-
#parse(raw_args) ⇒ Hash
Delegates all parsing responsiblities to a series of handlers, returning a structured hash needed to execute a command.
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_extractor ⇒ Object (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 |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
7 8 9 |
# File 'lib/fuelcell/cli.rb', line 7 def parser @parser end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/fuelcell/cli.rb', line 7 def root @root end |
#shell ⇒ Object (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.
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
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
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
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.(cmd) begin parser.call(cmd, cmd_args, raw_args) rescue Exception => e shell.error e. shell.failure_exit end end |