我是Thor (和Ruby)的新手,我正在考虑在构建脚本中使用它,因为据说它可以替代Rake (因此可以制造)。然而,经过短暂的试验,我对它返回的错误状态感到困惑。我很快就浏览了wiki,但没有看到任何关于这方面的报道。
只有第一个“简单的例子”,test.thor
class Test < Thor
desc "example", "an example task"
def example
puts "I'm a thor task!"
end
end版本#:
eruve>thor version
Thor 0.18.1我故意尝试了以下错误的命令:
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所以,出现了一个错误,但它仍然以0状态退出.这意味着我不希望在(非红宝石)脚本中使用它,否则脚本将继续运行,即使它应该终止。随后的错误可能难以分析。
我一定是错过了什么,所以我的问题:
谢谢。
发布于 2013-06-22 05:28:20
基于bundler的解决方案(非常感谢@fontno),以及我这方面的更多调查,这里有一个黑客,目的是让它与一个正常的外壳一起工作。警告:它不优雅,打印出异常堆栈垃圾,但我认为这是可行的(请毫不犹豫地告诉我,否则)。
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正如上面所写的,它有着和以前一样的行为(我相信)。现在,在从#中删除Thor#ough之后,如果Thor引发了一个Error,那么它应该以状态1退出,从而允许来自例如非红宝石外壳的一些控制。
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干杯。PS:我想知道,在托尔有很多这样的问题吗?如果这是一个预期的行为,它的目的/哲学是不符合我的项目的需要.黑客不是一个可靠的解决方案。
发布于 2013-08-28 13:57:09
我知道这个问题已经得到了回答,但我认为这是一个更好的答案,所以我想我还是会做出贡献的。
Thor有一个可以用来改变行为的方法,所以错误会导致非零退出代码。它没有很好的记录(IMHO)。
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默认情况下,这是莫名其妙的false。我不知道为什么有人想让它表现得像个人一样。雷神报第244期也解决了这个问题。
更新:从Thor 1.0.0开始,如果不提供自己的弃用警告方法,您将得到一个exit_on_failure?。这是为了处理令人困惑的默认行为。
发布于 2013-06-21 20:34:38
问得好。在查看用于项目的thor时,我也注意到了这一点。据我所知,这是预期的行为。这个bundler有一个有趣的解决方案,可能适合您。
它们启用了thor调试标志,以便捕获错误并设置适当的退出状态。
# bin/bundle
Bundler.with_friendly_errors {
# Set debug flag so we can rescue Thor::error's
# and set the correct exit code.
ENV["THOR_DEBUG"] = "1"
Bundler::CLI.start
}
# friendly_errors
rescue Thor::UndefinedCommandError => e
Bundler.ui.error e.message
exit 15
rescue Thor::Error => e
Bundler.ui.error e.message
exit 1https://stackoverflow.com/questions/17241932
复制相似问题