Class: Parser
- Inherits:
-
Object
- Object
- Parser
- Defined in:
- lib/micro-optparse/parser.rb
Instance Attribute Summary collapse
-
#banner ⇒ Object
Returns the value of attribute banner.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#initialize(default_settings = {}) {|_self| ... } ⇒ Parser
constructor
A new instance of Parser.
- #option(name, desc, settings = {}) ⇒ Object
- #process!(arguments = ARGV) ⇒ Object
- #short_from(name) ⇒ Object
-
#validate(result) ⇒ Object
remove this method if you want fewer lines of code and don't need validations.
Constructor Details
#initialize(default_settings = {}) {|_self| ... } ⇒ Parser
Returns a new instance of Parser.
5 6 7 8 9 10 11 |
# File 'lib/micro-optparse/parser.rb', line 5 def initialize(default_settings = {}) @options = [] @used_short = [] @default_values = {} @default_settings = default_settings yield self if block_given? end |
Instance Attribute Details
#banner ⇒ Object
Returns the value of attribute banner.
4 5 6 |
# File 'lib/micro-optparse/parser.rb', line 4 def @banner end |
#version ⇒ Object
Returns the value of attribute version.
4 5 6 |
# File 'lib/micro-optparse/parser.rb', line 4 def version @version end |
Instance Method Details
#option(name, desc, settings = {}) ⇒ Object
13 14 15 16 |
# File 'lib/micro-optparse/parser.rb', line 13 def option(name, desc, settings = {}) settings = @default_settings.clone.merge(settings) @options << {:name => name, :description => desc, :settings => settings} end |
#process!(arguments = ARGV) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/micro-optparse/parser.rb', line 42 def process!(arguments = ARGV) @result = @default_values.clone # reset or new @optionparser ||= OptionParser.new do |p| # prepare only once @options.each do |o| @used_short << short = o[:settings][:no_short] ? nil : o[:settings][:short] || short_from(o[:name]) @result[o[:name]] = o[:settings][:default] || false unless o[:settings][:optional] # set default name = o[:name].to_s.gsub("_", "-") klass = o[:settings][:default].is_a?(Integer) ? Integer : o[:settings][:default].class args = [o[:description]] args << "-" + short if short if [TrueClass, FalseClass, NilClass].include?(klass) # boolean switch args << "--[no-]" + name else # argument with parameter, add class for typecheck args << "--" + name + " " + o[:settings][:default].to_s << klass end p.on(*args) {|x| @result[o[:name]] = x} end p. = @banner unless @banner.nil? p.on_tail("-h", "--help", "Show this message") {puts p ; exit} short = @used_short.include?("v") ? "-V" : "-v" p.on_tail(short, "--version", "Print version") {puts @version ; exit} unless @version.nil? end @default_values = @result.clone # save default values to reset @result in subsequent calls begin @optionparser.parse!(arguments) rescue OptionParser::ParseError => e puts e. ; exit(1) end validate(@result) if self.respond_to?("validate") @result end |
#short_from(name) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/micro-optparse/parser.rb', line 18 def short_from(name) name.to_s.chars.each do |c| next if @used_short.include?(c) || c == "_" return c # returns from short_from method end return name.to_s.chars.first end |
#validate(result) ⇒ Object
remove this method if you want fewer lines of code and don't need validations
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/micro-optparse/parser.rb', line 26 def validate(result) # remove this method if you want fewer lines of code and don't need validations result.each_pair do |key, value| o = @options.find_all{ |option| option[:name] == key }.first key = "--" << key.to_s.gsub("_", "-") unless o[:settings][:value_in_set].nil? || o[:settings][:value_in_set].include?(value) puts "Parameter for #{key} must be in [" << o[:settings][:value_in_set].join(", ") << "]" ; exit(1) end unless o[:settings][:value_matches].nil? || o[:settings][:value_matches] =~ value puts "Parameter for #{key} must match /" << o[:settings][:value_matches].source << "/" ; exit(1) end unless o[:settings][:value_satisfies].nil? || o[:settings][:value_satisfies].call(value) puts "Parameter for #{key} must satisfy given conditions (see description)" ; exit(1) end end end |