Class: Tailor::CLI::Options

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

Class Method Summary collapse

Class Method Details

.aboutString

Returns:



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

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:



209
210
211
# File 'lib/tailor/cli/options.rb', line 209

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.



204
205
206
# File 'lib/tailor/cli/options.rb', line 204

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

.create_configObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/tailor/cli/options.rb', line 252

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



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
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
# File 'lib/tailor/cli/options.rb', line 14

def self.parse!(args)
  options = OpenStruct.new
  options.config_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('--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-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('--indentation-spaces NUMBER',
      'Spaces to expect indentation.  (default: 2)') do |c|
      options.style[:indentation_spaces] = c
    end

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

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

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

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

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

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

    opt.on('--spaces-in-empty-braces NUMBER', Integer,
      '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,
      'Spaces to expect after a [.  (default: 0)') do |c|
      options.style[:spaces_after_lbracket] = c
    end

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

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

    opt.on('--spaces-before-rparen NUMBER', Integer,
      '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,
      '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,
      '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,
      '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 |help|
      puts opt
      exit
    end
  end

  opts.parse!(args)
  colorize

  options
end

.rulerString

Returns:



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/tailor/cli/options.rb', line 219

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

.usageString

Returns:



240
241
242
243
244
245
246
247
248
249
250
# File 'lib/tailor/cli/options.rb', line 240

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:



214
215
216
# File 'lib/tailor/cli/options.rb', line 214

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