【问题标题】:Possible to call executable Thor-powered script without calling thor?可以在不调用 thor 的情况下调用可执行的 Thor 驱动的脚本吗?
【发布时间】:2011-04-04 19:48:56
【问题描述】:

我有一个基于 thor 的 Ruby 脚本,但我想将它作为 gem 部署在人们的 bin 目录中,人们无需执行 thor mytool 即可访问。

所以他们只会使用mytool

这可能吗?

我知道原版 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': nil:NilClass (NoMethodError) 的未定义方法`start' 来自 /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

【问题讨论】:

    标签: ruby command-line gem thor


    【解决方案1】:

    制作shebang线

    #!/usr/bin/env ruby
    

    然后在脚本末尾添加

    App.start
    

    【讨论】:

    • 这很好用。摘要:调用文件,使其可执行,将 bang 行更改为使用 Ruby,如上,将 App.start 添加到末尾。赢了!
    • 这是在哪里记录的?我不得不在整个网络上搜索示例以找出 shebang-start 技巧。
    • 这不是真的。 shebang 是制作可执行脚本的标准方法。混入 Thor 子类的“start”类方法记录在 rdoc.info/gems/thor/0.14.6/Thor/Base/ClassMethods:start。总的来说,我确实同意 Thor 的文档有点参差不齐。
    【解决方案2】:

    您可能会觉得这很有帮助:https://github.com/lastobelus/cleanthor

    我想为 gem 提供一个基于 thor 的可执行文件,带有命名空间的子命令,但根据正常的 ruby​​ gem lib/mygem/*/.rb 结构组织任务文件。 p>

    【讨论】:

      【解决方案3】:

      使脚本可执行

      chmod +x mytool
      

      并将#!/usr/bin/env thor 设为mytool 的第一行。

      【讨论】:

      • 我尝试了这个建议,这很好,但现在我遇到了我添加到问题中的错误。
      • 这似乎是一个单独的问题,更具体到你的代码和雷神。或许您应该针对这个新问题提出一个更适合的新问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多