Class: Hammer::Parser

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

Overview

Parses ARGV into positional args and an options hash, given a set of ‘Option` definitions.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
# File 'lib/hammer/parser.rb', line 7

def initialize(options)
  @options    = options
  @by_switch  = {}
  options.each do |opt|
    register_switch(opt.switch, opt)
    register_switch(opt.negation, opt) if opt.boolean?
    opt.aliases.each { |a| register_switch(a, opt) }
  end
end

Instance Method Details

#parse(argv) ⇒ Object



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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hammer/parser.rb', line 17

def parse(argv)
  positional = []
  values     = {}
  i = 0
  argv = argv.dup

  while i < argv.length
    token = argv[i]

    if token == '--'
      positional.concat(argv[(i + 1)..])
      break
    end

    if token.start_with?('--') && token.include?('=')
      key, val = token.split('=', 2)
      opt = lookup!(key)
      if opt.boolean? && key.start_with?('--no-')
        values[opt.name] = !opt.cast(val)   # `--no-x=false` -> true
      else
        values[opt.name] = opt.cast(val)
      end
      i += 1
      next
    end

    if @by_switch.key?(token)
      opt = @by_switch[token]
      if opt.boolean?
        values[opt.name] = !token.start_with?('--no-')
        i += 1
      else
        val = argv[i + 1]
        raise Error, "missing value for #{token}" if val.nil?
        values[opt.name] = opt.cast(val)
        i += 2
      end
      next
    end

    # Glued short flag with value: `-pVALUE` (non-boolean short opts only).
    if token.start_with?('-') && !token.start_with?('--') && token.length > 2
      if (opt = @by_switch[token[0, 2]]) && !opt.boolean?
        values[opt.name] = opt.cast(token[2..])
        i += 1
        next
      end
    end

    # Dash-led and not a negative number -> a genuinely unknown flag.
    # Bare `-` and negative numbers (`-5`) fall through to positionals.
    if token.start_with?('-') && token.length > 1 && token !~ /\A-\d/
      raise Error, "unknown option: #{token}"
    end

    positional << token
    i += 1
  end

  # Fill un-set non-boolean opts from positional args in declaration
  # order. Booleans always need an explicit flag. Scalars take one
  # positional each; an :array opt slurps whatever's left, so it's
  # filled last and never starves a later-declared scalar opt.
  array_opt = nil
  @options.each do |opt|
    next if opt.boolean? || values.key?(opt.name)
    if opt.type == :array
      array_opt = opt
      next
    end
    break if positional.empty?
    values[opt.name] = opt.cast(positional.shift)
  end
  if array_opt && !positional.empty?
    values[array_opt.name] = array_opt.cast(positional.shift(positional.size))
  end

  @options.each do |opt|
    values[opt.name] = opt.default if !values.key?(opt.name) && !opt.default.nil?
    raise Error, "missing required --#{opt.name}" if opt.required && !values.key?(opt.name)
  end

  [positional, values]
end