Class: Tailor::CLI::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/tailor/cli/options.rb

Constant Summary collapse

INTEGER_OR_OFF =
/^(\d+|false|off)$/

Class Method Summary collapse

Class Method Details

.aboutString

Returns:



263
264
265
266
267
268
# File 'lib/tailor/cli/options.rb', line 263

def self.about
  <<-ABOUT
  tailor (v#{Tailor::VERSION}).  \t\tA Ruby style checker.
\t\t\t\t\thttp://github.com/turboladen/tailor
  ABOUT
end

Returns:



240
241
242
# File 'lib/tailor/cli/options.rb', line 240

def self.banner
  ruler + about + "\r\n" + usage + "\r\n"
end

.colorizeObject

Sets colors based on –[no-]color. If the terminal doesn’t support colors, it turns colors off, despite the CLI setting.



235
236
237
# File 'lib/tailor/cli/options.rb', line 235

def self.colorize
  Term::ANSIColor.coloring = @output_color ? STDOUT.isatty : false
end

.create_configObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/tailor/cli/options.rb', line 283

def self.create_config
  if File.exists? Dir.pwd + '/.tailor'
    $stderr.puts "Can't create new config; it already exists."
    false
  else
    erb_file = File.expand_path(
      File.dirname(__FILE__) + '/../tailorrc.erb')
    formatters = Tailor::Configuration.default.formatters
    file_list = 'lib/**/*.rb'
    style = Tailor::Configuration::Style.new.to_hash
    default_config_file = ERB.new(File.read(erb_file)).result(binding)
    File.open('.tailor', 'w') { |f| f.write default_config_file }
  end
end

.parse!(args) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/tailor/cli/options.rb', line 15

def self.parse!(args)
  register_custom_option_types
  options = OpenStruct.new
  options.config_file = ''
  options.output_file = ''
  options.formatters = []
  options.show_config = false
  options.style = {}

  opts = OptionParser.new do |opt|
    opt.banner = self.banner
    opt.separator ''
    opt.separator '  ' + ('-' * 73)
    opt.separator ''
    opt.separator 'Config file options:'
    opt.on('-s', '--show-config', 'Show your current config.') do
      options.show_config = true
    end

    opt.on('-c', '--config-file FILE',
      'Use a specific config file.') do |config|
      options.config_file = config
    end

    opt.on('-o', '--output-file FILE',
      'Print result in a output file if using the proper formatter.') do |output|
      options.output_file = output
    end

    opt.on('--create-config', 'Create a new .tailor file') do
      if create_config
        msg = 'Your new tailor config file was created at '
        msg << "#{Dir.pwd}/.tailor"
        $stdout.puts msg
        exit
      else
        $stderr.puts 'Creation of .tailor failed!'
        exit 1
      end
    end

    #---------------------------------------------------------------------
    # Style options
    #---------------------------------------------------------------------
    opt.separator ''
    opt.separator 'Style Options:'
    opt.separator "  (Any option that doesn't have an explicit way of"
    opt.separator '  turning it off can be done so simply by passing'
    opt.separator "  passing it 'false'.)"

    opt.separator ''
    opt.separator '  * Horizontal Spacing:'

    opt.on('--allow-conditional-parentheses BOOL',
      'Check for conditionals wrapped in parentheses?  (default: true)') do |c|
      options.style[:allow_conditional_parentheses] = c
    end

    opt.on('--allow-hard-tabs BOOL',
      'Check for hard tabs?  (default: true)') do |c|
      options.style[:allow_hard_tabs] = c
    end

    opt.on('--allow-trailing-line-spaces BOOL',
      'Check for trailing spaces at the end of lines?',
      '(default: true)') do |c|
      options.style[:allow_trailing_line_spaces] = c
    end

    opt.on('--allow-unnecessary-interpolation BOOL',
      'Check for unnecessary interpolation in strings?',
      '(default: false)') do |c|
      options.style[:allow_unnecessary_interpolation] = c
    end

    opt.on('--allow-unnecessary-double-quotes BOOL',
      'Check for unnecessary use of double quotes?',
      '(default: false)') do |c|
      options.style[:allow_unnecessary_double_quotes] = c
    end

    opt.on('--indentation-spaces NUMBER', INTEGER_OR_OFF,
      'Spaces to expect indentation.  (default: 2)') do |c|
      options.style[:indentation_spaces] = c
    end

    opt.on('--max-line-length NUMBER', INTEGER_OR_OFF,
      'Max characters in a line. (default: 80)') do |c|
      options.style[:max_line_length] = c
    end

    opt.on('--spaces-after-comma NUMBER', INTEGER_OR_OFF,
      'Spaces to expect after a comma.  (default: 1)') do |c|
      options.style[:spaces_after_comma] = c
    end

    opt.on('--spaces-before-comma NUMBER', INTEGER_OR_OFF,
      'Spaces to expect before a comma.  (default: 0)') do |c|
      options.style[:spaces_before_comma] = c
    end

    opt.on('--spaces-after-conditional NUMBER', INTEGER_OR_OFF,
      'Spaces to expect after a conditional.  (default: 1)') do |c|
      options.style[:spaces_after_conditional] = c
    end

    opt.on('--spaces-after-lbrace NUMBER', INTEGER_OR_OFF,
      'Spaces to expect after a {.  (default: 1)') do |c|
      options.style[:spaces_after_lbrace] = c
    end

    opt.on('--spaces-before-lbrace NUMBER', INTEGER_OR_OFF,
      'Spaces to expect before a {.  (default: 1)') do |c|
      options.style[:spaces_before_lbrace] = c
    end

    opt.on('--spaces-before-rbrace NUMBER', INTEGER_OR_OFF,
      'Spaces to expect before a }.  (default: 1)') do |c|
      options.style[:spaces_before_rbrace] = c
    end

    opt.on('--spaces-in-empty-braces NUMBER', INTEGER_OR_OFF,
      'Spaces to expect between a { and }.  (default: 0)') do |c|
      options.style[:spaces_in_empty_braces] = c
    end

    opt.on('--spaces-after-lbracket NUMBER', INTEGER_OR_OFF,
      'Spaces to expect after a [.  (default: 0)') do |c|
      options.style[:spaces_after_lbracket] = c
    end

    opt.on('--spaces-before-rbracket NUMBER', INTEGER_OR_OFF,
      'Spaces to expect before a ].  (default: 0)') do |c|
      options.style[:spaces_before_rbracket] = c
    end

    opt.on('--spaces-after-lparen NUMBER', INTEGER_OR_OFF,
      'Spaces to expect after a (.  (default: 0)') do |c|
      options.style[:spaces_after_lparen] = c
    end

    opt.on('--spaces-before-rparen NUMBER', INTEGER_OR_OFF,
      'Spaces to expect before a ).  (default: 0)') do |c|
      options.style[:spaces_before_rparen] = c
    end

    opt.separator ''
    opt.separator ''

    opt.separator '  * Naming:'

    opt.on('--allow-camel-case-methods BOOL',
      'Check for camel-case method names?', '(default: true)') do |c|
      options.style[:allow_camel_case_methods] = instance_eval(c)
    end

    opt.on('--allow-screaming-snake-case-classes BOOL',
      'Check for classes like "My_Class"?', '(default: true)') do |c|
      options.style[:allow_screaming_snake_case_classes] =
        instance_eval(c)
    end

    opt.separator ''
    opt.separator ''
    opt.separator '  * Vertical Spacing'

    opt.on('--max-code-lines-in-class NUMBER', INTEGER_OR_OFF,
      'Max number lines of code in a class.', '(default: 300)') do |c|
      options.style[:max_code_lines_in_class] = c
    end

    opt.on('--max-code-lines-in-method NUMBER', INTEGER_OR_OFF,
      'Max number lines of code in a method.', '(default: 30)') do |c|
      options.style[:max_code_lines_in_method] = c
    end

    opt.on('--trailing-newlines NUMBER', INTEGER_OR_OFF,
      'Newlines to expect at the end of the file.', '(default: 1)') do |c|
      options.style[:trailing_newlines] = c
    end

    #---------------------------------------------------------------------
    # Common options
    #---------------------------------------------------------------------
    opt.separator ''
    opt.separator 'Common options:'

=begin
    opt.on('-f', '--format FORMATTER') do |format|
      options.formatters << format
    end
=end

    opt.on('--[no-]color', 'Output in color') do |color|
      @output_color = color
    end

    opt.on_tail('-v', '--version', 'Show the version') do
      puts version
      exit
    end

    opt.on_tail('-d', '--debug', 'Turn on debug logging') do
      Tailor::Logger.log = true
    end

    opt.on_tail('-h', '--help', 'Show this message') do |_|
      puts opt
      exit
    end
  end

  opts.parse!(args)
  colorize

  options
end

.register_custom_option_typesObject



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/tailor/cli/options.rb', line 298

def self.register_custom_option_types
  # We need to be able to mark integer options as :off as zero may be a
  # valid value.
  OptionParser.accept(INTEGER_OR_OFF) do |s|
    raise OptionParser::InvalidArgument unless s =~ INTEGER_OR_OFF
    if s == false.to_s || s == 'off'
      :off
    else
      s.to_i
    end
  end
end

.rulerString

Returns:



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/tailor/cli/options.rb', line 250

def self.ruler
  <<-RULER
  _________________________________________________________________________
  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |
  |     |     |     |     |     |     |     |     |     |     |     |     |
  |           |           |           |           |           |           |
  |           1           2           3           4           5           |
  |                                                                       |
  -------------------------------------------------------------------------
  RULER
end

.usageString

Returns:



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/tailor/cli/options.rb', line 271

def self.usage
  <<-USAGE
Usage:  tailor [options] [FILE|DIR|GLOB]

Examples:
tailor
tailor --no-color -d my_file.rb
tailor --config-file tailor_config lib/**/*.rb
tailor --show-config
  USAGE
end

.versionString

Returns:



245
246
247
# File 'lib/tailor/cli/options.rb', line 245

def self.version
  ruler + about + "\r\n"
end