I have this ruby rake task:
namespace :auth_switch do
task :generate, [:clean_up, :tabs, :gateway] => [:environment] do |t, args|
tabs = args[:tabs].to_s.split(' ')
tabs.each do |tab|
require_relative "auth_switch/#{tab}.rb"
end
end
end
task :export, [:clean_up, :tabs, :gateway] => :generate do |t, args|
end
end
setub.rb file
require 'factory_girl'
include FactoryGirl::Syntax::Methods
include FactoryGirl::Syntax::Default
p '~~~~~~~~~~~~~~~'
p gateway = ARGV[0]
args[:gateway]
binding.pry
I use this command to execute the rake:
bundle exec rake auth_switch:export\[true,'moto setup', bogus]
I want to get only the variable for 'bogus' which is number 3:
[1] pry(main)> gateway
=> "auth_switch:export[true, moto setup, bogus]"
How I can do this?
Related
I'm trying to create a custom rake task that takes in two arguments and uses them in my code.
I'm looking at the rails documentation and I see this excerpt for running a rails task with an argument:
task :task_name, [:arg_1] => [:pre_1, :pre_2] do |t, args|
# You can use args from here
end
The rake task can then be invoked like this:
bin/rake "task_name[value 1]"
However, this is way too vague for me. The rails documentation fails to give a concrete example of a rake task with an argument.
For example, I'm looking at this code and I'm thinking what does bin/rake "task_name[value 1]" do? What is [:pre1, :pre2]?
Additionally, I've found some other fantastic links that do things a little bit differently. Here are the links.
Thoughtbot version
In the thoughtbot version that have this example
task :send, [:username] => [:environment] do |t, args|
Tweet.send(args[:username])
end
What is the [:username => [:environment]? its different than the official rails docs.
Here is another:
4 ways to write rake tasks with arguments
I've also looked at the officail optparser documentation and that too has a different way of making it work.
All I want is for this example code that I have to work on my .rake file:
require 'optparse'
task :add do
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: rake add"
opts.on("-o", "--one ARGV", Integer) { |one| options[:one] = one }
opts.on("-t", "--two ARGV", Integer) { |two| options[:two] = two }
end.parse!
puts options[:one].to_i + options[:two].to_i
end
The code fails because of invalid option: -o. I just want to make this work so I can move on. Does anyone have any thoughts?
Here is one of my rake tasks with arguments:
namespace :admin do
task :create_user, [:user_email, :user_password, :is_superadmin] => :environment do |t, args|
email = args[:email]
password = args[:password]
is_superadmin = args[:is_superadmin]
... lots of fun code ...
end
end
and I invoke this task like this:
rake admin:create_user['admin#example.com','password',true]
EDIT
To pass flags in you can do something like this:
task :test_task do |t, args|
options = {a: nil, b: nil}
OptionParser.new do |opts|
opts.banner = "Usage: admin:test_task [options]"
opts.on("--a", "-A", "Adds a") do |a|
options[:a] = true
end
opts.on("--b", "-B", "Adds b") do |b|
options[:b] = true
end
end.parse!
puts options.inspect
end
And examples of invoking it:
rake admin:test_task -A -B
rake admin:test_task -A
rake admin:test_task -B
I would to know if it was possible to pass an option from the commande line to the task without having the Rakefile taking option for itself.
Here is my task:
task :task do
args = ARGV.drop(1)
system("ruby test/batch_of_test.rb #{args.join(' ')}")
args.each { |arg| task arg.to_sym do ; end }
end
Here is the command I want to be functional:
rake task --name test_first_one
The result I want is the same as this:
ruby test/batch_of_test.rb --name test_first_one
For this code, I get this error:
invalid option: --name
How can --name (or another option) can be passed to my task ?
As far as I know you cannot add flags to Rake tasks. I think the closest syntax available is using ENV variables.
rake task NAME=test_first_one
Then in your task:
name = ENV['NAME']
like this:
task :task, [args] => :environment do |t, args|
system("ruby test/batch_of_test.rb #{args.join(' ')}")
args.each { |arg| task arg.to_sym do ; end }
end
and you call it like this.
rake task[args]
UPDATE
task :task, [:options] => :environment do |t, args|
arg = args[:options].pop
options = args[:options]
end
Reffering that answer I was trying to use OptionParser to parse rake arguments. I simplified example from there and I had to add two ARGV.shift to make it work.
require 'optparse'
namespace :user do |args|
# Fix I hate to have here
puts "ARGV: #{ARGV}"
ARGV.shift
ARGV.shift
puts "ARGV: #{ARGV}"
desc 'Creates user account with given credentials: rake user:create'
# environment is required to have access to Rails models
task :create => :environment do
options = {}
OptionParser.new(args) do |opts|
opts.banner = "Usage: rake user:create [options]"
opts.on("-u", "--user {username}","Username") { |user| options[:user] = user }
end.parse!
puts "user: #{options[:user]}"
exit 0
end
end
This is the output:
$ rake user:create -- -u foo
ARGV: ["user:create", "--", "-u", "foo"]
ARGV: ["-u", "foo"]
user: foo
I assume ARGV.shift is not the way it should be done. I would like to know why it doesn't work without it and how to fix it in a proper way.
You can use the method OptionParser#order! which returns ARGV without the wrong arguments:
options = {}
o = OptionParser.new
o.banner = "Usage: rake user:create [options]"
o.on("-u NAME", "--user NAME") { |username|
options[:user] = username
}
args = o.order!(ARGV) {}
o.parse!(args)
puts "user: #{options[:user]}"
You can pass args like that: $ rake foo:bar -- '--user=john'
I know this does not strictly answer your question, but did you consider using task arguments?
That would free you having to fiddle with OptionParser and ARGV:
namespace :user do |args|
desc 'Creates user account with given credentials: rake user:create'
task :create, [:username] => :environment do |t, args|
# when called with rake user:create[foo],
# args is now {username: 'foo'} and you can access it with args[:username]
end
end
For more info, see this answer here on SO.
Try this:
require 'optparse'
namespace :programs do |args|
desc "Download whatever"
task :download => [:environment] do
# USAGE: rake programs:download -- rm
#-- Setting options $ rake programs:download -- --rm
options = {}
option_parser = OptionParser.new
option_parser.banner = "Usage: rake programs:download -- rm"
option_parser.on("-r", "--rm","Remove s3 files downloaded") do |value|
options[:remove] = value
end
args = option_parser.order!(ARGV) {}
option_parser.parse!(args)
#-- end
# your code here
end
end
You have to put a '=' between -u and foo:
$ rake user:create -- -u=foo
Instead of:
$ rake user:create -- -u foo
When I specify the value for argument, it is a string. So I tried to turn it into an integer, but this is not working.
Code is like this:
task :fetch_video, [:fetch_number] => :environment do |t, args|
args.with_defaults(:fetch_number => 4)
args.fetch_number = args.fetch_number.to_i
puts args.fetch_number.class
#run rake fetch_video[10],
#returns: String
end
What mistake did I make?
You're trying to write the args variable, I don't think you can do that. Try this:
task :fetch_video, [:fetch_number] => :environment do |t, args|
args.with_defaults(:fetch_number => 4)
local_fetch_number = args.fetch_number.to_i
puts local_fetch_number.class
end
I've seen similar questions here, couldn't figure out the answer I'm just starting with ror development. I've got a rake task :
namespace:generate do
desc "Export to txt"
task :txt => :environment do |t, args|
puts "testing #{args}"
end
end
When I do this from command line:
rake generate:txt
I get testing {}
When I try to inject value as an argument like this :
rake generate:txt[testvalue] or
rake generate:txt testvalue
First one does nothing same output testing {} second one I get an error. So in which way do I invoke command so I populate args with some value(s)?
IIRC, you have to declare parameters explicitly.
task :environment do
puts "setting up env"
end
namespace :generate do
desc "Export to txt"
task :txt, [:filename] => :environment do |t, args|
puts "testing #{args}"
end
end
Then
% rake generate:txt['foo']
setting up env
testing {:filename=>"foo"}
namespace:generate do
desc "Export to txt"
task :txt, [:arg1] => :environment do |t, args|
puts "testing #{args[:arg1]}"
end
end
rake generate:csv arg1="value"