class Bogo::Cli::Setup

Public Class Methods

define(&block) click to toggle source

Wrap parsing setup for consistent usage

@yield CLI setup block @return [TrueClass]

# File lib/bogo-cli/setup.rb, line 29
def define(&block)
  begin
    result = Parser.parse(help: true) do
      instance_exec(&block)
    end
    puts result.help
    exit 255
  rescue StandardError, ScriptError => err
    err_msg = err.message
    if err.respond_to?(:original) && err.original
      err_msg << "\n#{err.original.message}"
    end
    output_error err_msg
    if ENV["DEBUG"] || (Command.ui && Command.ui.options[:debug])
      output_debug "Stacktrace: #{err.class}: " \
                   "#{err.message}\n#{err.backtrace.join("\n")}"
      if err.respond_to?(:original) && err.original
        msg = "Original Stacktrace: #{err.original.class}: " \
        "#{err.original.message}\n#{err.original.backtrace.join("\n")}"
        output_debug msg
      end
    end
    exit err.respond_to?(:exit_code) ? err.exit_code : -1
  end
  true
end
output_debug(string) click to toggle source

Write debug message to UI. Uses formatted ui if available and falls back to stderr.

@param string [String]

# File lib/bogo-cli/setup.rb, line 72
def output_debug(string)
  if Command.ui
    Command.ui.debug string
  else
    $stderr.puts "DEBUG: #{string}"
  end
end
output_error(string) click to toggle source

Write error message to UI. Uses formatted ui if available and falls back to stderr.

@param string [String]

# File lib/bogo-cli/setup.rb, line 60
def output_error(string)
  if Command.ui
    Command.ui.error string
  else
    $stderr.puts "ERROR: #{string}"
  end
end