Ben*_*eys 7 ruby gem command-line thor
我有一个基于Thor的Ruby脚本,但是我希望将它作为一个宝石部署在人们的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
Run Code Online (Sandbox Code Playgroud)
错误:
/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
Sim*_*mon 12
制作shebang线
#!/usr/bin/env ruby
Run Code Online (Sandbox Code Playgroud)
然后在脚本的末尾添加
App.start
Run Code Online (Sandbox Code Playgroud)