@@ -5,39 +5,47 @@ require "option_parser"
5
5
module Ameba::Cli
6
6
extend self
7
7
8
- # ameba:disable Metrics/CyclomaticComplexity
8
+ private class Opts
9
+ property config : Path ?
10
+ property version : String ?
11
+ property formatter : Symbol | String | Nil
12
+ property globs : Array (String )?
13
+ property only : Array (String )?
14
+ property except : Array (String )?
15
+ property describe_rule : String ?
16
+ property location_to_explain : NamedTuple (file: String , line: Int32 , column: Int32 )?
17
+ property fail_level : Severity ?
18
+ property stdin_filename : String ?
19
+ property? skip_reading_config = false
20
+ property? rules = false
21
+ property? rule_versions = false
22
+ property? all = false
23
+ property? colors = true
24
+ property? without_affected_code = false
25
+ property? autocorrect = false
26
+ end
27
+
9
28
def run (args = ARGV ) : Nil
10
- opts = parse_args args
11
- location_to_explain = opts.location_to_explain
12
- stdin_filename = opts.stdin_filename
13
- autocorrect = opts.autocorrect?
29
+ opts = parse_args(args)
14
30
15
- if location_to_explain && autocorrect
31
+ if ( location_to_explain = opts.location_to_explain) && opts. autocorrect?
16
32
raise " Invalid usage: Cannot explain an issue and autocorrect at the same time."
17
33
end
18
34
19
- if stdin_filename && autocorrect
35
+ if opts. stdin_filename && opts. autocorrect?
20
36
raise " Invalid usage: Cannot autocorrect from stdin."
21
37
end
22
38
23
- config = Config .load opts.config, opts.colors?, opts.skip_reading_config?
24
- config.autocorrect = autocorrect
25
- config.stdin_filename = stdin_filename
26
-
27
- if globs = opts.globs
28
- config.globs = globs
29
- end
30
- if fail_level = opts.fail_level
31
- config.severity = fail_level
32
- end
33
-
34
- configure_formatter(config, opts)
35
- configure_rules(config, opts)
39
+ config = config_from_opts(opts)
36
40
37
41
if opts.rules?
38
42
print_rules(config.rules)
39
43
end
40
44
45
+ if opts.rule_versions?
46
+ print_rule_versions(config.rules)
47
+ end
48
+
41
49
if describe_rule_name = opts.describe_rule
42
50
unless rule = config.rules.find(& .name.== describe_rule_name)
43
51
raise " Unknown rule"
@@ -57,31 +65,14 @@ module Ameba::Cli
57
65
exit 255
58
66
end
59
67
60
- private class Opts
61
- property config : Path ?
62
- property formatter : Symbol | String | Nil
63
- property globs : Array (String )?
64
- property only : Array (String )?
65
- property except : Array (String )?
66
- property describe_rule : String ?
67
- property location_to_explain : NamedTuple (file: String , line: Int32 , column: Int32 )?
68
- property fail_level : Severity ?
69
- property stdin_filename : String ?
70
- property? skip_reading_config = false
71
- property? rules = false
72
- property? all = false
73
- property? colors = true
74
- property? without_affected_code = false
75
- property? autocorrect = false
76
- end
77
-
78
68
def parse_args (args, opts = Opts .new)
79
69
OptionParser .parse(args) do |parser |
80
70
parser.banner = " Usage: ameba [options] [file1 file2 ...]"
81
71
82
72
parser.on(" -v" , " --version" , " Print version" ) { print_version }
83
73
parser.on(" -h" , " --help" , " Show this help" ) { print_help(parser) }
84
74
parser.on(" -r" , " --rules" , " Show all available rules" ) { opts.rules = true }
75
+ parser.on(" -R" , " --rule-versions" , " Show all available rule versions" ) { opts.rule_versions = true }
85
76
parser.on(" -s" , " --silent" , " Disable output" ) { opts.formatter = :silent }
86
77
parser.unknown_args do |arr |
87
78
case
@@ -99,6 +90,11 @@ module Ameba::Cli
99
90
opts.config = Path [path] unless path.empty?
100
91
end
101
92
93
+ parser.on(" -u" , " --up-to-version VERSION" ,
94
+ " Choose a version" ) do |version |
95
+ opts.version = version
96
+ end
97
+
102
98
parser.on(" -f" , " --format FORMATTER" ,
103
99
" Choose an output formatter: #{ Config .formatter_names} " ) do |formatter |
104
100
opts.formatter = formatter
@@ -160,6 +156,27 @@ module Ameba::Cli
160
156
opts
161
157
end
162
158
159
+ private def config_from_opts (opts )
160
+ config = Config .load opts.config, opts.colors?, opts.skip_reading_config?
161
+ config.autocorrect = opts.autocorrect?
162
+ config.stdin_filename = opts.stdin_filename
163
+
164
+ if version = opts.version
165
+ config.version = version
166
+ end
167
+ if globs = opts.globs
168
+ config.globs = globs
169
+ end
170
+ if fail_level = opts.fail_level
171
+ config.severity = fail_level
172
+ end
173
+
174
+ configure_formatter(config, opts)
175
+ configure_rules(config, opts)
176
+
177
+ config
178
+ end
179
+
163
180
private def configure_rules (config , opts ) : Nil
164
181
case
165
182
when only = opts.only
@@ -225,4 +242,9 @@ module Ameba::Cli
225
242
Presenter ::RuleCollectionPresenter .new.run(rules)
226
243
exit 0
227
244
end
245
+
246
+ private def print_rule_versions (rules )
247
+ Presenter ::RuleVersionsPresenter .new.run(rules)
248
+ exit 0
249
+ end
228
250
end
0 commit comments