class CommandLineBoss
@api public
Command line interface parser based on OptionsParser
def add_error_message(message)
- 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
- 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
-
(Array
-)
def error_messages = @error_messages.dup.freeze
def failed? = !succeeded?
-
(Boolean)
-
def failed? = !succeeded?
def initialize(program_name: $PROGRAM_NAME)
-
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)
-
(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)
-
(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
- Api: - private
Returns:
-
(void)
-
def parse_arguments; end
def parse_options
- 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?
-
(Boolean)
-
def succeeded? = @error_messages.empty?
def validate
- 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
- Api: - private
Returns:
-
(void)
-
def validate_remaining_args return if args.empty? add_error_message("Unexpected arguments: #{args.join(' ')}") end