Class: HelpParser::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/help_parser/options.rb

Instance Method Summary collapse

Constructor Details

#initialize(version, help, argv) ⇒ Options

Returns a new instance of Options.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/help_parser/options.rb', line 3

def initialize(version, help, argv)
  @hash = HelpParser.parsea(argv)
  if version && VSN.any?{@hash.key? _1}
    # -v or --version
    raise VersionException, String(version)
  end
  if help
    help = String(help)
    if HLP.any?{@hash.key? _1}
      HelpParser.parseh(help, validate: true) if HLP.all?{@hash.key? _1}
      raise HelpException, help
    end
    specs = HelpParser.parseh(help)
    Completion.new(@hash, specs)
    if (exclusive=specs[EXCLUSIVE])
      exclusive.each do |x|
        count = x.count{@hash.key? _1}
        raise HelpParser::UsageError, MSG[EXCLUSIVE_KEYS,*x] if count > 1
      end
    end
    if (inclusive=specs[INCLUSIVE])
      inclusive.each do |i|
        count = i.count{@hash.key? _1}
        unless count.zero? || count==i.length
          raise HelpParser::UsageError, MSG[INCLUSIVE_KEYS,*i]
        end
      end
    end
    if (conditional=specs[CONDITIONAL])
      conditional.each do |c|
        if @hash.key?(c[0]) && !c.all?{@hash.key? _1}
          raise HelpParser::UsageError, MSG[CONDITIONAL_KEYS,*c]
        end
      end
    end
  end
  $VERBOSE = true if @hash[VRBS]==true
  $DEBUG = true if @hash[DBG]==true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/help_parser/options.rb', line 55

def method_missing(m, *args, &block)
  super unless respond_to_missing?(m)
  unless args.empty? && block.nil?
    raise ArgumentError, 'expected neither args nor block'
  end
  m[-1]=='?' ? @hash.key?(m[0..-2].to_s) : @hash[m.to_s]
end

Instance Method Details

#[](k) ⇒ Object



47
48
49
# File 'lib/help_parser/options.rb', line 47

def [](k)
  @hash[k]
end

#respond_to_missing?(m, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/help_parser/options.rb', line 51

def respond_to_missing?(m, include_all=false)
  m[-1]=='!' ? super : true
end

#to_hObject



43
44
45
# File 'lib/help_parser/options.rb', line 43

def to_h
  @hash
end