module Fuelcell
Exposes Fuelcell’s main interface. You can describe a complete command heirarchy, and start the processing/execution of a command all at the same time using Fuelcell.start
. Fuelcell.define
allows you to describe a command heirarchy using separate files. The cli interfaces are needed when :start & :define are used togather, allowing :define to access the root command
Constants
- VERSION
Attributes
Assigning the cli represents the current execution context for which commandline processing takes place. This is triggered by the :start method and is needed to allow the :define method to work in a separate file the was required by the script using :start
The alias :make_cli_available_for_definitions is ment to describe the intent of the :start method
Assigning the cli represents the current execution context for which commandline processing takes place. This is triggered by the :start method and is needed to allow the :define method to work in a separate file the was required by the script using :start
The alias :make_cli_available_for_definitions is ment to describe the intent of the :start method
Public Class Methods
# File lib/fuelcell.rb, line 23 def cli? ! @cli.nil? end
Allows for the configuration of the Cli
object which controls command line processing.
@param settings [Hash]
root_name: used to override the default script name root_cmd: override the RootCommand object with your own shell: override the Shell object with your own backtrace: causes backtraces to be printed to error stream output_stream: override $stdout as the output stream input_stream: override $stdin as the input stream error_stream: override $stderr as the error stream exit: cause fuelcell not to explicitly exit, use false parser: override opt & args parsing cmd_args_extractor: override command extraction strategy
@return [Fuelcell::Cli]
# File lib/fuelcell.rb, line 84 def create_cli(settings = {}) root_name = settings[:root_name] root_cmd = settings.fetch(:root_cmd) { Action::Root.new(root_name) } shell = settings.fetch(:shell) { Shell.new(settings) } Cli.new(root: root_cmd, shell: shell) end
Allows definitions to be created in a separate file and have them included by load/require in the start block.
@param name [String] name of the command to be defined @yield instance_eval into the root command
# File lib/fuelcell.rb, line 50 def define(name, &block) name = name.to_s if name.empty? fail ArgumentError, 'command name can not be empty' end unless cli? fail RuntimeError, 'cli interface does not exist, please use ' \ 'Fuelcell.start before Fuelcell.define' end root = cli.root cmd_args = name.split(' ') root.ensure_command_hierarchy(cmd_args) cmd = root[name] cmd.instance_eval(&block) end
Remove the cli object & replace it with nil. The general case for this is in the :start method when no more command definition will be processed. Since we don’t know for sure how long we will be in memory we clean up whats not used
The alias :remove_cli_availability is an attempt to improve intent of the :start method
@return [Nil]
# File lib/fuelcell.rb, line 40 def remove_cli @cli = nil end
Entry point for executing a command which was defined through the the fuelcell dsl. The executable command should return an exit code, used to exit the program.
@param args [Array] command line args to be processed @param settings [Hash] allows for overriding parts of fuelcell @yield optional block, will instance_eval on the root command @return [Int]
# File lib/fuelcell.rb, line 99 def start(args = [], settings = {}, &block) cli = settings[:cli] || create_cli(settings) root = cli.root make_cli_available_for_definitions(cli) # expose the command dsl root.instance_eval(&block) if block_given? context = cli.parse(args) remove_cli_availability code = cli.execute(context) cli.handle_exit code end