【问题标题】:How to make rubocop output nothing when there are no warnings or offenses?没有警告或冒犯时如何使 rubocop 不输出任何内容?
【发布时间】:2015-01-20 15:22:06
【问题描述】:

我想要 rubocop,但如果没有违规,它不会产生任何输出。我已经使用了记录在案的命令行选项,并快速浏览了配置选项和源代码,但没有看到任何有希望的东西。

我最接近的是rubocop -f o,但即使这样也会产生一个摘要行:

$ rubocop -f o

--
0  Total

关于如何在干净、成功的运行中抑制输出的任何想法?

【问题讨论】:

  • 为什么不通过sed 管道输出并让它剥离"--\n0 Total"

标签: ruby suppress-warnings suppress rubocop


【解决方案1】:

我相信没有预定义的选项可以满足您的要求。我认为最好的选择是实现具有所需行为的自定义格式化程序并使用它来运行 RuboCop。作为一个超级简单的示例,请考虑以下受 RuboCop 的 SimpleTextFormatter 启发的示例:

# my_rubocop_formatter.rb
class MyRuboCopFormatter < RuboCop::Formatter::BaseFormatter
  def started(target_files)
    @total_offenses = 0
  end
  def file_finished(file, offenses)
    unless offenses.empty?
      output.puts(offenses)
      @total_offenses += offenses.count
    end
  end
  def finished(inspected_files)
    puts @total_offenses unless @total_offenses.zero?
  end
end

您可以使用以下命令运行 RuboCop:

rubocop -r '/path/to/my_rubocop_formatter.rb' \ 
        -f MyRuboCopFormatter file_to_check.rb

【讨论】:

  • 我现在正在这样做。我对获得 --require 和 --format 选项来实际调用我的新格式化程序有点犹豫……但我相信我会克服的。感谢您的建议,@toro2k!
猜你喜欢
  • 1970-01-01
  • 2018-11-08
  • 2021-02-06
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多