lib/thor/base.rb in thor-0.15.2 vs lib/thor/base.rb in thor-0.15.3

- old
+ new

@@ -68,15 +68,16 @@ # Add the remaining arguments from the options parser to the # arguments passed in to initialize. Then remove any positional # arguments declared using #argument (this is primarily used # by Thor::Group). Tis will leave us with the remaining # positional arguments. - thor_args = Thor::Arguments.new(self.class.arguments) - thor_args.parse(args + opts.remaining).each { |k,v| send("#{k}=", v) } - args = thor_args.remaining + to_parse = args + to_parse += opts.remaining unless self.class.strict_args_position?(config) - @args = args + thor_args = Thor::Arguments.new(self.class.arguments) + thor_args.parse(to_parse).each { |k,v| send("#{k}=", v) } + @args = thor_args.remaining end class << self def included(base) #:nodoc: base.send :extend, ClassMethods @@ -139,10 +140,25 @@ def check_unknown_options?(config) #:nodoc: !!check_unknown_options 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 + end + + def strict_args_position #:nodoc: + @strict_args_position ||= from_superclass(:strict_args_position, false) + end + + def strict_args_position?(config) #:nodoc: + !!strict_args_position + end + # Adds an argument to the class and creates an attr_accessor for it. # # Arguments are different from options in several aspects. The first one # is how they are parsed from the command line, arguments are retrieved # from position: @@ -194,12 +210,13 @@ next if argument.required? raise ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " << "the non-required argument #{argument.human_name.inspect}." end if required - arguments << Thor::Argument.new(name, options[:desc], required, options[:type], - options[:default], options[:banner]) + options[:required] = required + + arguments << Thor::Argument.new(name, options) end # Returns this class arguments, looking up in the ancestors chain. # # ==== Returns @@ -508,14 +525,13 @@ # Build an option and adds it to the given scope. # # ==== Parameters # name<Symbol>:: The name of the argument. # options<Hash>:: Described in both class_option and method_option. + # scope<Hash>:: Options hash that is being built up def build_option(name, options, scope) #:nodoc: - scope[name] = Thor::Option.new(name, options[:desc], options[:required], - options[:type], options[:default], options[:banner], - options[:lazy_default], options[:group], options[:aliases], options[:hide]) + scope[name] = Thor::Option.new(name, options) end # Receives a hash of options, parse them and add to the scope. This is a # fast way to set a bunch of options: # @@ -574,10 +590,17 @@ def from_superclass(method, default=nil) if self == baseclass || !superclass.respond_to?(method, true) default else value = superclass.send(method) - value.dup if value + + if value + if value.is_a?(TrueClass) || value.is_a?(Symbol) + value + else + value.dup + end + end end end # A flag that makes the process exit with status 1 if any error happens. def exit_on_failure?