class Commando::Config

Manage the configuration for the actions available to the CLI

Constants

DEFAULT_GREETING
DEFAULT_PROMPT

Attributes

greeting[W]
history_file[RW]
mapping[R]
output[W]
prompt[W]

Public Class Methods

new() click to toggle source
# File lib/commando/config.rb, line 19
def initialize
  @mapping = {}

  # Register the default actions
  register(
    'help',
    Commando::Action::Help.new(config: self),
    'Print this message'
  )
  register(
    'history',
    Commando::Action::History.new(config: self),
    'Print the history of commands'
  )
  register(
    'quit',
    Commando::Action::Quit.new,
    'Exit the program'
  )
end

Public Instance Methods

commands() click to toggle source

@return [Array<String>] the list of all configured actions.

# File lib/commando/config.rb, line 63
def commands
  @mapping.keys
end
descriptions() click to toggle source

@return [Hash<String, String>] a map of commands to their descriptions

# File lib/commando/config.rb, line 75
def descriptions
  mapping.map.with_object({}) do |(command, action_config), hash|
    hash[command] = action_config.description
  end
end
greeting() click to toggle source
# File lib/commando/config.rb, line 48
def greeting
  @greeting || DEFAULT_GREETING
end
lookup(command) click to toggle source

@param command [String] the command to find an action class for. @return [Class] the action class that is registered for the command,

nil if none is registered.
# File lib/commando/config.rb, line 70
def lookup(command)
  mapping[command]&.action_class
end
output() click to toggle source
# File lib/commando/config.rb, line 40
def output
  @output || $stdout
end
prompt() click to toggle source
# File lib/commando/config.rb, line 44
def prompt
  @prompt || DEFAULT_PROMPT
end
register(command, action_class, description) click to toggle source

Register a new command/action to be available to the CLI

@param command [String] the string that a user will type to execute the

action, e.g. "help" for the command the will print out help info.

@param action_class [Class] the class that will execute the logic for

the given command.
# File lib/commando/config.rb, line 58
def register(command, action_class, description)
  mapping[command] = ActionConfig.new(action_class, description)
end