josevalim-thor 0.10.13 → 0.10.14

Sign up to get free protection for your applications and to get access to all the features.
data/lib/thor.rb CHANGED
@@ -124,7 +124,10 @@ class Thor
124
124
  opts = Thor::Options.new(options)
125
125
  opts.parse(args)
126
126
 
127
- instance = new(opts.arguments, opts.options, config) do |klass, invoke, overrides|
127
+ defaults = config[:default_options] ? config[:default_options].dup : {}
128
+ merge_with_thor_options(defaults, task.options)
129
+
130
+ instance = new(opts.arguments, defaults.merge!(opts.options), config) do |klass, invoke, overrides|
128
131
  klass.prepare(invoke, args, config.merge(overrides))
129
132
  end
130
133
 
data/lib/thor/actions.rb CHANGED
@@ -42,25 +42,9 @@ class Thor
42
42
  # as destination root.
43
43
  #
44
44
  def initialize(args=[], options={}, config={})
45
- self.behavior = case config[:behavior]
46
- when :force
47
- options.merge!(:force => true, 'force' => true)
48
- :invoke
49
- when :skip
50
- options.merge!(:skip => true, 'skip' => true)
51
- :invoke
52
- when :pretend
53
- options.merge!(:pretend => true, 'pretend' => true)
54
- :invoke
55
- when :revoke
56
- :revoke
57
- else
58
- :invoke
59
- end
60
-
61
45
  super
62
-
63
- self.root = config[:root]
46
+ self.root = config[:root]
47
+ self.behavior = config[:behavior].to_s == "revoke" ? :revoke : :invoke
64
48
  end
65
49
 
66
50
  # Wraps an action object and call it accordingly to the thor class behavior.
data/lib/thor/base.rb CHANGED
@@ -17,8 +17,8 @@ class Thor
17
17
  # It receives arguments in an Array and two hashes, one for options and
18
18
  # other for configuration.
19
19
  #
20
- # Notice that it does not check arguments type neither if all required
21
- # arguments were supplied. It should be done by the parser.
20
+ # Notice that it does not check if all required arguments were supplied.
21
+ # It should be done by the parser.
22
22
  #
23
23
  # ==== Parameters
24
24
  # args<Array[Object]>:: An array of objects. The objects are applied to their
@@ -31,10 +31,12 @@ class Thor
31
31
  # config<Hash>:: Configuration for this Thor class.
32
32
  #
33
33
  def initialize(args=[], options={}, config={})
34
+ # Set arguments and default values
34
35
  self.class.arguments.zip(args).each do |argument, value|
35
- send("#{argument.human_name}=", value)
36
+ send("#{argument.human_name}=", value || argument.default)
36
37
  end
37
38
 
39
+ self.class.merge_with_thor_options(options, self.class.class_options)
38
40
  self.options = Thor::CoreExt::HashWithIndifferentAccess.new(options).freeze
39
41
  end
40
42
 
@@ -350,6 +352,21 @@ class Thor
350
352
  config[:shell].error e.message
351
353
  end
352
354
 
355
+ # Receives a hash of options, stringify keys and merge with default
356
+ # values from thor options.
357
+ #
358
+ def merge_with_thor_options(options, thor_options)
359
+ options.each_key do |key|
360
+ next unless key.is_a?(Symbol)
361
+ options[key.to_s] = options.delete(key)
362
+ end
363
+
364
+ thor_options.each do |key, option|
365
+ next if option.argument? || option.default.nil?
366
+ options[option.human_name.to_s] ||= option.default
367
+ end
368
+ end
369
+
353
370
  protected
354
371
 
355
372
  # Prints the class optins per group. If a class options does not belong
@@ -462,6 +479,9 @@ class Thor
462
479
  create_task(meth)
463
480
  end
464
481
 
482
+ # Retrieves a value from superclass. If it reaches the baseclass,
483
+ # returns nil.
484
+ #
465
485
  def from_superclass(method, default=nil)
466
486
  if self == baseclass
467
487
  default
data/lib/thor/group.rb CHANGED
@@ -24,7 +24,10 @@ class Thor::Group
24
24
  opts = Thor::Options.new(class_options)
25
25
  opts.parse(args)
26
26
 
27
- instance = new(opts.arguments, opts.options, config) do |klass, invoke, overrides|
27
+ defaults = config[:default_options] ? config[:default_options].dup : {}
28
+ merge_with_thor_options(defaults, {})
29
+
30
+ instance = new(opts.arguments, defaults.merge!(opts.options), config) do |klass, invoke, overrides|
28
31
  klass.prepare(invoke, args, config.merge(overrides))
29
32
  end
30
33
 
data/lib/thor/options.rb CHANGED
@@ -57,12 +57,7 @@ class Thor
57
57
 
58
58
  @switches = switches.values.inject({}) do |mem, option|
59
59
  @non_assigned_required << option if option.required?
60
-
61
- if option.argument?
62
- @non_assigned_arguments << option
63
- elsif !option.default.nil?
64
- @options[option.human_name] = option.default
65
- end
60
+ @non_assigned_arguments << option if option.argument?
66
61
 
67
62
  # If there are no shortcuts specified, generate one using the first character
68
63
  shorts = option.aliases.dup
@@ -110,7 +105,6 @@ class Thor
110
105
  end
111
106
  end
112
107
 
113
- assign_arguments_default_values!
114
108
  check_validity!
115
109
  @options
116
110
  end
@@ -153,8 +147,8 @@ class Thor
153
147
  # Returns the option object for the given switch.
154
148
  #
155
149
  def switch_option(arg)
156
- if arg =~ /^--no-(\w+)$/
157
- @switches[arg] || @switches["--#{$1}"]
150
+ if arg =~ /^--(no|skip)-([-\w]+)$/
151
+ @switches[arg] || @switches["--#{$2}"]
158
152
  else
159
153
  @switches[arg]
160
154
  end
@@ -173,12 +167,16 @@ class Thor
173
167
  def parse_option(switch, option, hash)
174
168
  human_name = option.human_name
175
169
 
176
- case option.type
177
- when :default
178
- hash[human_name] = peek.nil? || peek.to_s =~ /^-/ || shift
170
+ type = if option.type == :default
171
+ peek.nil? || peek.to_s =~ /^-/ ? :boolean : :string
172
+ else
173
+ option.type
174
+ end
175
+
176
+ case type
179
177
  when :boolean
180
- if !@switches.key?(switch) && switch =~ /^--no-(\w+)$/
181
- hash[$1] = false
178
+ if !@switches.key?(switch) && switch =~ /^--(no|skip)-([-\w]+)$/
179
+ hash[$2] = false
182
180
  else
183
181
  hash[human_name] = true
184
182
  end
@@ -265,14 +263,6 @@ class Thor
265
263
  end
266
264
  end
267
265
 
268
- # Assign default values to the argument hash.
269
- #
270
- def assign_arguments_default_values!
271
- @non_assigned_arguments.each do |option|
272
- @arguments << option.default
273
- end
274
- end
275
-
276
266
  # Remove shortcuts that happen to coincide with any of the main switches
277
267
  #
278
268
  def remove_duplicated_shortcuts!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-thor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.13
4
+ version: 0.10.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-25 00:00:00 -07:00
12
+ date: 2009-06-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15