Module: Rbcli

Defined in:
lib/rbcli/version.rb,
lib/rbcli/util/exit.rb,
lib/rbcli/util/errors.rb,
lib/rbcli-tool/helpers.rb,
lib/rbcli/components/core/engine.rb,
lib/rbcli/components/logger/logger.rb,
lib/rbcli/components/commands/command.rb

Overview

Rbcli – A framework for developing command line applications in Ruby #

Copyright (C) 2024 Andrew Khoury <akhoury@live.com>                        #

Defined Under Namespace

Modules: Configurable, Configurate, Engine, Parser, ToolHelpers, UpdateChecker, UserConf, Warehouse Classes: Command, CommandError, Config, ConfigOfDeath, ConfigurateError, DeprecationWarning, Error, Logger, ParseError, ScriptWrapper

Constant Summary collapse

VERSION =
File.read(File.expand_path(File.join(File.dirname(__FILE__), '../../VERSION'))).chomp.strip

Class Method Summary collapse

Class Method Details

.command(name, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rbcli/components/commands/command.rb', line 87

def self.command name, & block
  name = name.to_s.downcase
  raise Rbcli::CommandError.new "The command #{name} cannot be defined twice." if Rbcli::Warehouse.get(:commands).key? name
  command = Rbcli::Command.new(name)
  command.instance_eval(&block)
  unless command.has_function?
    raise Rbcli::CommandError.new "Command #{command.name} does not have a function. An action, inline script, or external script must be defined."
  end
  if command.default? && Rbcli::Warehouse.get(:commands).any? { |_k, cmd| cmd.default? }
    raise Rbcli::CommandError.new "Only one command may be assigned as default"
  end
  Rbcli::Warehouse.get(:commands)[name] = command

  ## The first time a command is declared the engine gets primed to run commands
  Rbcli::Engine.register_operation(Proc.new {
    if Rbcli::Warehouse.get(:commands).empty?
      raise Rbcli::CommandError.new "No commands defined. At least one command must be declared to use Rbcli."
    end
    cmd_to_run = Rbcli::Warehouse.get(:command, :parsedopts)
    if cmd_to_run.nil?
      Rbcli.log.debug "No command specified and no default set. Skipping execution.", "CORE"
      break
    end
    Rbcli.log.debug "Running command '#{cmd_to_run.name}'", "CORE"
    cmd_to_run.run!(
      opts: Rbcli::Warehouse.get(:opts, :parsedopts),
      params: Rbcli::Warehouse.get(:params, :parsedopts),
      args: Rbcli::Warehouse.get(:args, :parsedopts),
      config: Rbcli::Warehouse.get(:config, :parsedopts) || Rbcli::ConfigOfDeath.new,
      env: Rbcli::Warehouse.get(:env, :parsedopts) || Rbcli::ConfigOfDeath.new
    )
  }, name: :run_command, priority: 100) if Rbcli::Warehouse.get(:commands).count == 1
end

.exit(code) ⇒ Object



9
10
11
# File 'lib/rbcli/util/exit.rb', line 9

def self.exit code
  @real_exit.call(code)
end

.go!Object



27
28
29
# File 'lib/rbcli/components/core/engine.rb', line 27

def self.go!
  Rbcli::Engine.run!
end

.logObject



152
153
154
155
# File 'lib/rbcli/components/logger/logger.rb', line 152

def self.log
  self.start_logger if @logger.nil?
  @logger
end

.start_logger(target: STDOUT, level: :info, format: :display) ⇒ Object



148
149
150
# File 'lib/rbcli/components/logger/logger.rb', line 148

def self.start_logger target: STDOUT, level: :info, format: :display
  @logger = Rbcli::Logger.new(target: target, level: level, format: format)
end