class Brainstem::Cli
Constants
- EXECUTABLE_NAME
Attributes
Holds a copy of the initial given args for debugging purposes.
Stores the method we should call to run the user command.
Stores the method we should use to log messages.
Storage for the extracted command.
Public Class Methods
Convenience for instantiating and calling the Cli
object.
@return [Brainstem::Cli] the created instance
# File lib/brainstem/cli.rb, line 19 def self.call(args, options = {}) new(args, options).call end
Creates a new instance of the Cli
to respond to user input.
Input is expected to be the name of the subcommand, followed by any additional arguments.
Brainstem::Concerns::Optional::new
# File lib/brainstem/cli.rb, line 29 def initialize(args, options = {}) super options self._args = args.dup.freeze self.requested_command = args.shift end
Public Instance Methods
Routes to an application endpoint depending on given options.
@return [Brainstem::Cli] the instance
# File lib/brainstem/cli.rb, line 41 def call if requested_command && commands.has_key?(requested_command) self.command_method = commands[requested_command].method(:call) end command_method.call(_args.drop(1)) self end
Private Instance Methods
Reader for the method to invoke. By default, will output the help text when called.
@return [Proc] An object responding to #call
that is used as the main point execution.
# File lib/brainstem/cli.rb, line 107 def command_method @command_method ||= Proc.new do # By default, serve help. log_method.call(help_text) end end
A basic routing table where the keys are the command to invoke, and where the value is a callable object or class that will be called with the command_options
.
@return [Hash] A hash of +'command' => Callable+
# File lib/brainstem/cli.rb, line 83 def commands { 'generate' => Brainstem::CLI::GenerateApiDocsCommand } end
Retrieves the help text and subs any placeholder values.
# File lib/brainstem/cli.rb, line 90 def help_text @help_text ||= File.read(File.expand_path('../help_text.txt', __FILE__)) .gsub('EXECUTABLE_NAME', EXECUTABLE_NAME) end
Reader for the method to log. By default, will print to stdout when called.
We return a proc here because it's much tricker to make assertions against stdout than it is to allow ourselves to inject a proc.
@return [Proc] An object responding to #call
that is used to print information.
# File lib/brainstem/cli.rb, line 129 def log_method @log_method ||= $stdout.method(:puts) end
A whitelist of valid options that can be applied to the instance.
@returns [Array<String>]
Brainstem::Concerns::Optional#valid_options
# File lib/brainstem/cli.rb, line 70 def valid_options super | [ :log_method ] end