Class: OptionsParser

Inherits:
Object
  • Object
show all
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

Geert::CLI

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#argumentsObject

Returns the value of attribute arguments.



8
9
10
# File 'lib/options_parser.rb', line 8

def arguments
  @arguments
end

#stdoutObject

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

.optionsObject



58
59
60
# File 'lib/options_parser.rb', line 58

def self.options
  @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)
  parse_options << [ args, code ] unless args.empty?
end

.parse_optionsObject



42
43
44
# File 'lib/options_parser.rb', line 42

def self.parse_options
  @parse_options ||= []
end

Instance Method Details

Override to alter the banner



66
# File 'lib/options_parser.rb', line 66

def banner; "" end

#optionsObject



26
27
28
# File 'lib/options_parser.rb', line 26

def options
  (@options || {}).merge(self.class.options)
end

#parseObject

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.banner = banner
    opts.separator separator
    parse_options(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

#separatorObject

Override to alter the separator



63
# File 'lib/options_parser.rb', line 63

def separator; "" end