I have a rake task that returns a count of records for a given email. The email is provided to the rake task as a command line argument, like so
rake stats:count email=me@gmail.com
But if the rake task is called without an email provided (ie rake stats:count
), I would like helpful message to appear, like "Please provide an email"
How do I do this?
I have tried this
email = ENV['email']
if email.nil? puts "Please provide an email"
end
But it errors with NameError: undefined local variable or method email' for main:Object
An argument provided to a rake task via var=value
is accessible inside the rake task with ENV['var']
.
If the argument is not provided, ENV['var']
will return nil
.
So you can simply check ENV['email'].nil?
ie
if ENV['email'].nil?
puts "Please provide an email"
else
# code to run if email is provided
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.