lib/thor/base.rb in thor-0.13.8 vs lib/thor/base.rb in thor-0.14.0

- old
+ new

@@ -51,11 +51,11 @@ array_options, hash_options = [], options end opts = Thor::Options.new(parse_options, hash_options) self.options = opts.parse(array_options) - opts.check_unknown! if self.class.check_unknown_options? + opts.check_unknown! if self.class.check_unknown_options?(config) end class << self def included(base) #:nodoc: base.send :extend, ClassMethods @@ -112,14 +112,18 @@ # This is disabled by default to allow dynamic invocations. def check_unknown_options! @check_unknown_options = true end - def check_unknown_options? #:nodoc: - @check_unknown_options || false + def check_unknown_options #:nodoc: + @check_unknown_options ||= from_superclass(:check_unknown_options, false) end + def check_unknown_options?(config) #:nodoc: + !!check_unknown_options + 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: @@ -362,23 +366,29 @@ # # thor :my_task # def namespace(name=nil) case name - when nil - @namespace ||= Thor::Util.namespace_from_thor_class(self) - else - @namespace = name.to_s + when nil + @namespace ||= Thor::Util.namespace_from_thor_class(self) + else + @namespace = name.to_s end end - # Default way to start generators from the command line. + # Parses the task and options from the given args, instantiate the class + # and invoke the task. This method is used when the arguments must be parsed + # from an array. If you are inside Ruby and want to use a Thor class, you + # can simply initialize it: # + # script = MyScript.new(args, options, config) + # script.invoke(:task, first_arg, second_arg, third_arg) + # def start(given_args=ARGV, config={}) - self.debugging = given_args.include?("--debug") + self.debugging = given_args.delete("--debug") config[:shell] ||= Thor::Base.shell.new - yield(given_args.dup) + dispatch(nil, given_args.dup, nil, config) rescue Thor::Error => e debugging ? (raise e) : config[:shell].error(e.message) exit(1) if exit_on_failure? end @@ -533,8 +543,14 @@ # SIGNATURE: Defines behavior when the initialize method is added to the # class. def initialize_added #:nodoc: end + + # SIGNATURE: The hook invoked by start. + def dispatch(task, given_args, given_opts, config) #:nodoc: + raise NotImplementedError + end + end end end