可以调用可执行的Thor驱动的脚本而不调用thor? [英] Possible to call executable Thor-powered script without calling thor?

查看:244
本文介绍了可以调用可执行的Thor驱动的脚本而不调用thor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于thor的Ruby脚本,但我想将它作为一个gem部署在人们的 bin 目录中,人们可以不必做<$ c $所以他们只是使用 mytool



p>

这是否可能?



我知道可以使用vanilla optparse

更新:这是我使用的代码基于上面的例子。 Thor页面,但我得到以下错误:

 #!/ usr / bin / env thor 
$ b b class App< Thor
map-L=> :list

desc安装APP_NAME,安装一个可用的应用程序
method_options:force => :boolean,:alias => :string
def install(name)
user_alias = options [:alias]
if options.force?
#do something
end
#其他代码
end

desclist [SEARCH],列出所有可用的应用程式,限制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方法`start'for nil:NilClass(NoMethodError)
从/ usr / lib / ruby​​ / gems /1.8/gems/thor-0.14.0/lib/thor/task.rb:22:in`send'
来自/usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib /thor/task.rb:22:in`run'
从/usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/task.rb:108:in`run '
从/usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib/thor/invocation.rb:118:in`invoke_task'
从/ usr / lib / ruby /gems/1.8/gems/thor-0.14.0/lib/thor.rb:246:in'dispatch'
来自/usr/lib/ruby/gems/1.8/gems/thor-0.14.0/lib /thor/base.rb:389:in`start'
从/usr/lib/ruby/gems/1.8/gems/thor-0.14.0/bin/thor:6
从/ usr / bin / thor:19:in'load'
从/ usr / bin / thor:19


解决方案

创建shebang行

 #!/ usr / bin / env ruby​​ 

,然后在脚本的末尾添加

  App.start 


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

解决方案

Make the shebang line

#!/usr/bin/env ruby

and then at the end of your script add

App.start

这篇关于可以调用可执行的Thor驱动的脚本而不调用thor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