簡體   English   中英

可以在不調用Thor的情況下調用可執行Thor驅動的腳本嗎?

[英]Possible to call executable Thor-powered script without calling thor?

我有一個基於Thor的Ruby腳本,但是我想把它作為一個寶石部署在人們的bin目錄中,人們可以在不需要做thor mytool情況下進行thor 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

您可能會發現這有用: https//github.com/lastobelus/cleanthor

我想為gem創建一個基於thor的可執行文件,使用命名空間子命令,但是根據正常的ruby gem lib / mygem / * / .rb結構組織任務文件。

使腳本可執行

chmod +x mytool

並在mytool #!/usr/bin/env thor mytool #!/usr/bin/env thormytool的第一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM