class Abt::BaseCommand

Attributes

ari[R]
cli[R]
flags[R]

Public Class Methods

description() click to toggle source
# File lib/abt/base_command.rb, line 11
def self.description
  raise NotImplementedError, "Command classes must implement .description"
end
flags() click to toggle source
# File lib/abt/base_command.rb, line 15
def self.flags
  []
end
new(ari:, cli:) click to toggle source
# File lib/abt/base_command.rb, line 23
def initialize(ari:, cli:)
  @cli = cli
  @ari = ari
  @flags = parse_flags(ari.flags)
end
usage() click to toggle source
# File lib/abt/base_command.rb, line 7
def self.usage
  raise NotImplementedError, "Command classes must implement .usage"
end

Public Instance Methods

perform() click to toggle source
# File lib/abt/base_command.rb, line 29
def perform
  raise NotImplementedError, "Command classes must implement #perform"
end

Private Instance Methods

flag_parser() click to toggle source
# File lib/abt/base_command.rb, line 47
    def flag_parser
      @flag_parser ||= OptionParser.new do |opts|
        opts.banner = <<~TXT
          #{self.class.description.strip}

          Usage: #{self.class.usage.strip}
        TXT

        opts.on("-h", "--help", "Display this help")

        self.class.flags.each do |(*flag)|
          opts.on(*flag)
        end
      end
    end
parse_flags(flags) click to toggle source
# File lib/abt/base_command.rb, line 35
def parse_flags(flags)
  result = {}

  flag_parser.parse!(flags.dup, into: result)

  exit_with_message(flag_parser.help) if result[:help]

  result
rescue OptionParser::InvalidOption => e
  abort(e.message)
end