2013-06-21 5 views
8

토르와 Ruby에 익숙하지 않으며 빌드 스크립트 내에서이 스크립트를 사용하려고합니다. Rake를 대체 할 수 있다고합니다 (따라서 Make). 그러나 짧은 재판 후에는 반환되는 오류 상태에 대해 혼란 스럽습니다. 나는 빨리 wiki를 훑어 보았지만 그에 대한 언급은 보지 못했다. 단지 최초의 "간단한 예"로오류 발생시 Ruby/Thor 종료 상태

, 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 

I 시도 목적에 따라 잘못된 명령

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으로 종료되었습니다 ... 그럼에도 불구하고 그것을 사용하지 않으려한다는 것을 의미합니다 (n on-ruby) 스크립트를 사용하거나 종료해야만 스크립트가 계속 실행됩니다. 후속 오류는 분석하기 어려울 수 있습니다.

  • 오류의 경우에는 기본적으로 비 제로 상태를 확인하는 쉬운 방법이 있나요 (설정 파일 등) :

    는 내 질문에 따라서, 뭔가 빠진해야합니까?

  • 그렇지 않다면 제대로하려면 어떻게해야합니까?

고맙습니다.

답변

2

을 들러의 솔루션을 기반으로 (많은 감사 @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 

건배. 추신 : 토르에 이런 점이 많이 있습니까? 그것이 예상되는 행동이라면, 그것의 목적/철학은 나의 프로젝트의 요구와 양립 할 수 없다. 해킹은 신뢰할만한 해결책이 아니다.

1

좋은 질문입니다. 나는 또한 프로젝트에 사용할 thor을 찾을 때 이것을 알아 차렸다. 지금까지 내가 예상 할 수있는 행동이라고 말할 수 있습니다. 이 pull request for bundler에는 적절한 해결책이 있습니다. 그들은 내 측면에 대한 조사를 오류를 잡아 설정 적절한 종료 상태

# 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 1 
+0

+1 해킹이 발생했기 때문에 (내 답변 참조), 직접 사용할 수는 없지만 특히 Ruby 외부의 솔루션으로는 유용합니다. – eruve

18

나는 이것이 이미 답변되었지만 이것이 더 좋은 대답이라고 생각하므로 어쨌든 그것을 기여할 것이라고 생각합니다.

Thor에는 오류로 인해 0이 아닌 종료 코드가 발생하도록 동작을 변경하는 데 사용할 수있는 방법이 있습니다. 그것은 잘 문서화되지 않았습니다 (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입니다. 나는 왜 누군가가 그걸 개인적으로 행동하기를 바랄 지 모르겠다. Thor issue 244도이 문제를 해결합니다.

+0

허용 된 답변이어야합니다. –

관련 문제