class CommandLineBoss


@api public
Command line interface parser based on OptionsParser

def add_error_message(message)

Other tags:
    Api: - private

Returns:
  • (void) -

Parameters:
  • message (String) -- the error message to add
def add_error_message(message)
  @error_messages << "ERROR: #{message}"
end

def define_options

Other tags:
    Api: - private

Returns:
  • (void) -
def define_options
  parser.separator 'Options:'
  private_methods.select { |m| m.to_s.match?(DEFINITION_METHOD_REGEXP) }.each { |m| send(m) }
  parser.separator ''
end

def error_messages = @error_messages.dup.freeze

Returns:
  • (Array) -
def error_messages = @error_messages.dup.freeze

def failed? = !succeeded?

Returns:
  • (Boolean) -
def failed? = !succeeded?

def initialize(program_name: $PROGRAM_NAME)

Parameters:
  • program_name (String) -- the name of the program to report in the usage line
def initialize(program_name: $PROGRAM_NAME)
  @program_name = program_name
  @parser = OptionParser.new.tap { |p| p.set_program_name(program_name) }
  @error_messages = []
  set_defaults if private_methods.include?(:set_defaults)
  define_options
end

def parse(args)

Returns:
  • (CommandLineParser) - returns self

Parameters:
  • args (Array) -- the command line arguments
def parse(args)
  @args = args.dup
  parse_options
  parse_arguments
  validate if @error_messages.empty?
  self
end

def parse!(args)

Returns:
  • (CommandLineParser) - returns self

Raises:
  • (SystemExit) - if the command line arguments are invalid

Parameters:
  • args (Array) -- the command line arguments
def parse!(args)
  parse(args)
  if failed?
    warn error_messages.join("\n")
    exit 1
  end
  self
end

def parse_arguments; end

Other tags:
    Api: - private

Returns:
  • (void) -
def parse_arguments; end

def parse_options

Other tags:
    Api: - private

Returns:
  • (void) -

Raises:
  • (SystemExit) - if the command line arguments are invalid
def parse_options
  parser.parse!(args)
rescue OptionParser::ParseError => e
  add_error_message(e.message)
end

def succeeded? = @error_messages.empty?

Returns:
  • (Boolean) -
def succeeded? = @error_messages.empty?

def validate

Other tags:
    Api: - private

Returns:
  • (void) -
def validate
  private_methods.select { |m| m.to_s.match?(VALIDATION_METHOD_REGEXP) }.each { |m| send(m) }
end

def validate_remaining_args

Other tags:
    Api: - private

Returns:
  • (void) -
def validate_remaining_args
  return if args.empty?
  add_error_message("Unexpected arguments: #{args.join(' ')}")
end