Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --rules switch to the CLI #179

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions spec/ameba/cli/cmd_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ module Ameba::Cli
c.except.should eq %w(RULE1 RULE2)
end

it "defaults rules? flag to false" do
c = Cli.parse_args %w(file.cr)
c.rules?.should eq false
end

it "accepts --rules flag" do
c = Cli.parse_args %w(--rules)
c.rules?.should eq true
end

it "defaults all? flag to false" do
c = Cli.parse_args %w(file.cr)
c.all?.should eq false
Expand Down
20 changes: 18 additions & 2 deletions src/ameba/cli/cmd.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module Ameba::Cli
configure_formatter(config, opts)
configure_rules(config, opts)

if opts.rules?
print_rules(config)
end

runner = Ameba.run(config)

if location = opts.location_to_explain
Expand All @@ -34,6 +38,7 @@ module Ameba::Cli
property except : Array(String)?
property location_to_explain : NamedTuple(file: String, line: Int32, column: Int32)?
property fail_level : Severity?
property? rules = false
property? all = false
property? colors = true
property? without_affected_code = false
Expand All @@ -44,7 +49,8 @@ module Ameba::Cli
parser.banner = "Usage: ameba [options] [file1 file2 ...]"

parser.on("-v", "--version", "Print version") { print_version }
parser.on("-h", "--help", "Show this help") { show_help parser }
parser.on("-h", "--help", "Show this help") { print_help(parser) }
parser.on("-r", "--rules", "Show all available rules") { opts.rules = true }
parser.on("-s", "--silent", "Disable output") { opts.formatter = :silent }
parser.unknown_args do |f|
if f.size == 1 && f.first =~ /.+:\d+:\d+/
Expand Down Expand Up @@ -145,8 +151,18 @@ module Ameba::Cli
exit 0
end

private def show_help(parser)
private def print_help(parser)
puts parser
exit 0
end

private def print_rules(config)
config.rules.each do |rule|
puts \
"#{rule.name.colorize(:white)} " \
"[#{rule.severity.symbol.to_s.colorize(:green)}] - " \
"#{rule.description.colorize(:dark_gray)}"
end
exit 0
end
end