可以在不调用Thor的情况下调用可执行的Thor驱动脚本吗?
我有一个基于Thor的Ruby脚本,但是我想将它作为一个gem部署在人们的bin
目录中,人们可以在不需要做thor mytool
情况下进行thor mytool
。
所以相反他们只是使用mytool
这可能吗?
我知道有可能使用vanilla optparse
但如果可能,我宁愿使用Thor。
更新:这是我根据Thor页面上的示例使用的代码,但我收到以下错误:
#!/usr/bin/env thor class App :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的未定义方法`start':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:在'load'中 来自/ 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
并将#!/usr/bin/env thor
作为mytool
的第一行。