Skip to main content
Fixing title
Link
engineersmnky
  • 30.5k
  • 2
  • 42
  • 64

Caling Calling OptionParser inside a rake task with optparseRake Task

added 352 characters in body
Source Link
Sig
  • 6.2k
  • 12
  • 64
  • 104

Given the dummy rake task below

require "optparse" namespace :a do namespace :b do task c: :environment do options = {} OptionParser.new do |parser| parser.banner = "" parser.on("-v", "--verbose", "Run verbosely") do |v| options[:verbose] = v end end.parse! puts "options: #{options}" end end end 

How should I call it?

From my understanding, the way should be bin/rails a:b:c -v 3 or bin/rails a:b:c -- -v 3

But both ways generate:

Unrecognized command "3" (Rails::Command::UnrecognizedCommandError)

What am I missing here?

I'm running the task on a Mac.

UPDATE 1

a@mac appa % bundle exec rake a:b:c -- -v 3 options: {} rake aborted! Don't know how to build task '3' (See the list of available tasks with `rake --tasks`) /Users/sa/.rbenv/versions/3.3.7/bin/bundle:25:in `load' /Users/sa/.rbenv/versions/3.3.7/bin/bundle:25:in `<main>' (See full trace by running task with --trace) 

Given the dummy rake task below

require "optparse" namespace :a do namespace :b do task c: :environment do options = {} OptionParser.new do |parser| parser.banner = "" parser.on("-v", "--verbose", "Run verbosely") do |v| options[:verbose] = v end end.parse! puts "options: #{options}" end end end 

How should I call it?

From my understanding, the way should be bin/rails a:b:c -v 3 or bin/rails a:b:c -- -v 3

But both ways generate:

Unrecognized command "3" (Rails::Command::UnrecognizedCommandError)

What am I missing here?

I'm running the task on a Mac.

Given the dummy rake task below

require "optparse" namespace :a do namespace :b do task c: :environment do options = {} OptionParser.new do |parser| parser.banner = "" parser.on("-v", "--verbose", "Run verbosely") do |v| options[:verbose] = v end end.parse! puts "options: #{options}" end end end 

How should I call it?

From my understanding, the way should be bin/rails a:b:c -v 3 or bin/rails a:b:c -- -v 3

But both ways generate:

Unrecognized command "3" (Rails::Command::UnrecognizedCommandError)

What am I missing here?

I'm running the task on a Mac.

UPDATE 1

a@mac appa % bundle exec rake a:b:c -- -v 3 options: {} rake aborted! Don't know how to build task '3' (See the list of available tasks with `rake --tasks`) /Users/sa/.rbenv/versions/3.3.7/bin/bundle:25:in `load' /Users/sa/.rbenv/versions/3.3.7/bin/bundle:25:in `<main>' (See full trace by running task with --trace) 
Source Link
Sig
  • 6.2k
  • 12
  • 64
  • 104

Caling a rake task with optparse

Given the dummy rake task below

require "optparse" namespace :a do namespace :b do task c: :environment do options = {} OptionParser.new do |parser| parser.banner = "" parser.on("-v", "--verbose", "Run verbosely") do |v| options[:verbose] = v end end.parse! puts "options: #{options}" end end end 

How should I call it?

From my understanding, the way should be bin/rails a:b:c -v 3 or bin/rails a:b:c -- -v 3

But both ways generate:

Unrecognized command "3" (Rails::Command::UnrecognizedCommandError)

What am I missing here?

I'm running the task on a Mac.