How to Run Thor Tasks From Your Ruby Scripts

If you have a thor task defined in some file like i do:

Normally, you would invoke this thor task newgem from the command-line like this:

$ thor newgem some_name --test-framework rspec

But what if you need to do that from another ruby script?

Well, you can do so by passing the arguments directly, like this:

1
2
args = ["some_name", "--test-framework", "rspec"]
Newgem.start(args)

Using start is a way to go, but thor documentation suggest using invoke() instead, like this:

1
2
3
4
args = ["some_name"]
opts = {:test_framework => 'rspec'}
script = Newgem.new(args, opts)
script.invoke_all

Good look with your ruby scripting!

Comments