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 run(*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
246 247 248 |
# File 'lib/command_kit/options/parser.rb', line 246 def help end |
#help_options ⇒ Object
Prints the --help
output.
237 238 239 |
# File 'lib/command_kit/options/parser.rb', line 237 def puts option_parser end |
#initialize(**kwargs) ⇒ OptionParser
The option parser.
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/command_kit/options/parser.rb', line 75 def initialize(**kwargs) super(**kwargs) @option_parser = OptionParser.new do |opts| opts. = "Usage: #{usage}" 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.
100 101 102 103 104 |
# File 'lib/command_kit/options/parser.rb', line 100 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.
228 229 230 |
# File 'lib/command_kit/options/parser.rb', line 228 def on_ambiguous_argument(error) on_parse_error(error) end |
#on_ambiguous_option(error) ⇒ Object
Place-holder method for handling OptionParser::AmbiguousOption
exceptions.
172 173 174 |
# File 'lib/command_kit/options/parser.rb', line 172 def on_ambiguous_option(error) on_parse_error(error) end |
#on_invalid_argument(error) ⇒ Object
Place-holder method for handling OptionParser::InvalidArgument
exceptions.
186 187 188 |
# File 'lib/command_kit/options/parser.rb', line 186 def on_invalid_argument(error) on_parse_error(error) end |
#on_invalid_option(error) ⇒ Object
Place-holder method for handling OptionParser::InvalidOption
exceptions.
158 159 160 |
# File 'lib/command_kit/options/parser.rb', line 158 def on_invalid_option(error) on_parse_error(error) end |
#on_missing_argument(error) ⇒ Object
Place-holder method for handling OptionParser::MissingArgument
exceptions.
200 201 202 |
# File 'lib/command_kit/options/parser.rb', line 200 def on_missing_argument(error) on_parse_error(error) end |
#on_needless_argument(error) ⇒ Object
Place-holder method for handling OptionParser::NeedlessArgument
exceptions.
214 215 216 |
# File 'lib/command_kit/options/parser.rb', line 214 def on_needless_argument(error) on_parse_error(error) end |
#on_parse_error(error) ⇒ Object
Prints an option parsing error.
143 144 145 146 147 |
# File 'lib/command_kit/options/parser.rb', line 143 def on_parse_error(error) print_error(error.) stderr.puts("Try '#{command_name} --help' for more information.") exit(1) end |
#parse_options(argv) ⇒ Array<String>
Parses the given options.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/command_kit/options/parser.rb', line 117 def (argv) 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 |