Class: Pablo::Parser

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/pablo/parser.rb

Overview

Superclass for Command, Option, Token etc. Handles common initialization tasks and provides helpers.

Direct Known Subclasses

Command, Option, Token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pablo, *args, &block) ⇒ Parser

Returns a new instance of Parser.



35
36
37
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
69
70
# File 'lib/pablo/parser.rb', line 35

def initialize pablo, *args, &block
    # set names, opts
    @pablo, @toplevel, @block = pablo, pablo.toplevel?, block
    @opts = Hash.new
    @opts.merge!(args.delete_at(-1).to_hash) if args[-1].respond_to?(:to_hash)
    check_options()

    @file_methods =
        %w{file? directory? exists? exist? executable? readable? writable?}.
        collect(&:to_sym)
    @verify, args = args.partition { |x|
        [String, Array, Regexp, Proc, Fixnum, Range, Proc].include? x.class or @file_methods.include? x
    }

    [:position, :matches, :is].each do |sym|
        @verify << @opts.delete(sym) unless @opts[sym].nil?
    end

    @names = args
    check_names()

    # build expansion table
    names.each do |name|
        name = format(name)
        @pablo.expand << name unless @pablo.expand.include? name
    end if @pablo.expands?(klass_to_sym())

    # get desc, longdesc, usage
    @desc, @longdesc, @usage = @opts[:desc], @opts[:longdesc], @opts[:usage]
    %w{desc longdesc usage}.each { |sym| @opts.delete(sym.to_sym) }

    # register
    @pablo.registered << self
    # set default
    @names.each { |n| @pablo.send(klass_to_sym())[n] = @opts[:default] } unless @opts[:default].nil?
end

Instance Attribute Details

#descObject

Returns the value of attribute desc.



33
34
35
# File 'lib/pablo/parser.rb', line 33

def desc
  @desc
end

#last_matchObject

Returns the value of attribute last_match.



33
34
35
# File 'lib/pablo/parser.rb', line 33

def last_match
  @last_match
end

#longdescObject

Returns the value of attribute longdesc.



33
34
35
# File 'lib/pablo/parser.rb', line 33

def longdesc
  @longdesc
end

#namesObject

Returns the value of attribute names.



33
34
35
# File 'lib/pablo/parser.rb', line 33

def names
  @names
end

#usageObject

Returns the value of attribute usage.



33
34
35
# File 'lib/pablo/parser.rb', line 33

def usage
  @usage
end

Instance Method Details

#klass_to_symObject



72
73
74
75
76
77
78
# File 'lib/pablo/parser.rb', line 72

def klass_to_sym
    case self
    when Pablo::Command then :commands
    when Pablo::Option then :options
    when Pablo::Token then :tokens
    end
end