Module: CommandKit::Options::Parser
- Extended by:
- ModuleMethods
- Included in:
- CommandKit::Options
- Defined in:
- lib/command_kit/options/parser.rb
Overview
Adds an OptionParser to the command class and automatically parses options
before calling main
.
include CommandKit::OptParser
def initialize
@opts.on('-c','--custom','Custom option') do
@custom = true
end
end
def main(*argv)
if @custom
puts "Custom mode enabled"
end
end
Defined Under Namespace
Modules: ModuleMethods
Constant Summary
Constants included from Printing
Instance Attribute Summary collapse
-
#option_parser ⇒ OptionParser
readonly
The option parser.
Attributes included from CommandName
Instance Method Summary collapse
- #help ⇒ Object
-
#help_options ⇒ Object
Prints the
--help
output. -
#initialize(**kwargs) ⇒ OptionParser
The option parser.
-
#main(argv = []) ⇒ Integer
Parses the options and passes any additional non-option arguments to the superclass'es
#main
method. -
#on_ambiguous_argument(error) ⇒ Object
Place-holder method for handling
OptionParser::AmbiguousArgument
exceptions. -
#on_ambiguous_option(error) ⇒ Object
Place-holder method for handling
OptionParser::AmbiguousOption
exceptions. -
#on_invalid_argument(error) ⇒ Object
Place-holder method for handling
OptionParser::InvalidArgument
exceptions. -
#on_invalid_option(error) ⇒ Object
Place-holder method for handling
OptionParser::InvalidOption
exceptions. -
#on_missing_argument(error) ⇒ Object
Place-holder method for handling
OptionParser::MissingArgument
exceptions. -
#on_needless_argument(error) ⇒ Object
Place-holder method for handling
OptionParser::NeedlessArgument
exceptions. -
#on_parse_error(error) ⇒ Object
Prints an option parsing error.
-
#parse_options(argv) ⇒ Array<String>
Parses the given options.
Methods included from ModuleMethods
Methods included from Printing
#print_error, #print_exception
Methods included from Stdio
#abort, #gets, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout
Methods included from Main
Methods included from Main::ModuleMethods
Methods included from Usage
Methods included from Usage::ModuleMethods
Methods included from Help::ModuleMethods
Methods included from CommandName::ModuleMethods
Instance Attribute Details
#option_parser ⇒ OptionParser (readonly)
The option parser.
66 67 68 |
# File 'lib/command_kit/options/parser.rb', line 66 def option_parser @option_parser end |
Instance Method Details
#help ⇒ Object
251 252 253 |
# File 'lib/command_kit/options/parser.rb', line 251 def help end |
#help_options ⇒ Object
Prints the --help
output.
242 243 244 |
# File 'lib/command_kit/options/parser.rb', line 242 def puts option_parser end |
#initialize(**kwargs) ⇒ OptionParser
The option parser.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/command_kit/options/parser.rb', line 75 def initialize(**kwargs) super(**kwargs) @option_parser = OptionParser.new do |opts| opts. = "Usage: #{usage}" opts.separator '' opts.separator 'Options:' opts.on_tail('-h','--help','Print help information') do help exit(0) end end end |
#main(argv = []) ⇒ Integer
Parses the options and passes any additional non-option arguments
to the superclass'es #main
method.
103 104 105 106 107 |
# File 'lib/command_kit/options/parser.rb', line 103 def main(argv=[]) super((argv)) rescue SystemExit => system_exit system_exit.status end |
#on_ambiguous_argument(error) ⇒ Object
Place-holder method for handling OptionParser::AmbiguousArgument
exceptions.
233 234 235 |
# File 'lib/command_kit/options/parser.rb', line 233 def on_ambiguous_argument(error) on_parse_error(error) end |
#on_ambiguous_option(error) ⇒ Object
Place-holder method for handling OptionParser::AmbiguousOption
exceptions.
177 178 179 |
# File 'lib/command_kit/options/parser.rb', line 177 def on_ambiguous_option(error) on_parse_error(error) end |
#on_invalid_argument(error) ⇒ Object
Place-holder method for handling OptionParser::InvalidArgument
exceptions.
191 192 193 |
# File 'lib/command_kit/options/parser.rb', line 191 def on_invalid_argument(error) on_parse_error(error) end |
#on_invalid_option(error) ⇒ Object
Place-holder method for handling OptionParser::InvalidOption
exceptions.
163 164 165 |
# File 'lib/command_kit/options/parser.rb', line 163 def on_invalid_option(error) on_parse_error(error) end |
#on_missing_argument(error) ⇒ Object
Place-holder method for handling OptionParser::MissingArgument
exceptions.
205 206 207 |
# File 'lib/command_kit/options/parser.rb', line 205 def on_missing_argument(error) on_parse_error(error) end |
#on_needless_argument(error) ⇒ Object
Place-holder method for handling OptionParser::NeedlessArgument
exceptions.
219 220 221 |
# File 'lib/command_kit/options/parser.rb', line 219 def on_needless_argument(error) on_parse_error(error) end |
#on_parse_error(error) ⇒ Object
Prints an option parsing error.
148 149 150 151 152 |
# File 'lib/command_kit/options/parser.rb', line 148 def on_parse_error(error) print_error("#{command_name}: #{error.}") print_error("Try '#{command_name} --help' for more information.") exit(1) end |
#parse_options(argv) ⇒ Array<String>
Parses the given options.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/command_kit/options/parser.rb', line 120 def (argv) begin option_parser.parse(argv) rescue OptionParser::InvalidOption => error on_invalid_option(error) rescue OptionParser::AmbiguousOption => error on_ambiguous_option(error) rescue OptionParser::InvalidArgument => error on_invalid_argument(error) rescue OptionParser::MissingArgument => error on_missing_argument(error) rescue OptionParser::NeedlessArgument => error on_needless_argument(error) rescue OptionParser::AmbiguousArgument => error on_ambiguous_argument(error) rescue OptionParser::ParseError => error on_parse_error(error) end end |