public
Created

Illustration of rake/pull/24

  • Download Gist
Rakefile
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
# To run, export RUBYOPT so that it applies the patch before invoking rake:
#
# export RUBYOPT='-rpatch'
# rake -h
# rake -T
# rake -- cmd_echo a b
#
 
#
# Part 1
#
 
task :standard_echo, :a, :b do |t, args|
message = ENV['MESSAGE'] || 'got'
puts "#{message}: #{args.a} #{args.b}"
end
 
cmd :cmd_echo, :a, :b, %{
-m,--message [got] : A message
} do |config, args|
puts "#{config.message}: #{args.a} #{args.b}"
end
 
#
# Part 2
#
 
task :standard_one do
puts(ENV['MESSAGE'] || 'got')
end
task :standard_two do
puts(ENV['MESSAGE'] || 'got')
end
 
cmd :cmd_one, %{
-m,--message [got] : A message
} do |config, args|
puts config.message
end
cmd :cmd_two, %{
-m,--message [got] : A message
} do |config, args|
puts config.message
end
examples.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
export RUBYOPT='-rpatch'
echo '------------ part 1 ------------'
 
rake standard_echo[a,b]
# got: a b
rake standard_echo['a,b',c]
# got: a b
 
rake -- cmd_echo a b
# got: a b
rake -- cmd_echo 'a,b' c
# got: a b
 
echo '------------ part 2 ------------'
 
rake standard_one MESSAGE=hello standard_two MESSAGE=goodbye
# goodbye
# goodbye
 
rake -- cmd_one -m hello -- cmd_two -m goodbye
# hello
# goodbye
patch.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
require 'rubygems'
require 'rake'
require 'optparse'
 
module Rake
class CommandTask < Task
def parser
@parser ||= OptionParser.new do |opts|
opts.on_tail("-h", "--help", "Display this help message.") do
puts opts
exit
end
end
end
 
def invoke(*args)
parser.parse!(args)
super(*args)
end
 
def invoke_with_call_chain(*args)
super
reenable
end
 
def reenable
@config = nil
super
end
 
def config
@configs ||= default_config.dup
end
 
def default_config
@default_config ||= {}
end
 
def [](key)
config[key.to_sym]
end
 
def method_missing(sym, *args, &block)
sym = sym.to_sym
config.has_key?(sym) ? config[sym] : super
end
 
def set_arg_names(args)
while options = parse_options(args.last)
set_options(options)
args.pop
end
@arg_names = args.map { |a| a.to_sym }
parser.banner = "Usage: rake -- #{name} [options] #{@arg_names.join(' ')}"
@arg_names
end
 
def parse_options(obj)
case obj
when Array then [obj]
when String then parse_options_string(obj)
else nil
end
end
 
def parse_options_string(string)
string = string.strip
return nil unless string[0] == ?-
string.split(/\s*\n\s*/).collect do |str|
flags, desc = str.split(':', 2)
flags = flags.split(',').collect! {|arg| arg.strip }
key = guess_key(flags)
default = flags.last =~ /\s+\[(.*)\]/ ? guess_default($1) : guess_bool_default(flags)
[key, default] + flags + [desc.to_s.strip]
end
end
 
def guess_key(flags)
keys = flags.collect do |flag|
case flag.split(' ').first
when /\A-([^-])\z/ then $1
when /\A--\[no-\](.*)\z/ then $1
when /\A--(.*)\z/ then $1
else nil
end
end
keys.compact.sort_by {|key| key.length }.last
end
 
def guess_default(str)
case str
when /\A(\d+)\z/ then str.to_i
when /\A(\d+\.\d+)\z/ then str.to_f
else str
end
end
 
def guess_bool_default(flags)
flags.any? {|flag| flag =~ /\A--\[no-\]/ ? true : false }
end
 
def set_options(options)
options.each do |(key, default, *option)|
default = false if default.nil?
option = guess_option(key, default) if option.empty?
 
default_config[key.to_sym] = default
parser.on(*option) do |value|
config[key.to_sym] = parse_config_value(default, value)
end
end
end
 
