How do I pass arguments to my rake task in ruby - ruby-on-rails

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"

Related

Read command line arguments into rb file

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?

How can I write a Rake task which depends on task with namespace and parameter

I have rake tasks which installs and starts neo4j.
rake neo4j:install[community-latest, stable]
rake neo4j:start[stable] where `stable` is environment.
Now I want to write another rake task something like rake setup and create dependency on rake neo4j:start[stable] and rake neo4j:install[community-latest, stable]
I have tried,
task :setup_dev_env => [:neo4j:install[community-latest, stable], :neo4j:start[stable]] do
puts "Created Rake task"
end
obviously this doesn't work, because in the above task neo4j is namespace. Then I have changed my task to something like,
task :setup_dev_env => [:'neo4j:install[community-latest, stable]', :'neo4j:start[stable]'] do
puts "Hello rake task working"
end
so, at least this solved my issue with neo4j namespace, but still couldn't solve the problem.
When I run rake setup_dev_env It says
rake aborted!
Don't know how to build task 'neo4j:install[community-latest, stable]' (see --tasks)
You can modify that task as:
desc 'Some description'
# setup_dev_env is dependent on neo4j:start
task :setup_dev_env,[:stable] => :environment do |t, arg|
param = arg[:stable].nil? ? 'stable' : arg[:stable]
Rake::Task['neo4j:start'].invoke(param)
puts "Created Rake task"
end
You can write your task as follow:
desc 'Some description'
# setup_dev_env is dependent on neo4j:start
task :setup_dev_env,[:stable] => "neo4j:start" do
puts "Created Rake task"
end
The above lines of code accept a parameter stable and pass it to neo4j:start. To use stable parameter in neo4j:start, you must have to receive as like:
desc 'Some description'
# it will be inside namespace neo4j
task :start, [:stable] do |t, args|
puts "Created Rake task #{args.inspect}"
end

I don't understand how to add arguments to a rake task. (Excerpt from the documentation)

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

How to pass an option to a Rake task?

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

Unable to change passed arguments to rake tasks

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

Resources