class Thor

def dispatch(meth, given_args, given_opts, config) #:nodoc: # rubocop:disable MethodLength

:nodoc: # rubocop:disable MethodLength
The method responsible for dispatching given the args.
def dispatch(meth, given_args, given_opts, config) #:nodoc: # rubocop:disable MethodLength
  meth ||= retrieve_command_name(given_args)
  command = all_commands[normalize_command_name(meth)]
  if !command && config[:invoked_via_subcommand]
    # We're a subcommand and our first argument didn't match any of our
    # commands. So we put it back and call our default command.
    given_args.unshift(meth)
    command = all_commands[normalize_command_name(default_command)]
  end
  if command
    args, opts = Thor::Options.split(given_args)
    if stop_on_unknown_option?(command) && !args.empty?
      # given_args starts with a non-option, so we treat everything as
      # ordinary arguments
      args.concat opts
      opts.clear
    end
  else
    args = given_args
    opts = nil
    command = dynamic_command_class.new(meth)
  end
  opts = given_opts || opts || []
  config[:current_command] = command
  config[:command_options] = command.options
  instance = new(args, opts, config)
  yield instance if block_given?
  args = instance.args
  trailing = args[Range.new(arguments.size, -1)]
  instance.invoke_command(command, trailing || [])
end