diff options
Diffstat (limited to 'lib/bundler/vendor/thor.rb')
-rw-r--r-- | lib/bundler/vendor/thor.rb | 77 |
1 files changed, 49 insertions, 28 deletions
diff --git a/lib/bundler/vendor/thor.rb b/lib/bundler/vendor/thor.rb index 92b24342..225c085b 100644 --- a/lib/bundler/vendor/thor.rb +++ b/lib/bundler/vendor/thor.rb @@ -5,7 +5,7 @@ class Thor # Sets the default task when thor is executed without an explicit task to be called. # # ==== Parameters - # meth<Symbol>:: name of the defaut task + # meth<Symbol>:: name of the default task # def default_task(meth=nil) case meth @@ -28,7 +28,7 @@ class Thor def register(klass, subcommand_name, usage, description, options={}) if klass <= Thor::Group desc usage, description, options - define_method(subcommand_name) { invoke klass } + define_method(subcommand_name) { |*args| invoke(klass, args) } else desc usage, description, options subcommand subcommand_name, klass @@ -108,6 +108,8 @@ class Thor @method_options end + alias options method_options + # Adds an option to the set of method options. If :for is given as option, # it allows you to change the options from a previous defined task. # @@ -132,6 +134,7 @@ class Thor # :aliases - Aliases for this option. # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean. # :banner - String to show on usage notes. + # :hide - If you want to hide this option from the help. # def method_option(name, options={}) scope = if options[:for] @@ -143,6 +146,8 @@ class Thor build_option(name, options, scope) end + alias option method_option + # Prints help information for the given task. # # ==== Parameters @@ -160,7 +165,7 @@ class Thor class_options_help(shell, nil => task.options.map { |_, o| o }) if task.long_description shell.say "Description:" - shell.print_wrapped(task.long_description, :ident => 2) + shell.print_wrapped(task.long_description, :indent => 2) else shell.say task.description end @@ -179,7 +184,7 @@ class Thor list.sort!{ |a,b| a[0] <=> b[0] } shell.say "Tasks:" - shell.print_table(list, :ident => 2, :truncate => true) + shell.print_table(list, :indent => 2, :truncate => true) shell.say class_options_help(shell) end @@ -202,7 +207,11 @@ class Thor def subcommand(subcommand, subcommand_class) self.subcommands << subcommand.to_s subcommand_class.subcommand_help subcommand - define_method(subcommand) { |*args| invoke subcommand_class, args } + + define_method(subcommand) do |*args| + args, opts = Thor::Arguments.split(args) + invoke subcommand_class, args, opts + end end # Extend check unknown options to accept a hash of conditions. @@ -259,8 +268,11 @@ class Thor opts = given_opts || opts || [] config.merge!(:current_task => task, :task_options => task.options) + instance = new(args, opts, config) + yield instance if block_given? + args = instance.args trailing = args[Range.new(arguments.size, -1)] - new(args, opts, config).invoke_task(task, trailing || []) + instance.invoke_task(task, trailing || []) end # The banner for this class. You can customize it if you are invoking the @@ -300,7 +312,6 @@ class Thor # Retrieve the task name from given args. def retrieve_task_name(args) #:nodoc: meth = args.first.to_s unless args.empty? - if meth && (map[meth] || meth !~ /^\-/) args.shift else @@ -308,35 +319,45 @@ class Thor end end - # Receives a task name (can be nil), and try to get a map from it. - # If a map can't be found use the sent name or the default task. + # receives a (possibly nil) task name and returns a name that is in + # the tasks hash. In addition to normalizing aliases, this logic + # will determine if a shortened command is an unambiguous prefix of + # a task or alias. + # + # +normalize_task_name+ also converts names like +animal-prison+ + # into +animal_prison+. def normalize_task_name(meth) #:nodoc: - meth = map[meth.to_s] || find_subcommand_and_update_argv(meth) || meth || default_task - meth.to_s.gsub('-','_') # treat foo-bar > foo_bar - end - - # terrible hack that overwrites ARGV - def find_subcommand_and_update_argv(subcmd_name) #:nodoc: - return unless subcmd_name - cmd = find_subcommand(subcmd_name) - ARGV[0] = cmd if cmd - cmd - end + return default_task.to_s.gsub('-', '_') unless meth - def find_subcommand(subcmd_name) - possibilities = find_subcommand_possibilities subcmd_name + possibilities = find_task_possibilities(meth) if possibilities.size > 1 - raise "Ambiguous subcommand #{subcmd_name} matches [#{possibilities.join(', ')}]" + raise ArgumentError, "Ambiguous task #{meth} matches [#{possibilities.join(', ')}]" elsif possibilities.size < 1 - return nil + meth = meth || default_task + elsif map[meth] + meth = map[meth] + else + meth = possibilities.first end - possibilities.first + meth.to_s.gsub('-','_') # treat foo-bar as foo_bar end - def find_subcommand_possibilities(subcmd_name) - len = subcmd_name.length - all_tasks.map {|t| t.first}.select { |n| subcmd_name == n[0, len] } + # this is the logic that takes the task name passed in by the user + # and determines whether it is an unambiguous prefix of a task or + # alias name. + def find_task_possibilities(meth) + len = meth.to_s.length + possibilities = all_tasks.merge(map).keys.select { |n| meth == n[0, len] }.sort + unique_possibilities = possibilities.map { |k| map[k] || k }.uniq + + if possibilities.include?(meth) + [meth] + elsif unique_possibilities.size == 1 + unique_possibilities + else + possibilities + end end def subcommand_help(cmd) |