Class: Thor::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/thor/lib/thor/options.rb

Defined Under Namespace

Classes: Error, Hash

Constant Summary collapse

NUMERIC =
/(\d*\.\d+|\d+)/
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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(switches) ⇒ Options

Takes an array of switches. Each array consists of up to three elements that indicate the name and type of switch. Returns a hash containing each switch name, minus the ‘-’, as a key. The value for each key depends on the type of switch and/or the value provided by the user.

The long switch must be provided. The short switch defaults to the first letter of the short switch. The default type is :boolean.

Example:

opts = Thor::Options.new(
   "--debug" => true,
   ["--verbose", "-v"] => true,
   ["--level", "-l"] => :numeric
).parse(args)


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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/thor/lib/thor/options.rb', line 72

def initialize(switches)
  @defaults = {}
  @shorts = {}
  
  @leading_non_opts, @trailing_non_opts = [], []

  @switches = switches.inject({}) do |mem, (name, type)|
    if name.is_a?(Array)
      name, *shorts = name
    else
      name = name.to_s
      shorts = []
    end
    # we need both nice and dasherized form of switch name
    if name.index('-') == 0
      nice_name = undasherize name
    else
      nice_name = name
      name = dasherize name
    end
    # if there are no shortcuts specified, generate one using the first character
    shorts << "-" + nice_name[0,1] if shorts.empty? and nice_name.length > 1
    shorts.each { |short| @shorts[short] = name }
    
    # normalize type
    case type
    when TrueClass
      @defaults[nice_name] = true
      type = :boolean
    when FalseClass
      @defaults[nice_name] = false
      type = :boolean
    when String
      @defaults[nice_name] = type
      type = :optional
    when Numeric
      @defaults[nice_name] = type
      type = :numeric
    end
    
    mem[name] = type
    mem
  end
  
  # remove shortcuts that happen to coincide with any of the main switches
  @shorts.keys.each do |short|
    @shorts.delete(short) if @switches.key?(short)
  end
end

Instance Attribute Details

#leading_non_optsObject (readonly)

Returns the value of attribute leading_non_opts.



49
50
51
# File 'lib/thor/lib/thor/options.rb', line 49

def leading_non_opts
  @leading_non_opts
end

#trailing_non_optsObject (readonly)

Returns the value of attribute trailing_non_opts.



49
50
51
# File 'lib/thor/lib/thor/options.rb', line 49

def trailing_non_opts
  @trailing_non_opts
end

Instance Method Details

#formatted_usageObject Also known as: to_s



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/thor/lib/thor/options.rb', line 178

def formatted_usage
  return "" if @switches.empty?
  @switches.map do |opt, type|
    case type
    when :boolean
      "[#{opt}]"
    when :required
      opt + "=" + opt.gsub(/\-/, "").upcase
    else
      sample = @defaults[undasherize(opt)]
      sample ||= case type
        when :optional then undasherize(opt).gsub(/\-/, "_").upcase
        when :numeric  then "N"
        end
      "[" + opt + "=" + sample.to_s + "]"
    end
  end.join(" ")
end

#non_optsObject



51
52
53
# File 'lib/thor/lib/thor/options.rb', line 51

def non_opts
  leading_non_opts + trailing_non_opts
end

#parse(args, skip_leading_non_opts = true) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/thor/lib/thor/options.rb', line 122

def parse(args, skip_leading_non_opts = true)
  @args = args
  # start with Thor::Options::Hash pre-filled with defaults
  hash = Hash.new @defaults
  
  @leading_non_opts = []
  if skip_leading_non_opts
    @leading_non_opts << shift until current_is_option? || @args.empty?
  end

  while current_is_option?
    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)
    nice_name = undasherize(switch)
    type      = switch_type(switch)
    
    case type
    when :required
      assert_value!(switch)
      raise Error, "cannot pass switch '#{peek}' as an argument" if valid?(peek)
      hash[nice_name] = shift
    when :optional
      hash[nice_name] = peek.nil? || valid?(peek) || shift
    when :boolean
      if !@switches.key?(switch) && nice_name =~ /^no-(\w+)$/
        hash[$1] = false
      else
        hash[nice_name] = true
      end
      
    when :numeric
      assert_value!(switch)
      unless peek =~ NUMERIC and $& == peek
        raise Error, "expected numeric value for '#{switch}'; got #{peek.inspect}"
      end
      hash[nice_name] = $&.index('.') ? shift.to_f : shift.to_i
    end
  end
  
  @trailing_non_opts = @args

  check_required! hash
  hash.freeze
  hash
end