Possible to call executable Thor-powered script without calling thor?
Asked Answered
F

3

9

I have a thor-based Ruby script, but I want to deploy it as a gem in people's bin directories that people can hit without having to do thor mytool.

So instead they'd just use mytool

Is this possible?

I know it's possible with vanilla optparse but I'd rather use Thor if possible.

Update: This is the code I'm using based on the example on the Thor page, but I get the error below:

#!/usr/bin/env thor

class App < Thor
  map "-L" => :list

  desc "install APP_NAME", "install one of the available apps"
  method_options :force => :boolean, :alias => :string
  def install(name)
    user_alias = options[:alias]
    if options.force?
      # do something
    end 
    # other code
  end 

  desc "list [SEARCH]", "list all of the available apps, limited by SEARCH"
  def list(search="")
    # list everything
  end 
end

Error:

/usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/runner.rb:34:in `method_missing': undefined method `start' for nil:NilClass (NoMethodError)
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/task.rb:22:in `send'
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/task.rb:22:in `run'
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/task.rb:108:in `run'
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/invocation.rb:118:in `invoke_task'
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor.rb:246:in `dispatch'
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/base.rb:389:in `start'
        from /usr/lib/ruby/gems/1.8/gems/thor-0.14.0/bin/thor:6
        from /usr/bin/thor:19:in `load'
        from /usr/bin/thor:19
Finlay answered 27/8, 2010 at 13:43 Comment(0)
S
13

Make the shebang line

#!/usr/bin/env ruby

and then at the end of your script add

App.start
Stipend answered 1/12, 2010 at 14:18 Comment(3)
This works great. Summary: call the file whatever, make it executable, change the bang line to use Ruby as above, add App.start to the end. Win!Finlay
Where is this documented? I had to search for examples on the whole net to figure out the shebang-start trick.Housemother
It isn't really. The shebang is the standard way of making executable scripts. The "start" class method that gets mixed into your Thor subclass is documented at rdoc.info/gems/thor/0.14.6/Thor/Base/ClassMethods:start. I do agree, in general, that Thor's documentation is a bit spotty though.Stipend
B
1

You may find this helpful: https://github.com/lastobelus/cleanthor

I wanted to have a thor-based executable for a gem, with namespaced subcommands, but organize the task files according to the normal ruby gem lib/mygem/*/.rb structure.

Briefcase answered 7/12, 2012 at 6:17 Comment(0)
A
0

Make the script executable

chmod +x mytool

and make #!/usr/bin/env thor the first line in mytool.

Abby answered 27/8, 2010 at 14:19 Comment(2)
I tried this suggestion, it was good but now I have the error that I added to the question.Finlay
This seems to be a separate issue, more specific to you code and thor. Perhaps you should open a new question more fitting for this new issue?Abby

© 2022 - 2024 — McMap. All rights reserved.