Class: BashVar

Inherits:
Object
  • Object
show all
Defined in:
lib/bashvar.rb,
lib/bashvar/version.rb

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.parse(input, symbolize_names: false) ⇒ Object



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
# File 'lib/bashvar.rb', line 7

def parse(input, symbolize_names: false)
  return {} if input.nil? || input.strip.empty?

  vars = {}
  input.each_line do |line|
    line = line.strip

    # Main regex for parsing declare lines
    m = line.match(/^declare\s+(-[A-Za-z\-]+)?\s+([A-Za-z_][A-Za-z0-9_]*)=(.*)$/)
    next unless m

    flags = m[1] || ''
    name  = m[2]
    raw   = m[3]

    # skip functions explicitly
    next if flags.include?('f') && !flags.match?(/-[^A-Za-z]*[ai]/) # crude but protects `declare -f`

    value = if flags.include?('a') || flags.include?('A')
              parse_array_like(raw, assoc: flags.include?('A'), symbolize_names: symbolize_names)
            elsif flags.include?('i')
              parse_integer(raw)
            else
              parse_scalar(raw)
            end
    key = symbolize_names ? name.to_sym : name
    vars[key] = value
  end
  vars
end