class Baf::CLI

Constants

ArgumentError
EX_SOFTWARE
EX_USAGE

Attributes

arguments[R]
env[R]
parser[R]
registrant[R]

Public Class Methods

new(env, arguments, **opts) click to toggle source
# File lib/baf/cli.rb, line 52
def initialize env, arguments, **opts
  @env = env
  @arguments = arguments
  @parser = opts.fetch(:parser) { OptionParser.new }
  @registrant = opts.fetch(:registrant) { OptionsRegistrant.new }

  registrant.register(env, parser) { setup }
end
run(arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr) click to toggle source
# File lib/baf/cli.rb, line 17
def run arguments, stdin: $stdin, stdout: $stdout, stderr: $stderr
  cli = new env = build_env(stdin, stdout, stderr), arguments
  cli.parse_arguments!
  cli.run
rescue ArgumentError => e
  stderr.puts e
  exit EX_USAGE
rescue StandardError => e
  if respond_to? :handle_error
    status = handle_error cli.env, e
    exit status if status.respond_to? :to_int
  else
    stderr.puts "#{e.class.name}: #{e}"
    stderr.puts e.backtrace.map { |l| '  %s' % l }
  end
  exit EX_SOFTWARE
end

Private Class Methods

build_env(stdin, stdout, stderr) click to toggle source
# File lib/baf/cli.rb, line 37
def build_env stdin, stdout, stderr
  env_class.new input: stdin, output: stdout, output_error: stderr
end
env_class() click to toggle source
# File lib/baf/cli.rb, line 41
def env_class
  return Env unless parent_name = name =~ /::[^:]+\Z/ ? $` : nil
  parent = Object.const_get parent_name
  parent.const_defined?(:Env) ? parent.const_get(:Env) : Env
end
ruby2_keywords(*;) click to toggle source
# File lib/baf/cli.rb, line 47
def ruby2_keywords *; end

Public Instance Methods

banner(arg) click to toggle source
flag(*args) click to toggle source
# File lib/baf/cli.rb, line 69
def flag *args
  registrant.flag *args
end
flag_debug() click to toggle source
# File lib/baf/cli.rb, line 73
def flag_debug
  flag :d, :debug
end
flag_verbose() click to toggle source
# File lib/baf/cli.rb, line 77
def flag_verbose
  flag :v, :verbose
end
flag_version(version) click to toggle source
# File lib/baf/cli.rb, line 81
def flag_version version
  flag :V, :version, 'print version', -> *, env { env.puts version; exit },
    tail: true
end
option(*args, &block) click to toggle source
# File lib/baf/cli.rb, line 86
def option *args, &block
  args = [*args, block] if block_given?
  registrant.option *args
end
parse_arguments!() click to toggle source
# File lib/baf/cli.rb, line 91
def parse_arguments!
  parser.parse! arguments
rescue OptionParser::InvalidOption
  fail ArgumentError, parser
end
run() click to toggle source
# File lib/baf/cli.rb, line 101
def run
end
setup() click to toggle source
# File lib/baf/cli.rb, line 61
def setup
end
usage!() click to toggle source
# File lib/baf/cli.rb, line 97
def usage!
  fail ArgumentError, parser
end