josevalim-thor 0.10.11 → 0.10.12

Sign up to get free protection for your applications and to get access to all the features.
data/lib/thor.rb CHANGED
@@ -104,6 +104,8 @@ class Thor
104
104
  # :aliases - Aliases for this option.
105
105
  # :type - The type of the argument, can be :string, :hash, :array, :numeric, :boolean or :default.
106
106
  # Default accepts arguments as booleans (--switch) or as strings (--switch=VALUE).
107
+ # :group - The group for this options. Use by class options to output options in different levels.
108
+ # :banner - String to show on usage notes.
107
109
  #
108
110
  def method_option(name, options)
109
111
  scope = if options[:for]
data/lib/thor/actions.rb CHANGED
@@ -56,12 +56,6 @@ class Thor
56
56
  super
57
57
 
58
58
  self.root = config[:root]
59
-
60
- # For last, invoke source root to allow the result to be cached. This is
61
- # needed because source root depends on the file location (__FILE__) which
62
- # returns the wrong value if invoked after FileUtils#cd.
63
- #
64
- self.source_root if self.class.respond_to?(:source_root)
65
59
  end
66
60
 
67
61
  # Wraps an action object and call it accordingly to the thor class behavior.
data/lib/thor/base.rb CHANGED
@@ -9,10 +9,7 @@ require 'thor/util'
9
9
 
10
10
  class Thor
11
11
  HELP_MAPPINGS = %w(-h -? --help -D)
12
- THOR_RESERVED_WORDS = %w(all invoke shell options behavior root destination_root relative_root source_root)
13
-
14
- class Maxima < Struct.new(:usage, :options, :class_options)
15
- end
12
+ THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root source_root)
16
13
 
17
14
  module Base
18
15
  attr_accessor :options
@@ -109,6 +106,7 @@ class Thor
109
106
  # :optional - If the argument is optional or not.
110
107
  # :type - The type of the argument, can be :string, :hash, :array, :numeric.
111
108
  # :default - Default value for this argument. It cannot be required and have default values.
109
+ # :banner - String to show on usage notes.
112
110
  #
113
111
  # ==== Errors
114
112
  # ArgumentError:: Raised if you supply a required argument after a non required one.
@@ -131,8 +129,8 @@ class Thor
131
129
  "the non-required argument #{option.human_name.inspect}."
132
130
  end if required
133
131
 
134
- class_options[name] = Thor::Argument.new(name, options[:desc], required,
135
- options[:type], options[:default])
132
+ class_options[name] = Thor::Argument.new(name, options[:desc], required, options[:type],
133
+ options[:default], options[:banner])
136
134
  end
137
135
 
138
136
  # Returns this class arguments, looking up in the ancestors chain.
@@ -173,6 +171,7 @@ class Thor
173
171
  # :aliases - Aliases for this option.
174
172
  # :type - The type of the argument, can be :string, :hash, :array, :numeric, :boolean or :default.
175
173
  # Default accepts arguments as booleans (--switch) or as strings (--switch=VALUE).
174
+ # :banner - String to show on usage notes.
176
175
  #
177
176
  def class_option(name, options)
178
177
  build_option(name, options, class_options)
@@ -358,16 +357,15 @@ class Thor
358
357
  groups = self.class_options.group_values_by { |o| o.group }
359
358
 
360
359
  printer = lambda do |group_name, options|
361
- unless options.empty?
362
- list = []
363
-
364
- options.each do |option|
365
- next if option.argument?
360
+ list = []
366
361
 
367
- list << [ option.usage(false), option.description || "" ]
368
- list << [ "", "Default: #{option.default}" ] if option.description && option.default
369
- end
362
+ options.each do |option|
363
+ next if option.argument?
364
+ list << [ option.usage, option.description || "" ]
365
+ list << [ "", "Default: #{option.default}" ] if option.description && option.default
366
+ end
370
367
 
368
+ unless list.empty?
371
369
  if group_name
372
370
  shell.say "#{group_name} options:"
373
371
  else
