Stand-alone cli that loads a rails app?

I am trying to write a standalone cli app that loads a rails app and uses its code (orm, etc), but handles its own args processing. This article is a good overview of how command line arguments are second class citizens with rake tasks. What I would like to do is manually load the rails app/env/config as part of the execution of my cli, so I can provide a decent cli interface for this tool.

Can anyone point to a good, self-contained example of code that does the proper initialization for this purpose? Like, the code that runs to setup rails console, in one place. Thanks for any assistance you can provide!

Stand-alone cli that loads a rails app?

3 comments · 100% upvoted · other discussions

comments

ralfv

You can run a custom script via:

rails runner myscript.rb myargument

Though it should suffice to use

require 'config/environment'

In your script directly (if said script is located in Rails.root)

localhostdotdev

``` require 'config/environment'

=> cannot load such file -- config/environment

require_relative "config/environment"

works

```

jstrong [OP]

thanks! all examples of runner I had found passed the string of a code path (inside the rails app), but if I can pass the filename that would work well, I think.