> For the complete documentation index, see [llms.txt](https://ploegert.gitbook.io/til/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ploegert.gitbook.io/til/programmy/rails/handle-named-arguments-in-a-rake-task.md).

# Handle Named Arguments In A Rake Task

There are [a number of ways](https://www.seancdavis.com/blog/4-ways-to-pass-arguments-to-a-rake-task/) to pass arguments into a Rake task. The standard approach only allows for positional arguments. When I need named and optional arguments, my preferred approach is to use environment variables.

Here is a skimmed down version of a user lookup task. Notice the task definition itself doesn't include any arguments. Instead, a couple optional values are extracted from the environment (`ENV`) at the beginngin of the task.

```ruby
desc "An example task with named, optional arguments"
task :lookup_user => :environment do
  user_id = ENV['USER_ID']
  email = ENV['EMAIL']

  if user_id.present?
    user = User.find(user_id)

    if user.blank?
      puts "No user for id ##{user_id}"
    end
  elsif email.present?
    user = User.find_by(email: email)

    if user.blank?
      puts "No user for email #{email}"
    end
  end

  puts "User found" if user.present?
end
```

This task can be invoked in the following ways:

```bash
$ rake lookup_user USER_ID=123
```

```bash
$ rake lookup_user EMAIL="user@example.com"
```

or even with both arguments included, in which case the task has been written to give precedence to `USER_ID`:

```bash
$ rake lookup_user EMAIL="user@example.com" USER_ID=123
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/handle-named-arguments-in-a-rake-task.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
