Module: OptionParsing

Included in:
PunchClock
Defined in:
lib/option_parsing.rb

Instance Method Summary collapse

Instance Method Details

#argsObject


37
38
39
# File 'lib/option_parsing.rb', line 37

def args
  @args
end

#args=(args) ⇒ Object


27
28
29
30
31
32
33
34
35
# File 'lib/option_parsing.rb', line 27

def args=(args)
  @args = args.flat_map do |a|
    if a =~ /^-\w{2,}$/
      a[1..-1].split("").map { |c| c.prepend('-') }
    else
      a
    end
  end
end

#flag(*names) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/option_parsing.rb', line 11

def flag(*names)
  names.each do |n|
    next unless (index = args.index(n))
    argument = args[index + 1]
    # Only yield the argument when it is not another option. Note that it
    # can be nil, so flag arguments are optional by default.
    next if argument.to_s.start_with?('-')
    yield argument
    # Delete flag.
    args.delete_at index
    # Delete flag argument.
    args.delete_at index
    break
  end
end

#switch(*names) ⇒ Object


2
3
4
5
6
7
8
9
# File 'lib/option_parsing.rb', line 2

def switch(*names)
  names.each do |n|
    if args.delete(n)
      yield
      break
    end
  end
end