4

Using the task dependency notation, you can pass in arguments to the default task. For example, say "version" is your argument:

task :default, [:version] => [:build]

task :build, :version do |t,args|
  version = args[:version]
  puts version ? "version is #{version}" : "no version passed"
end

Then you can call it like so:

$ rake
no version passed

or

$ rake default[3.2.1]
version is 3.2.1

or

$ rake build[3.2.1]
version is 3.2.1

However, I have not found a way to avoid specifying the task name (default or build) while passing in arguments.

Does anyone know of a way to use this notation and not have to specify the task name? (i.e. take advantage of the "default" syntax as well?)

I know of the ENV[] approach of receving parameters, as described here: How do I have the :default Rake task depend on a task with arguments?

I'm looking for a way to use the built in notation (as shown above) but avoid having to specify the task name.

4

1 回答 1

3

如果您查看 rake 源代码,您会发现这是不可能的(GitHub):

# Collect the list of tasks on the command line.  If no tasks are
# given, return a list containing only the default task.
# Environmental assignments are processed at this time as well.
def collect_tasks
  @top_level_tasks = []
  ARGV.each do |arg|
    if arg =~ /^(\w+)=(.*)$/m
      ENV[$1] = $2
    else
      @top_level_tasks << arg unless arg =~ /^-/
    end
  end
  @top_level_tasks.push(default_task_name) if @top_level_tasks.empty?
end

在此方法中,要运行的任务是从提供的参数中收集的。以开头的-所有内容都被忽略,表单foo=bar中的所有内容都用于设置环境,其他所有内容都被视为任务名称。

如果您使用参数 ( foo[bar]) 指定任务,则参数将被解析为parse_task_string.

如果不指定任务,default则不带任何参数使用。

于 2013-10-20T21:20:06.383 回答