lib/thor/base.rb in thor-0.16.0 vs lib/thor/base.rb in thor-0.17.0

- old
+ new

@@ -8,10 +8,11 @@ require 'thor/util' class Thor autoload :Actions, 'thor/actions' autoload :RakeCompat, 'thor/rake_compat' + autoload :Group, 'thor/group' # Shortcuts for help. HELP_MAPPINGS = %w(-h -? --help -D) # Thor methods that should not be overwritten by the user. @@ -56,11 +57,12 @@ end # Let Thor::Options parse the options first, so it can remove # declared options from the array. This will leave us with # a list of arguments that weren't declared. - opts = Thor::Options.new(parse_options, hash_options) + stop_on_unknown = self.class.stop_on_unknown_option? config[:current_task] + opts = Thor::Options.new(parse_options, hash_options, stop_on_unknown) self.options = opts.parse(array_options) # If unknown options are disallowed, make sure that none of the # remaining arguments looks like an option. opts.check_unknown! if self.class.check_unknown_options?(config) @@ -72,11 +74,11 @@ # positional arguments. to_parse = args to_parse += opts.remaining unless self.class.strict_args_position?(config) thor_args = Thor::Arguments.new(self.class.arguments) - thor_args.parse(to_parse).each { |k,v| send("#{k}=", v) } + thor_args.parse(to_parse).each { |k,v| __send__("#{k}=", v) } @args = thor_args.remaining end class << self def included(base) #:nodoc: @@ -140,10 +142,17 @@ def check_unknown_options?(config) #:nodoc: !!check_unknown_options end + # If true, option parsing is suspended as soon as an unknown option or a + # regular argument is encountered. All remaining arguments are passed to + # the task as regular arguments. + def stop_on_unknown_option?(task_name) #:nodoc: + false + end + # If you want only strict string args (useful when cascading thor classes), # call strict_args_position! This is disabled by default to allow dynamic # invocations. def strict_args_position! @strict_args_position = true @@ -302,15 +311,15 @@ # # ==== Parameters # name<String|Symbol> # def group(name=nil) - case name - when nil - @group ||= from_superclass(:group, 'standard') - else - @group = name.to_s + @group = case name + when nil + @group || from_superclass(:group, 'standard') + else + name.to_s end end # Returns the tasks for this Thor class. # @@ -402,13 +411,13 @@ # Your tasks can be invoked with a shortcut. Instead of: # # thor :my_task # def namespace(name=nil) - case name + @namespace = case name when nil - @namespace ||= Thor::Util.namespace_from_thor_class(self) + @namespace || Thor::Util.namespace_from_thor_class(self) else @namespace = name.to_s end end @@ -460,11 +469,15 @@ def handle_argument_error(task, error, arity=nil) #:nodoc: msg = "#{basename} #{task.name}" if arity required = arity < 0 ? (-1 - arity) : arity - msg << " requires at least #{required} argument" - msg << "s" if required > 1 + if required == 0 + msg << " should have no arguments" + else + msg << " requires at least #{required} argument" + msg << "s" if required > 1 + end else msg = "call #{msg} as" end msg << ": #{self.banner(task).inspect}."