Module: Lucy::Goosey

Defined in:
lib/lucy-goosey.rb,
lib/lucy-goosey/version.rb

Constant Summary collapse

UNIX_SINGLE_FLAG =
/^-/
UNIX_DOUBLE_FLAG =
/^--/
EQUAL =
/=/
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.leading_word?(word) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/lucy-goosey.rb', line 10

def self.leading_word?(word)
  return unless word
  ! (word.match(UNIX_DOUBLE_FLAG) || word.match(UNIX_SINGLE_FLAG) || word.match(EQUAL))
end

.parse_options(_args) ⇒ Object

Raises:

  • (ArgumentError)


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
42
43
44
45
46
# File 'lib/lucy-goosey.rb', line 16

def self.parse_options(_args)
  args = _args.dup
  config = {}

  raise ArgumentError, 'must be an array' unless _args.is_a? Array
  return config if args.empty?

  args = args[1..-1] while leading_word?(args.first)

  args.size.times do
    break if args.empty?
    arg  = args.shift
    peek = args.first
    key  = arg
    if key.match(/=/)
      key, value = key.split('=', 2)
    elsif peek && peek.match(/=/)
      config[key.sub(UNIX_DOUBLE_FLAG, '')] = true
      key, value = peek.split('=', 2)
    elsif peek.nil? || peek.match(UNIX_DOUBLE_FLAG) || peek.match(UNIX_SINGLE_FLAG)
      value = true
    else
      value = args.shift
    end
    value = true if value == 'true'
    key = key.sub(UNIX_DOUBLE_FLAG, '').sub(UNIX_SINGLE_FLAG,'')
    config[key] = value
  end

  config
end