Class: EditorConfigGenerator::ArgumentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/editorconfig/argument_parser.rb

Overview

Parses the arguments passed to the Editor Configurator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ask_charset(options) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/editorconfig/argument_parser.rb', line 33

def self.ask_charset(options)
  options[:charset] = ask_question('What is the character set of the
                                   project? (latin1/utf-8/utf-8-bom
                                   /utf-16be/utf-16le)',
                                   %w(latin utf-8 utf-8-bom
                                      utf-16be utf-16le))
end

.ask_end_of_line(options) ⇒ Object



41
42
43
44
45
# File 'lib/editorconfig/argument_parser.rb', line 41

def self.ask_end_of_line(options)
  options[:end_of_line] = ask_question('What character would you like to
                                       use to represent the end of line?
                                       (lf/cr/ctrlf)', %w(cr lf ctrlf))
end

.ask_file_type(options) ⇒ Object



57
58
59
60
# File 'lib/editorconfig/argument_parser.rb', line 57

def self.ask_file_type(options)
  options[:file_type] = ask_question('What is the filetype for these rules?
                                     [*]', nil, '*')
end

.ask_indent_size(options) ⇒ Object



47
48
49
50
# File 'lib/editorconfig/argument_parser.rb', line 47

def self.ask_indent_size(options)
  options[:indent_size] = ask_question('What size would you like
                                       indentations to be? ', %w(2 4))
end

.ask_indent_style(options) ⇒ Object



52
53
54
55
# File 'lib/editorconfig/argument_parser.rb', line 52

def self.ask_indent_style(options)
  options[:indent_style] = ask_question('What indent style do you use
                                         (space/tabs)', %w(space tabs))
end

.ask_insert_newline(options) ⇒ Object



21
22
23
24
25
# File 'lib/editorconfig/argument_parser.rb', line 21

def self.ask_insert_newline(options)
  options[:insert_final_newline] = ask_question('Would you like to insert
                                                a final newline? (y/n)',
                                                %w(y n))
end

.ask_question(question, valid_answers, default_value = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/editorconfig/argument_parser.rb', line 62

def self.ask_question(question, valid_answers, default_value = nil)
  loop do
    print "#{question}: "
    answer = STDIN.gets.chomp
    return default_value if answer.chomp == ''
    return answer if !valid_answers.nil? && valid_answers.include?(answer)
    return answer if valid_answers.nil?
  end
end

.ask_trailing_whitespace(options) ⇒ Object



27
28
29
30
31
# File 'lib/editorconfig/argument_parser.rb', line 27

def self.ask_trailing_whitespace(options)
  options[:trim_trailing_whitespace] = ask_question('Would you like to trim
                                                    trailing whitespace?
                                                    (y/n)', %w(y n))
end

.ask_user(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/editorconfig/argument_parser.rb', line 9

def self.ask_user(options)
  ask_file_type(options)
  ask_indent_style(options)
  ask_indent_size(options)
  ask_end_of_line(options)
  ask_charset(options)
  ask_trailing_whitespace(options)
  ask_insert_newline(options)
  ask_question('Would you like to add another (file specific) section
               (y/n) [n]', %w(y n), 'n')
end

.parse(args) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/editorconfig/argument_parser.rb', line 72

def self.parse(args)
  option_parser = OptionParser.new do |options|
    options.banner = "Generates a .editorconfig file with settings of your
                      choosing.
                    \nUsage: editorconfig generate "
    parse_options(options)
  end
  option_parser.parse!(args)
end

.parse_options(options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/editorconfig/argument_parser.rb', line 82

def self.parse_options(options)
  option_prompt = 'Generate an editorconfig file.'
  options.on('-g', '--generate', option_prompt) do |_answer|
    configs = []
    options = {}
    options[:root] = ask_question('Is this for the topmost .editorconfig
                                  file? (y/n) [y]', %w(y n), 'y')
    ask_questions(options)
    file_generator = FileGenerator.new(configs)
    file_generator.generate_config_file
  end
end

Instance Method Details

#ask_questions(options, configs) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/editorconfig/argument_parser.rb', line 95

def ask_questions(options, configs)
  loop do
    add_another_config = ask_user(options)
    e = EditorConfig.new(options)
    configs.push(e)
    options = {}
    warn_user_bare_minimum(add_another_config, e)
    break if add_another_config == 'n'
  end
end

#warn_user_bare_minimum(add_another_config, e) ⇒ Object



106
107
108
109
110
# File 'lib/editorconfig/argument_parser.rb', line 106

def warn_user_bare_minimum(add_another_config, e)
  return unless add_another_config == 'n' && e.minimum?
  puts 'Warning: you have selected the bare minimum options for the
          file'
end