lib/thor/base.rb in thor-0.19.1 vs lib/thor/base.rb in thor-0.19.2

- old
+ new

@@ -12,15 +12,15 @@ autoload :Actions, "thor/actions" autoload :RakeCompat, "thor/rake_compat" autoload :Group, "thor/group" # Shortcuts for help. - HELP_MAPPINGS = %w[-h -? --help -D] + HELP_MAPPINGS = %w(-h -? --help -D) # Thor methods that should not be overwritten by the user. - THOR_RESERVED_WORDS = %w[invoke shell options behavior root destination_root relative_root - action add_file create_file in_root inside run run_ruby_script] + THOR_RESERVED_WORDS = %w(invoke shell options behavior root destination_root relative_root + action add_file create_file in_root inside run run_ruby_script) TEMPLATE_EXTNAME = ".tt" module Base attr_accessor :options, :parent_options, :args @@ -39,26 +39,28 @@ # The hash given is converted to a hash with indifferent # access, magic predicates (options.skip?) and then frozen. # # config<Hash>:: Configuration for this Thor class. # - def initialize(args = [], local_options = {}, config = {}) # rubocop:disable MethodLength - parse_options = self.class.class_options + def initialize(args = [], local_options = {}, config = {}) + parse_options = config[:current_command] && config[:current_command].disable_class_options ? {} : self.class.class_options # The start method splits inbound arguments at the first argument # that looks like an option (starts with - or --). It then calls # new, passing in the two halves of the arguments Array as the # first two parameters. command_options = config.delete(:command_options) # hook for start parse_options = parse_options.merge(command_options) if command_options if local_options.is_a?(Array) - array_options, hash_options = local_options, {} + array_options = local_options + hash_options = {} else # Handle the case where the class was explicitly instantiated # with pre-parsed options. - array_options, hash_options = [], local_options + array_options = [] + hash_options = local_options 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. @@ -203,12 +205,12 @@ # :banner - String to show on usage notes. # # ==== Errors # ArgumentError:: Raised if you supply a required argument after a non required one. # - def argument(name, options = {}) # rubocop:disable MethodLength - is_thor_reserved_word?(name, :argument) + def argument(name, options = {}) + thor_reserved_word?(name, :argument) no_commands { attr_accessor name } required = if options.key?(:optional) !options[:optional] elsif options.key?(:required) @@ -217,15 +219,17 @@ options[:default].nil? end remove_argument name - arguments.each do |argument| - next if argument.required? - fail ArgumentError, "You cannot have #{name.to_s.inspect} as required argument after " << - "the non-required argument #{argument.human_name.inspect}." - end if required + if required + arguments.each do |argument| + 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 + end options[:required] = required arguments << Thor::Argument.new(name, options) end @@ -341,11 +345,11 @@ # OrderedHash:: An ordered hash with commands names as keys and Thor::Command # objects as values. # def all_commands @all_commands ||= from_superclass(:all_commands, Thor::CoreExt::OrderedHash.new) - @all_commands.merge(commands) + @all_commands.merge!(commands) end alias_method :all_tasks, :all_commands # Removes a given command from this Thor class. This is usually done if you # are inheriting from another class and don't want it to be available @@ -465,24 +469,21 @@ end end alias_method :public_task, :public_command def handle_no_command_error(command, has_namespace = $thor_runner) #:nodoc: - if has_namespace - fail UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace." - else - fail UndefinedCommandError, "Could not find command #{command.inspect}." - end + raise UndefinedCommandError, "Could not find command #{command.inspect} in #{namespace.inspect} namespace." if has_namespace + raise UndefinedCommandError, "Could not find command #{command.inspect}." end alias_method :handle_no_task_error, :handle_no_command_error def handle_argument_error(command, error, args, arity) #:nodoc: msg = "ERROR: \"#{basename} #{command.name}\" was called with " msg << "no arguments" if args.empty? msg << "arguments " << args.inspect unless args.empty? msg << "\nUsage: #{banner(command).inspect}" - fail InvocationError, msg + raise InvocationError, msg end protected # Prints the class options per group. If an option does not belong to @@ -511,29 +512,28 @@ list = [] padding = options.map { |o| o.aliases.size }.max.to_i * 4 options.each do |option| - unless option.hide - item = [option.usage(padding)] - item.push(option.description ? "# #{option.description}" : "") + next if option.hide + item = [option.usage(padding)] + item.push(option.description ? "# #{option.description}" : "") - list << item - list << ["", "# Default: #{option.default}"] if option.show_default? - list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum - end + list << item + list << ["", "# Default: #{option.default}"] if option.show_default? + list << ["", "# Possible values: #{option.enum.join(', ')}"] if option.enum end shell.say(group_name ? "#{group_name} options:" : "Options:") shell.print_table(list, :indent => 2) shell.say "" end # Raises an error if the word given is a Thor reserved word. - def is_thor_reserved_word?(word, type) #:nodoc: + def thor_reserved_word?(word, type) #:nodoc: return false unless THOR_RESERVED_WORDS.include?(word.to_s) - fail "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}" + raise "#{word.inspect} is a Thor reserved word and cannot be defined as #{type}" end # Build an option and adds it to the given scope. # # ==== Parameters @@ -564,11 +564,11 @@ if commands[name.to_s] commands[name.to_s] elsif command = all_commands[name.to_s] # rubocop:disable AssignmentInCondition commands[name.to_s] = command.clone else - fail ArgumentError, "You supplied :for => #{name.inspect}, but the command #{name.inspect} could not be found." + raise ArgumentError, "You supplied :for => #{name.inspect}, but the command #{name.inspect} could not be found." end end alias_method :find_and_refresh_task, :find_and_refresh_command # Everytime someone inherits from a Thor class, register the klass @@ -592,11 +592,11 @@ return unless public_method_defined?(meth.to_sym) @no_commands ||= false return if @no_commands || !create_command(meth) - is_thor_reserved_word?(meth, :command) + thor_reserved_word?(meth, :command) Thor::Base.register_klass_file(self) end # Retrieves a value from superclass. If it reaches the baseclass, # returns default. @@ -647,10 +647,10 @@ def initialize_added #:nodoc: end # SIGNATURE: The hook invoked by start. def dispatch(command, given_args, given_opts, config) #:nodoc: - fail NotImplementedError + raise NotImplementedError end end end end