Class: CoolOptions
- Inherits:
-
Object
- Object
- CoolOptions
- Defined in:
- lib/cooloptions.rb
Overview
For a high-level overview of using CoolOptions, see README.txt.
Usage
:include:samples/literate.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
:nodoc:
'1.1.1'
- NO_DEFAULT =
:nodoc:
Object.new
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
-
.out ⇒ Object
:nodoc:.
-
.out=(out) ⇒ Object
:nodoc:.
-
.parse!(banner = "[options]", argv = ARGV) ⇒ Object
Takes an optional banner and the arguments you want to parse (defaults to ARGV) and yields a new CoolOptions to the supplied block.
Instance Method Summary collapse
-
#after(&after) ⇒ Object
CoolOptions only handles options parsing, and it only does rudimentary option validation.
-
#desc(string) ⇒ Object
Adds additional descriptive text to the help text.
-
#error(message) ⇒ Object
If you want to throw an option parsing error, just call #error with a message and CoolOptions will bail out and display the help message.
-
#help(error = false) ⇒ Object
:nodoc:.
-
#initialize(banner) ⇒ CoolOptions
constructor
:nodoc:.
-
#on(long, description, default = NO_DEFAULT) ⇒ Object
Called on cooloptions within the #parse! block to add options to parse on.
-
#out ⇒ Object
:nodoc:.
-
#parse!(argv) ⇒ Object
:nodoc:.
Constructor Details
#initialize(banner) ⇒ CoolOptions
:nodoc:
47 48 49 50 51 52 53 54 55 |
# File 'lib/cooloptions.rb', line 47 def initialize() #:nodoc: @parser = OptionParser.new @parser. = "Usage: #{File.basename($0)} #{}" @required = [] @result = {} @after = nil @used_shorts = {} end |
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
45 46 47 |
# File 'lib/cooloptions.rb', line 45 def parser @parser end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
45 46 47 |
# File 'lib/cooloptions.rb', line 45 def result @result end |
Class Method Details
.out ⇒ Object
:nodoc:
33 34 35 |
# File 'lib/cooloptions.rb', line 33 def self.out #:nodoc: @out || STDOUT end |
.out=(out) ⇒ Object
:nodoc:
41 42 43 |
# File 'lib/cooloptions.rb', line 41 def self.out=(out) #:nodoc: @out = out end |
.parse!(banner = "[options]", argv = ARGV) ⇒ Object
Takes an optional banner and the arguments you want to parse (defaults to ARGV) and yields a new CoolOptions to the supplied block. You can then declare your options in the block using the #on method, and do post- processing using #after. When processing is done, an OpenStruct containing the parsed options is returned.
24 25 26 27 28 29 30 31 |
# File 'lib/cooloptions.rb', line 24 def self.parse!(="[options]", argv=ARGV) #:yields: cooloptions o = new() yield o o.parse!(argv) rescue Error => e out.puts e. o.help true end |
Instance Method Details
#after(&after) ⇒ Object
CoolOptions only handles options parsing, and it only does rudimentary option validation. If you want to do more, #after is a convenient place do it, especially since the right thing will just happen if you call #error.
123 124 125 |
# File 'lib/cooloptions.rb', line 123 def after(&after) @after = after end |
#desc(string) ⇒ Object
Adds additional descriptive text to the help text.
58 59 60 61 62 |
# File 'lib/cooloptions.rb', line 58 def desc(string) string.each_line do |s| @parser.separator s.chomp end end |
#error(message) ⇒ Object
If you want to throw an option parsing error, just call #error with a message and CoolOptions will bail out and display the help message.
116 117 118 |
# File 'lib/cooloptions.rb', line 116 def error() raise Error, , caller end |
#help(error = false) ⇒ Object
:nodoc:
127 128 129 130 |
# File 'lib/cooloptions.rb', line 127 def help(error=false) #:nodoc: out.puts @parser exit(error ? 1 : 0) end |
#on(long, description, default = NO_DEFAULT) ⇒ Object
Called on cooloptions within the #parse! block to add options to parse on. Long is the long option itself, description is, well, the description, and default is the default value for the option, if any.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/cooloptions.rb', line 69 def on(long, description, default=NO_DEFAULT) if /^(.)\)(.+)$/ =~ long short, long = $1, $2 elsif /^(.*)\((.)\)(.*)$/ =~ long short = $2 long = $1 + $2 + $3 end short = long[0,1] unless short key = long.split(/ /).first.gsub('-', '_').to_sym unless long =~ / / long = "[no-]#{long}" end args = [] args << "-#{short}" unless @used_shorts[short] @used_shorts[short] = true args.concat(["--#{long}", description]) if default == NO_DEFAULT @required << key else @result[key] = default args << "Default is: #{default}" end @parser.on(*args){|e| self.result[key] = e} end |
#out ⇒ Object
:nodoc:
37 38 39 |
# File 'lib/cooloptions.rb', line 37 def out #:nodoc: self.class.out end |
#parse!(argv) ⇒ Object
:nodoc:
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cooloptions.rb', line 98 def parse!(argv) #:nodoc: @parser.on('-h', '--help', "This help info."){help} begin @parser.parse!(argv) rescue OptionParser::ParseError => e error e..capitalize end @required.reject!{|e| @result.key?(e)} error "Missing required options: #{@required.join(', ')}" unless @required.empty? r = OpenStruct.new(@result) @after.call(r) if @after r end |