@@ -380,7 +378,7 @@ class Thor
380
378
  end
381
379
 
382
380
  # Deal with default group
383
- global_options = groups.delete(nil)
381
+ global_options = groups.delete(nil) || []
384
382
  printer.call(ungrouped_name, global_options) if global_options
385
383
 
386
384
  # Print all others
@@ -392,7 +390,7 @@ class Thor
392
390
  #
393
391
  def is_thor_reserved_word?(word, type)
394
392
  return false unless THOR_RESERVED_WORDS.include?(word.to_s)
395
- raise ScriptError, "'#{word}' is a Thor reserved word and cannot be defined as #{type}"
393
+ raise "'#{word}' is a Thor reserved word and cannot be defined as #{type}"
396
394
  end
397
395
 
398
396
  # Build an option and adds it to the given scope.
@@ -402,8 +400,9 @@ class Thor
402
400
  # options<Hash>:: Described in both class_option and method_option.
403
401
  #
404
402
  def build_option(name, options, scope)
405
- scope[name] = Thor::Option.new(name, options[:desc], options[:required], options[:type],
406
- options[:default], options[:aliases], options[:group])
403
+ scope[name] = Thor::Option.new(name, options[:desc], options[:required],
404
+ options[:type], options[:default], options[:banner],
405
+ options[:group], options[:aliases])
407
406
  end
408
407
 
409
408
  # Receives a hash of options, parse them and add to the scope. This is a
@@ -435,10 +434,13 @@ class Thor
435
434
  end
436
435
 
437
436
  # Everytime someone inherits from a Thor class, register the klass
438
- # and file into baseclass.
437
+ # and file into baseclass. Also invoke the source_root if the klass
438
+ # respond to it. This is needed to ensure that the source_root does
439
+ # not change after FileUtils#cd is called.
439
440
  #
440
441
  def inherited(klass)
441
442
  Thor::Base.register_klass_file(klass)
443
+ klass.source_root if klass.respond_to?(:source_root)
442
444
  end
443
445
 
444
446
  # Fire this callback whenever a method is added. Added methods are
data/lib/thor/group.rb CHANGED
@@ -29,8 +29,8 @@ class Thor::Group
29
29
  opts = Thor::Options.new(class_options)
30
30
  opts.parse(args)
31
31
 
32
- instance = new(opts.arguments, opts.options, config) do |klass, invoke|
33
- klass.prepare(invoke, args, config)
32
+ instance = new(opts.arguments, opts.options, config) do |klass, invoke, overrides|
33
+ klass.prepare(invoke, args, config.merge(overrides))
34
34
  end
35
35
 
36
36
  return instance, nil
@@ -80,29 +80,31 @@ class Thor
80
80
  #
81
81
  def invoke(name, task=nil, method_args=nil)
82
82
  task, method_args = nil, task if task.is_a?(Array)
83
-
84
83
  object, task = _setup_for_invoke(name, task)
85
- klass = object.is_a?(Class) ? object : object.class
86
-
87
- current = @_invocations[klass]
88
- return if current.include?("all")
89
84
 
90
85
  if object.is_a?(Class)
86
+ klass = object
91
87
  instance, trailing = @_initializer.call(klass, task, _overrides_config)
92
88
  method_args ||= trailing
93
89
  else
94
- instance = object
90
+ klass, instance = object.class, object
95
91
  end
96
92
 
97
93
  method_args ||= []
94
+ current = @_invocations[klass]
95
+
96
+ iterator = lambda do |_, task|
97
+ unless current.include?(task.name)
98
+ current << task.name
99
+ task.run(instance, method_args)
100
+ end
101
+ end
98
102
 
99
103
  if task
100
- return if current.include?(task.name)
101
- current << task.name
102
- task.run(instance, method_args)
104
+ iterator.call(nil, task)
103
105
  else
104
- current << "all"
105
- klass.all_tasks.collect { |_, task| task.run(instance) }
106
+ method_args = []
107
+ klass.all_tasks.map(&iterator)
106
108
  end
107
109
  end
