How can I start a Rails console with command line arguments?

Aaron

I'm in the process of upgrading a Rails app from major version 3 to 4. In the previous version we could tap into the boot process by passing a block to config.before_initialize in our Application class. From here I could specify options like rails c -some_cool_switch and have access to it from ARGV.

In Rails 4 I cannot even get to the pry line in this block as I'm getting the following error invalid option: --foobar (OptionParser::InvalidOption).

I'm about to dive into the source code to figure out what I can do short of monkey patching and will report back. I was hoping that someone else has a better solution or at least an idea that I'm going up the wrong tree.

I can always fallback to so something like SOME_ENV_VAR=some_cool_switch rails c and just use another ENV var but I'd like to know my options and if this is the better approach or not.

Reason this no longer works

The railties gem has changed. Previously on Rails 3 the command line arguments were not parsed until start was called on the Console. Now the command line is parsed before the app initializes and the options are strict so an error is raised.

tl;dr

In Rails 4, is it possible to pass arguments to the console like rails c --some_arg, and if so, how?

Mohanraj

You should specify the arguments like below,

➜  test_app git:(master) ✗ RAILS_E=rails_test rails c -- --rails_env_test test
Loading development environment (Rails 4.2.1)
[1] pry(main)> ARGV
=> ["--rails_env_test", "test"]
[2] pry(main)> 

or

➜  test_app git:(master) ✗ RAILS_E=rails_test rails c -- --rails_env_test=test               
Loading development environment (Rails 4.2.1)
[1] pry(main)> ARGV
=> ["--rails_env_test=test"]
[2] pry(main)> 

Then you can process the ARGV to get the passed values. Please let me know if you need more on this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I pass "start date" and "end date" as command line arguments in Python

From Java

How can i ignore a character at start/end of the match, i am like tryin to find command-line arguments

From Dev

How can I parse command line arguments in PHP?

From Dev

How can i comment command line arguments on multiple lines?

From Dev

How can I use command line arguments in Angularjs Protractor?

From Dev

How can I use spaces in systemd command line arguments?

From Dev

How can I call a function in a vbscript with command line arguments?

From Dev

How can I pass command-line arguments to a Erlang program?

From Dev

How can I escape arguments passed in bash script command line

From Dev

How can I run an application with command line arguments in Mac OS

From Dev

How can I specify command line arguments using pipes in Linux?

From Dev

How can I pass command line arguments to sed?

From Dev

How can I pass all command line arguments to a program in a Makefile?

From Dev

How can i use asterisk(*) in command line arguments in groovy?

From Dev

How to start a program with command line arguments on Windows' cmd with 'start' command?

From Dev

cURL for Windows: how can I send multiple line command in a console?

From Dev

How can I start octave from the command line and not the GUI?

From Dev

How can I make a Windows Form pass a command to the console/command line?

From Dev

How can I learn the command which will start an application in Unity's 'System Settings' from the command line?

From Dev

How can I learn the command which will start an application in Unity's 'System Settings' from the command line?

From Dev

How to start MingW Console (GitBash) from Command Line on Windows?

From Dev

How can I start a Node.js app with forever on a port that I set on the command-line?

From Dev

How I can I start a powershell command line from a batch file and enforce it to stay open?

From Dev

How can I get command line arguments(sys.argv) in python

From Dev

How can I get a Perl script to accept parameters from both STDIN and command line arguments?

From Dev

How can I specify Maven command line arguments system wide on Windows?

From Dev

How can I pass command line arguments to a script through Dash? (14.04)

From Dev

How can I get command line arguments from java equinox bundle?

From Dev

How can I get a Perl script to accept parameters from both STDIN and command line arguments?

Related Related

  1. 1

    How can I pass "start date" and "end date" as command line arguments in Python

  2. 2

    How can i ignore a character at start/end of the match, i am like tryin to find command-line arguments

  3. 3

    How can I parse command line arguments in PHP?

  4. 4

    How can i comment command line arguments on multiple lines?

  5. 5

    How can I use command line arguments in Angularjs Protractor?

  6. 6

    How can I use spaces in systemd command line arguments?

  7. 7

    How can I call a function in a vbscript with command line arguments?

  8. 8

    How can I pass command-line arguments to a Erlang program?

  9. 9

    How can I escape arguments passed in bash script command line

  10. 10

    How can I run an application with command line arguments in Mac OS

  11. 11

    How can I specify command line arguments using pipes in Linux?

  12. 12

    How can I pass command line arguments to sed?

  13. 13

    How can I pass all command line arguments to a program in a Makefile?

  14. 14

    How can i use asterisk(*) in command line arguments in groovy?

  15. 15

    How to start a program with command line arguments on Windows' cmd with 'start' command?

  16. 16

    cURL for Windows: how can I send multiple line command in a console?

  17. 17

    How can I start octave from the command line and not the GUI?

  18. 18

    How can I make a Windows Form pass a command to the console/command line?

  19. 19

    How can I learn the command which will start an application in Unity's 'System Settings' from the command line?

  20. 20

    How can I learn the command which will start an application in Unity's 'System Settings' from the command line?

  21. 21

    How to start MingW Console (GitBash) from Command Line on Windows?

  22. 22

    How can I start a Node.js app with forever on a port that I set on the command-line?

  23. 23

    How I can I start a powershell command line from a batch file and enforce it to stay open?

  24. 24

    How can I get command line arguments(sys.argv) in python

  25. 25

    How can I get a Perl script to accept parameters from both STDIN and command line arguments?

  26. 26

    How can I specify Maven command line arguments system wide on Windows?

  27. 27

    How can I pass command line arguments to a script through Dash? (14.04)

  28. 28

    How can I get command line arguments from java equinox bundle?

  29. 29

    How can I get a Perl script to accept parameters from both STDIN and command line arguments?

HotTag

Archive