Class: OptionsParser
- Inherits:
-
Object
- Object
- OptionsParser
- Defined in:
- lib/options_parser.rb
Overview
You can extend this class to use a wrapper around the default Ruby OptionParser. If you see this, feel free to copy it to your own gem. Copyright 2009, Iain Hecker. Released under the MIT License.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#stdout ⇒ Object
Returns the value of attribute stdout.
Class Method Summary collapse
-
.after(&code) ⇒ Object
Define what needs to be done after parsing the options.
-
.before(&code) ⇒ Object
Define what needs to be done before parsing the options.
-
.execute(stdout, arguments = []) ⇒ Object
Call the excute method from your bin file:.
- .options ⇒ Object
-
.parse(*args, &code) ⇒ Object
Define your application specific command line options.
- .parse_options ⇒ Object
Instance Method Summary collapse
-
#banner ⇒ Object
Override to alter the banner.
-
#initialize(stdout, arguments) ⇒ OptionsParser
constructor
A new instance of OptionsParser.
- #options ⇒ Object
-
#parse ⇒ Object
Does the main work or parsing the options.
-
#separator ⇒ Object
Override to alter the separator.
Constructor Details
#initialize(stdout, arguments) ⇒ OptionsParser
Returns a new instance of OptionsParser.
22 23 24 |
# File 'lib/options_parser.rb', line 22 def initialize(stdout, arguments) @stdout, @arguments = stdout, arguments end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
8 9 10 |
# File 'lib/options_parser.rb', line 8 def arguments @arguments end |
#stdout ⇒ Object
Returns the value of attribute stdout.
8 9 10 |
# File 'lib/options_parser.rb', line 8 def stdout @stdout end |
Class Method Details
.after(&code) ⇒ Object
Define what needs to be done after parsing the options. Similar to RSpec’s after.
48 49 50 |
# File 'lib/options_parser.rb', line 48 def self.after(&code) @after = code end |
.before(&code) ⇒ Object
Define what needs to be done before parsing the options. Similar to RSpec’s before.
54 55 56 |
# File 'lib/options_parser.rb', line 54 def self.before(&code) @before = code end |
.execute(stdout, arguments = []) ⇒ Object
Call the excute method from your bin file:
#!/usr/bin/env ruby
require File.dirname(__FILE__) + "/../lib/your_gem/cli")
YourGem::CLI.execute(STDOUT, ARGV)
15 16 17 18 19 20 |
# File 'lib/options_parser.rb', line 15 def self.execute(stdout, arguments = []) cli = new(stdout, arguments) cli.instance_eval(&@before) if @before cli.parse cli.instance_eval(&@after) if @after end |
.options ⇒ Object
58 59 60 |
# File 'lib/options_parser.rb', line 58 def self. @options ||= {} end |
.parse(*args, &code) ⇒ Object
Define your application specific command line options. This gets passed to OptionParser. The first argument is the method to call, the rest is you world expect
Example:
class CLI < OptionsParser
parse(:on, "-v", "Show version") { stdout.puts YourGem::Version }
38 39 40 |
# File 'lib/options_parser.rb', line 38 def self.parse(*args, &code) << [ args, code ] unless args.empty? end |
.parse_options ⇒ Object
42 43 44 |
# File 'lib/options_parser.rb', line 42 def self. @parse_options ||= [] end |
Instance Method Details
#banner ⇒ Object
Override to alter the banner
66 |
# File 'lib/options_parser.rb', line 66 def ; "" end |
#options ⇒ Object
26 27 28 |
# File 'lib/options_parser.rb', line 26 def (@options || {}).merge(self.class.) end |
#parse ⇒ Object
Does the main work or parsing the options
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/options_parser.rb', line 69 def parse OptionParser.new do |opts| opts. = opts.separator separator (opts) opts.separator separator opts.on "--readme", "Show the Readme", &readme opts.on "-v", "--version", "Show version", &version opts.on "-h", "--help", "Show this help message", &help(opts) opts.parse! arguments end end |
#separator ⇒ Object
Override to alter the separator
63 |
# File 'lib/options_parser.rb', line 63 def separator; "" end |