ruby – 可以在不调用Thor的情况下调用可执行的Thor驱动的脚本吗?

前端之家收集整理的这篇文章主要介绍了ruby – 可以在不调用Thor的情况下调用可执行的Thor驱动的脚本吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个基于Thor的 Ruby脚本,但是我想将它作为一个宝石部署在人们的bin目录中,人们可以在不需要做thto mytool的情况下进行打击.

所以相反他们只是使用mytool

这可能吗?

我知道有可能使用vanilla optparse,但如果可能,我宁愿使用Thor.

更新:这是我根据Thor页面上的示例使用的代码,但我收到以下错误

#!/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

错误

/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

解决方法

做shebang线
#!/usr/bin/env ruby

然后在脚本的末尾添加

App.start

猜你在找的Ruby相关文章