Class: Thor::Options

Inherits:
Arguments show all
Defined in:
lib/thor/parser/options.rb

Overview

This is a modified version of Daniel Berger’s Getopt::Long class, licensed under Ruby’s license.

Constant Summary collapse

LONG_RE =
/^(--\w+[-\w+]*)$/
SHORT_RE =
/^(-[a-z])$/i
EQ_RE =
/^(--\w+[-\w+]*|-[a-z])=(.*)$/i
SHORT_SQ_RE =

Allow either -x -v or -xv style for single char args

/^-([a-z]{2,})$/i
SHORT_NUM =
/^(-[a-z])#{NUMERIC}$/i

Constants inherited from Arguments

Arguments::NUMERIC

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Arguments

parse, split

Constructor Details

#initialize(options = {}) ⇒ Options

Takes a hash of Thor::Option objects.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/thor/parser/options.rb', line 33

def initialize(options={})
  options = options.values
  super(options)
  @shorts, @switches = {}, {}

  options.each do |option|
    @switches[option.switch_name] = option

    option.aliases.each do |short|
      @shorts[short.to_s] ||= option.switch_name
    end
  end
end

Class Method Details

.to_switches(options) ⇒ Object

Receives a hash and makes it switches.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/thor/parser/options.rb', line 14

def self.to_switches(options)
  options.map do |key, value|
    case value
      when true
        "--#{key}"
      when Array
        "--#{key} #{value.map{ |v| v.inspect }.join(' ')}"
      when Hash
        "--#{key} #{value.map{ |k,v| "#{k}:#{v}" }.join(' ')}"
      when nil, false
        ""
      else
        "--#{key} #{value.inspect}"
    end
  end.join(" ")
end

Instance Method Details

#parse(args) ⇒ Object



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
# File 'lib/thor/parser/options.rb', line 47

def parse(args)
  @pile = args.dup

  while peek
    if current_is_switch?
      case shift
        when SHORT_SQ_RE
          unshift($1.split('').map { |f| "-#{f}" })
          next
        when EQ_RE, SHORT_NUM
          unshift($2)
          switch = $1
        when LONG_RE, SHORT_RE
          switch = $1
      end

      switch = normalize_switch(switch)
      next unless option = switch_option(switch)

      @assigns[option.human_name] = parse_peek(switch, option)
    else
      shift
    end
  end

  check_requirement!
  @assigns
end