def guess_option(key, default)
n = key.to_s.length
case default
when false
n == 1 ? ["-#{key}"] : ["--#{key}"]
when true
["--[no-]#{key}"]
else
n == 1 ? ["-#{key} [#{key.to_s.upcase}]"] : ["--#{key} [#{key.to_s.upcase}]"]
end
end
 
def parse_config_value(default, value)
case default
when String then value.to_s
when Integer then value.to_i
when Float then value.to_f
else value
end
end
end
end
 
module Rake
module DSL
def cmd(*args, &block)
# Removed to skip depracation warning...
# unless args.last.kind_of?(Hash)
# args << {:needs => []}
# end
Rake::CommandTask.define_task(*args, &block)
end
end
end
 
module Rake
class Application
def format_task_string(args)
"#{args.shift}[#{args.join(',')}]"
end
 
# Read and handle the command line options.
def handle_options
options.rakelib = ['rakelib']
 
OptionParser.new do |opts|
opts.banner = "rake [-f rakefile] {options} targets..."
opts.separator ""
opts.separator "Options are ..."
 
opts.on_tail("-h", "--help", "-H", "Display this help message.") do
puts opts
exit
end
 
standard_rake_options.each { |args| opts.on(*args) }
 
opts.on_tail("--", "Turn on arg syntax ('-- a b c' => 'a[b,c]')") do
# restores option break to ARGV if found, allowing arg syntax
throw :terminate, "--"
end
 
opts.environment('RAKEOPT')
end.parse!
 
# If class namespaces are requested, set the global options
# according to the values in the options structure.
if options.classic_namespace
$show_tasks = options.show_tasks
$show_prereqs = options.show_prereqs
$trace = options.trace
$dryrun = options.dryrun
$silent = options.silent
end
end
# 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 = []
 
current = nil
ARGV.each do |arg|
case arg
when '--'
if current && !current.empty?
@top_level_tasks << format_task_string(current)
end
current = []
when /^(\w+)=(.*)$/
ENV[$1] = $2
else
if current
current << arg
else
@top_level_tasks << arg unless arg =~ /^-/
end
end
end
 
if current && !current.empty?
@top_level_tasks << format_task_string(current)
end
 
@top_level_tasks.push("default") if @top_level_tasks.size == 0
end
end
end
response.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
@richmeyers for the most part I agree with your opinion but I should clarify that the '--' syntax does nothing to fix the escape issues. Commas cannot be specified using either syntax. Here are some examples - see this [gist](https://gist.github.com/1027463) to run these yourself:
 
```bash
# [Rakefile]
# task :standard_echo, :a, :b do |t, args|
# message = ENV['MESSAGE'] || 'got'
# puts "#{message}: #{args.a} #{args.b}"
# end
#
# cmd :cmd_echo, :a, :b, %{
# -m,--message [got] : A message
# } do |config, args|
# puts "#{config.message}: #{args.a} #{args.b}"
# end
 
rake standard_echo[a,b]
# got: a b
rake standard_echo['a,b',c]
# got: a b
 
rake -- cmd_echo a b
# got: a b
rake -- cmd_echo 'a,b' c
# got: a b
```
 
To answer your question, one of the benefits of per-task options is that you can specify different options for different tasks. This is not possible with the ENV syntax, or at least opens you up to collisions because ENV is global. Sometimes you do want global options; this proposal provides per-task options when you don't.
 
```bash
# [Rakefile (continued)]
# task :standard_one do
# puts(ENV['MESSAGE'] || 'got')
# end
# task :standard_two do
# puts(ENV['MESSAGE'] || 'got')
# end
#
# cmd :cmd_one, %{
# -m,--message [got] : A message
# } do |config, args|
# puts config.message
# end
# cmd :cmd_two, %{
# -m,--message [got] : A message
# } do |config, args|
# puts config.message
# end
 
rake standard_one MESSAGE=hello standard_two MESSAGE=goodbye
# goodbye
# goodbye
 
rake -- cmd_one -m hello -- cmd_two -m goodbye
# hello
# goodbye
```

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.