可以在不调用 thor 的情况下调用由 Thor 驱动的可执行脚本吗?

发布于 2024-09-16 22:18:34 字数 1590 浏览 3 评论 0原文

我有一个基于 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': 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筱果果 2024-09-23 22:18:34

制作 shebang 行

#!/usr/bin/env ruby

,然后在脚本末尾添加

App.start

Make the shebang line

#!/usr/bin/env ruby

and then at the end of your script add

App.start
千柳 2024-09-23 22:18:34

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

我想要一个雷神基于 gem 的可执行文件,具有命名空间子命令,但根据正常的 ruby​​ gem lib/mygem/*/.rb 结构组织任务文件。

You may find this helpful: https://github.com/lastobelus/cleanthor

I wanted to have a thor-based executable for a gem, with namespaced subcommands, but organize the task files according to the normal ruby gem lib/mygem/*/.rb structure.

一城柳絮吹成雪 2024-09-23 22:18:34

使脚本可执行

chmod +x mytool

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

Make the script executable

chmod +x mytool

and make #!/usr/bin/env thor the first line in mytool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文