Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/github_downloader/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Parser

Returns a new instance of Parser.

Yields:

  • (_self)

Yield Parameters:

  • _self (Parser)

    the object that the method was called on



4
5
6
7
8
9
# File 'lib/github_downloader/parser.rb', line 4

def initialize
  @options = []
  @used_short = []
  @default_values = nil
  yield self if block_given?
end

Instance Attribute Details

Returns the value of attribute banner.



2
3
4
# File 'lib/github_downloader/parser.rb', line 2

def banner
  @banner
end

#versionObject

Returns the value of attribute version.



2
3
4
# File 'lib/github_downloader/parser.rb', line 2

def version
  @version
end

Instance Method Details

#option(name, desc, settings = {}) ⇒ Object



11
12
13
# File 'lib/github_downloader/parser.rb', line 11

def option(name, desc, settings = {})
  @options << [name, desc, settings]
end

#process!(arguments = ARGV) ⇒ Object



38
39
40
41
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
# File 'lib/github_downloader/parser.rb', line 38

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[2][:short] || short_from(o[0])
      @result[o[0]] = o[2][:default] || false # set default
      klass = o[2][:default].class == Fixnum ? Integer : o[2][:default].class

      if [TrueClass, FalseClass, NilClass].include?(klass) # boolean switch
        p.on("-" << short, "--[no-]" << o[0].to_s.gsub("_", "-"), o[1]) { |x| @result[o[0]] = x }
      else # argument with parameter
        p.on("-" << short, "--" << o[0].to_s.gsub("_", "-") << " " << o[2][:default].to_s, klass, o[1]) { |x| @result[o[0]] = x }
      end
    end

    p.banner = @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?
    @default_values = @result.clone # save default values to reset @result in subsequent calls
  end

  begin
    @optionparser.parse!(arguments)
  rescue OptionParser::ParseError => e
    puts e.message; exit(1)
  end

  validate(@result) if self.respond_to?("validate")
  @result
end

#short_from(name) ⇒ Object



15
16
17
18
19
20
# File 'lib/github_downloader/parser.rb', line 15

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
end

#validate(options) ⇒ Object

remove this method if you want fewer lines of code and don’t need validations



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github_downloader/parser.rb', line 22

def validate(options) # remove this method if you want fewer lines of code and don't need validations
  options.each_pair do |key, value|
    opt = @options.find_all { |o| o[0] == key }.first
    key = "--" << key.to_s.gsub("_", "-")
    unless opt[2][:value_in_set].nil? || opt[2][:value_in_set].include?(value)
      puts "Parameter for #{key} must be in [" << opt[2][:value_in_set].join(", ") << "]"; exit(1)
    end
    unless opt[2][:value_matches].nil? || opt[2][:value_matches] =~ value
      puts "Parameter for #{key} must match /" << opt[2][:value_matches].source << "/"; exit(1)
    end
    unless opt[2][:value_satisfies].nil? || opt[2][:value_satisfies].call(value)
      puts "Parameter for #{key} must satisfy given conditions (see description)"; exit(1)
    end
  end
end