我是Thor(和Ruby)的新手,我正在考虑在构建脚本中使用它,因为它说它可以替代Rake(因此对Make).但经过短暂的试用后,我对它返回的错误状态感到困惑.我很快就浏览了维基,但没有看到任何提及.
只有第一个"简单示例",test.thor:
class Test < Thor
desc "example", "an example task"
def example
puts "I'm a thor task!"
end
end
Run Code Online (Sandbox Code Playgroud)
版本号:
eruve>thor version
Thor 0.18.1
Run Code Online (Sandbox Code Playgroud)
我尝试了以下,故意错误的命令:
eruve>ruby --version; thor test:example badarg; echo exit status: $?
ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin10.8.0]
ERROR: thor example was called with arguments ["badarg"]
Usage: "thor test:example".
exit status: 0
Run Code Online (Sandbox Code Playgroud)
所以,有一个错误,但它仍以状态0退出...意味着我宁愿不在(非ruby)脚本中使用它,否则脚本将继续运行,即使它应该终止.后续错误可能难以分析.
我必须遗漏一些东西,因此我的问题是:
如果出现错误(配置文件等),是否有一种简单的方法可以默认获得非零状态?
如果没有,我应该做些什么才能做到正确?
谢谢.
Zom*_*Dev 20
我知道这已经回答了,但我认为这是一个更好的答案所以我认为无论如何我都会贡献它.
Thor有一种方法可以用来改变行为,因此错误会导致退出代码非零.它没有记录得很好(恕我直言).
class Test < Thor
def self.exit_on_failure?
true
end
desc "example", "an example task"
def example
puts "I'm a thor task!"
end
end
Run Code Online (Sandbox Code Playgroud)
这个的默认值是莫名其妙的false.我不知道为什么有人会希望它表现得像个人一样.Thor问题244也解决了这个问题.
基于bundler的解决方案(非常感谢@fontno),以及我这边的更多调查,这里有一个 hack,以便让它与普通的 shell 一起工作。警告:它并不优雅,打印出异常堆栈垃圾,但我认为这是可行的(请毫不犹豫地告诉我其他情况)。
class Thorough < Thor
ENV["THOR_DEBUG"] = "1"
check_unknown_options!
private
def subcommand(*_) super subcommand(*_)
rescue Thor::Error => e
$stderr.puts e.message
exit 1
end
end
class Test < Thor#ough
desc "example", "an example task"
def example
puts "I'm a thor task!"
end
end
Run Code Online (Sandbox Code Playgroud)
如上所写,它的行为与以前相同(我相信)。现在,在删除#from后Thor#ough,如果 Thor 引发了 ,它应该以状态 1 退出Error,从而允许从非 ruby shell 等进行一些控制。
eruve>thor test:example badarg; echo $?
/Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/base.rb:482:in `handle_argument_error': ERROR: thor example was called with arguments ["badarg"] (Thor::InvocationError)
Usage: "thor test:example".
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/command.rb:35:in `rescue in run'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/command.rb:21:in `run'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor.rb:363:in `dispatch'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/runner.rb:36:in `method_missing'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/command.rb:29:in `run'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/command.rb:128:in `run'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/invocation.rb:120:in `invoke_command'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor.rb:363:in `dispatch'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/lib/thor/base.rb:439:in `start'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/gems/thor-0.18.1/bin/thor:6:in `<top (required)>'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/bin/thor:23:in `load'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/bin/thor:23:in `<main>'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
from /Users/eruve/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'
1
eruve>thor test:example; echo $?
I'm a thor task!
0
eruve>thor test:example badarg 2>/dev/null; echo $?
1
Run Code Online (Sandbox Code Playgroud)
干杯。PS:我想知道《雷神》中有很多这样的陷阱吗?如果这是预期的行为,那么它的目的/理念与我的项目的需求不相容……黑客并不是一个可靠的解决方案。
| 归档时间: |
|
| 查看次数: |
4237 次 |
| 最近记录: |