108
110
 
data/lib/thor/option.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  class Thor
2
2
  class Option
3
- attr_reader :name, :description, :required, :type, :default, :aliases, :group
3
+ attr_reader :name, :description, :required, :type, :default, :aliases, :group, :banner
4
4
 
5
5
  VALID_TYPES = [:boolean, :numeric, :hash, :array, :string, :default]
6
6
 
7
- def initialize(name, description=nil, required=nil, type=nil, default=nil, aliases=nil, group=nil)
7
+ def initialize(name, description=nil, required=nil, type=nil, default=nil, banner=nil, group=nil, aliases=nil)
8
8
  raise ArgumentError, "Option name can't be nil." if name.nil?
9
9
  raise ArgumentError, "Option cannot be required and have default values." if required && !default.nil?
10
10
  raise ArgumentError, "Type :#{type} is not valid for options." if type && !VALID_TYPES.include?(type.to_sym)
@@ -15,6 +15,7 @@ class Thor
15
15
  @type = (type || :default).to_sym
16
16
  @default = default
17
17
  @aliases = [*aliases].compact
18
+ @banner = banner || formatted_value
18
19
  @group = group.to_s.capitalize if group
19
20
  end
20
21
 
@@ -75,7 +76,7 @@ class Thor
75
76
  value.class.name.downcase.to_sym
76
77
  end
77
78
 
78
- self.new(name.to_s, nil, required, type, default, aliases)
79
+ self.new(name.to_s, nil, required, type, default, nil, nil, aliases)
79
80
  end
80
81
 
81
82
  def argument?
@@ -121,15 +122,9 @@ class Thor
121
122
  (str.length > 1 ? "--" : "-") + str.gsub('_', '-')
122
123
  end
123
124
 
124
- def usage(use_default=true)
125
- sample = if use_default
126
- formatted_default || formatted_value
127
- else
128
- formatted_value
129
- end
130
-
131
- sample = if sample
132
- "#{switch_name}=#{sample}"
125
+ def usage
126
+ sample = if banner
127
+ "#{switch_name}=#{banner}"
133
128
  else
134
129
  switch_name
135
130
  end
@@ -151,30 +146,6 @@ class Thor
151
146
  end
152
147
  end
153
148
 
154
- def formatted_default
155
- return unless default
156
-
157
- case type
158
- when :boolean
159
- nil
160
- when :numeric
161
- default.to_s
162
- when :string, :default
163
- default.to_s.empty? ? formatted_value : default.to_s
164
- when :hash
165
- if default.empty?
166
- formatted_value
167
- else
168
- default.inject([]) do |mem, (key, value)|
169
- mem << "#{key}:#{value}".gsub(/\s/, '_')
170
- mem
171
- end.join(' ')
172
- end
173
- when :array
174
- default.empty? ? formatted_value : default.join(" ")
175
- end
176
- end
177
-
178
149
  def formatted_value
179
150
  case type
180
151
  when :boolean
@@ -197,19 +168,19 @@ class Thor
197
168
  class Argument < Option
198
169
  VALID_TYPES = [:numeric, :hash, :array, :string]
199
170
 
200
- def initialize(name, description=nil, required=true, type=:string, default=nil)
171
+ def initialize(name, description=nil, required=true, type=:string, default=nil, banner=nil)
201
172
  raise ArgumentError, "Argument name can't be nil." if name.nil?
202
173
  raise ArgumentError, "Type :#{type} is not valid for arguments." if type && !VALID_TYPES.include?(type.to_sym)
203
174
 
204
- super(name, description, required, type || :string, default)
175
+ super(name, description, required, type || :string, default, banner)
205
176
  end
206
177
 
207
178
  def argument?
208
179
  true
209
180
  end
210
181
 
211
- def usage(use_default=true)
212
- required? ? formatted_value : "[#{formatted_value}]"
182
+ def usage
183
+ required? ? banner : "[#{banner}]"
213
184
  end
214
185
  end
215
186
  end
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.11
4
+ version: 0.10.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz