forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.rb
97 lines (80 loc) · 2.37 KB
/
options.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'optparse'
require 'reek/source'
module Reek
class Options
CTX_SORT = '%m%c %w (%s)'
SMELL_SORT = '%m[%s] %c %w'
def self.default_options
{
:format => CTX_SORT,
:show_all => false,
:quiet => false
}
end
@@opts = default_options
def self.[](key)
@@opts[key]
end
def self.parse_args(args)
result = default_options
parser = OptionParser.new { |opts| set_options(opts, result) }
parser.parse!(args)
result
end
def self.set_options(opts, config)
opts.banner = <<EOB
Usage: #{opts.program_name} [options] files...
If no files are given, Reek reads source code from standard input.
See http://wiki.github.com/kevinrutherford/reek for detailed help.
EOB
opts.separator "\nOptions:"
set_all_options(opts, config)
end
def self.parse(args)
@@opts = parse_args(args)
if args.length > 0
return Source.from_pathlist(args)
else
return Source.from_io($stdin, '$stdin')
end
end
private
def self.set_all_options(opts, config)
set_show_all_option(opts, config)
set_help_option(opts)
set_sort_option(config, opts)
set_version_option(opts)
end
def self.set_version_option(opts)
opts.on("-v", "--version", "Show version") do
puts "#{opts.program_name} #{Reek::VERSION}"
exit(0)
end
end
def self.set_show_all_option(opts, config)
opts.on("-a", "--[no-]show-all", "Show all smells, including those masked by config settings") do |opt|
config[:show_all] = opt
end
opts.on("-q", "--quiet", "Suppress headings for smell-free source files") do
config[:quiet] = true
end
end
def self.set_help_option(opts)
opts.on("-h", "--help", "Show this message") do
puts opts
exit(0)
end
end
def self.set_sort_option(config, opts)
opts.on('-f', "--format FORMAT", 'Specify the format of smell warnings') do |arg|
config[:format] = arg unless arg.nil?
end
opts.on('-c', '--context-first', "Sort by context; sets the format string to \"#{CTX_SORT}\"") do
config[:format] = CTX_SORT
end
opts.on('-s', '--smell-first', "Sort by smell; sets the format string to \"#{SMELL_SORT}\"") do
config[:format] = SMELL_SORT
end
end
end
end