lib/command_kit/options/parser.rb in command_kit-0.1.0.rc1 vs lib/command_kit/options/parser.rb in command_kit-0.1.0
- old
+ new
@@ -31,10 +31,13 @@
module Parser
include Usage
include Main
include Printing
+ #
+ # @api private
+ #
module ModuleMethods
#
# Sets {CommandKit::Usage::ClassMethods#usage .usage} or extends
# {ModuleMethods}, depending on whether {Options::Parser} is being
# included into a class or a module.
@@ -56,17 +59,21 @@
extend ModuleMethods
# The option parser.
#
# @return [OptionParser]
+ #
+ # @api semipublic
attr_reader :option_parser
#
# The option parser.
#
# @return [OptionParser]
#
+ # @api public
+ #
def initialize(**kwargs)
super(**kwargs)
@option_parser = OptionParser.new do |opts|
opts.banner = "Usage: #{usage}"
@@ -89,10 +96,12 @@
# The given arguments Array.
#
# @return [Integer]
# The exit status code.
#
+ # @api public
+ #
def main(argv=[])
super(parse_options(argv))
rescue SystemExit => system_exit
system_exit.status
end
@@ -104,10 +113,12 @@
# The given command-line arguments.
#
# @return [Array<String>]
# The remaining non-option arguments.
#
+ # @api semipublic
+ #
def parse_options(argv)
begin
option_parser.parse(argv)
rescue OptionParser::InvalidOption => error
on_invalid_option(error)
@@ -130,10 +141,12 @@
# Prints an option parsing error.
#
# @param [OptionParser::ParseError] error
# The error from `OptionParser`.
#
+ # @api semipublic
+ #
def on_parse_error(error)
print_error("#{command_name}: #{error.message}")
print_error("Try '#{command_name} --help' for more information.")
exit(1)
end
@@ -143,10 +156,12 @@
#
# @param [OptionParser::InvalidOption] error
#
# @see on_parse_error
#
+ # @api semipublic
+ #
def on_invalid_option(error)
on_parse_error(error)
end
#
@@ -155,10 +170,12 @@
#
# @param [OptionParser::AmbiguousOption] error
#
# @see on_parse_error
#
+ # @api semipublic
+ #
def on_ambiguous_option(error)
on_parse_error(error)
end
#
@@ -167,10 +184,12 @@
#
# @param [OptionParser::InvalidArgument] error
#
# @see on_parse_error
#
+ # @api semipublic
+ #
def on_invalid_argument(error)
on_parse_error(error)
end
#
@@ -179,10 +198,12 @@
#
# @param [OptionParser::MissingArgument] error
#
# @see on_parse_error
#
+ # @api semipublic
+ #
def on_missing_argument(error)
on_parse_error(error)
end
#
@@ -191,10 +212,12 @@
#
# @param [OptionParser::NeedlessArgument] error
#
# @see on_parse_error
#
+ # @api semipublic
+ #
def on_needless_argument(error)
on_parse_error(error)
end
#
@@ -203,22 +226,28 @@
#
# @param [OptionParser::AmbiguousArgument] error
#
# @see on_parse_error
#
+ # @api semipublic
+ #
def on_ambiguous_argument(error)
on_parse_error(error)
end
#
# Prints the `--help` output.
#
+ # @api semipublic
+ #
def help_options
puts option_parser
end
#
# @see #help_options
+ #
+ # @api public
#
def help
help_options
end
end