2016-10-13 2 views
2

여러 개의 외부 보석을 사용하는 Thor CLI 응용 프로그램을 만들었습니다. 내가 그것을 실행할 때 나는 내 출력을 어지럽히는 그 보석으로부터 경고 메시지를받습니다. 어떻게 이것을 억제 할 수 있습니까?Thor CLI 앱에서 경고 메시지 표시 안 함

명확한 설명 : 나는 메시지만을 경고를 억제하지만, 여전히 오류 및 puts 결과를 포함 내 응용 프로그램의 표준 출력을 수신 할.

예를 들어, Sinatra 앱에서이 같은 보석을 사용하면 Thor과 마찬가지로 보석 코드에서 나오는 모든 경고 메시지가 표시되지 않습니다. (http://willschenk.com/making-a-command-line-utility-with-gems-and-thor/에서 파생)

우리가 safe_yaml 보석을 필요로하고 있기 때문에, 우리는 명령을 실행할 때마다 우리는 우리의 출력에 다음과 같은 경고를 얻을 수 있습니다이 경우

require 'thor' 
require 'safe_yaml' 

module Socialinvestigator 
    class HammerOfTheGods < Thor 
    desc "hello NAME", "This will greet you" 
    long_desc <<-HELLO_WORLD 

    `hello NAME` will print out a message to the person of your choosing. 

    HELLO_WORLD 
    option :upcase 
    def hello(name) 
     greeting = "Hello, #{name}" 
     greeting.upcase! if options[:upcase] 
     puts greeting 
    end 
    end 
end 

:

/usr/local/lib/ruby/gems/2.3.0/gems/safe_yaml-1.0.4/lib/safe_yaml.rb:28 : 경고 : 메서드를 재정의; 이전 safe_load 삭제 /usr/local/Cellar/ruby/2.3.0/lib/ruby/2.3.0/psych.rb:290 : 경고 : safe_load의 이전 정의는 여기에 있습니다. /usr/local/lib/ruby ​​/ gems/2.3.0/gems/safe_yaml-1.0.4/lib/safe_yaml.rb : 52 : 경고 : 메서드 재정의; 오래된 LOAD_FILE /usr/local/Cellar/ruby/2.3.0/lib/ruby/2.3.0/psych.rb:470을 폐기 : 경고 : LOAD_FILE의 이전의 정의는 여기에 우리가 사용하고있는

했다 다른 보석의 숫자와 우리의 출력을 어수선하게하는 경고의 전체 배열을 받고 ...

+0

놀아볼 코드 예제가 있으면 좋겠습니까? –

+0

[KISS] (https://en.wikipedia.org/wiki/KISS_principle) 해결책 : 경고를 수정하십시오. – Blacksilver

답변

1

우선, 당신은 잠재적으로 당겨 요청을 제출하고 종속성에있는 메시지를 억제하거나 보석 개발자에게 당신을 위해 밖으로

그렇지 않으면, 이것은 내가 전에 사용했던 무엇인가이다 - 그것은 아마 어딘가에서이다 (또는 인터넷 i n 일반) 그러나 기억이 안나요 ...

따라서 기본적으로 STDOUT을 StringIO 개체로 푸시하고 다시 STDOUT으로 푸시하는 silence 메서드를 사용하여 종속성에서 시끄러운 메서드를 래핑합니다.

require 'stringio' 
def silence 
    $stdout = temp_out = StringIO.new 
    yield 
    temp_out.string 
ensure 
    $stdout = STDOUT 
end 

out = silence { run_dependency_method } 
puts out # if in dev mode etc... 
+0

또한보십시오 http://stackoverflow.com/questions/1496019/suppresing-output-to-console-with-ruby –

+0

나 나쁜 나는 명확하지 않았다. 여전히 표준 출력이 필요합니다. 나는 보석에서 나오는 경고 메시지를 원하지 않습니다. 위의 수정 내용보기 ... – Yarin

0

@Yarin,

@ 이스마일의 대답에 최대에 따라, 당신은 단어 "경고"를 포함하는 메시지를 필터링 할 silence 방법 내부의 새로운 "임시"StringIO를 오버로드하여이 솔루션을 확장 할 수 있습니다.

여기서 예이다 :

require 'stringio' 

class FooIO < StringIO 
     def puts s 
       super unless s.start_with? "WARN" 
     end 
end 

def silence 
    $stdout = temp_out = FooIO.new 
    yield 
    temp_out.string 
ensure 
    $stdout = STDOUT 
end 

def foo 
     puts "INFO : Hello World!" 
     puts "WARN : Oops.. This is a warning..." 
     puts "ALRT : EVERYONE IS GOING TO DIE!!!" 
end 

out = silence { foo } 

puts out 
0

루비 출력의 verboseness 정의하는 전역 변수를 갖는다.nilfalse"정상" 모드이며 true 추가 자세한 정보는 (ruby --verbose를 실행하는 것과 몇 가지 추가 런타임 정보를 추가)되고, 어떤 경고를 의미합니다 :

def without_warnings 
    verboseness_level = $VERBOSE 
    $VERBOSE = nil 

    yield 
ensure 
    $VERBOSE = verboseness_level 
end 

# Doesn't show warnings about overwriting the constant 
without_warnings do 
    Foo = 42 
    Foo = 'bar' 
end 

# Still shows normal output 
without_warnings { puts 42 } 

# Still shows and throws errors 
without_warnings { raise 'wtf' } 
어떻게 프로그램을 제어 할 경우

가 실행되면 루비의 -W 플래그 대신 0, 1 또는 2 (이 경우 ruby -W 0) 플래그를 사용할 수 있습니다.