Class: EditorConfigGenerator::ArgumentParser
- Inherits:
-
Object
- Object
- EditorConfigGenerator::ArgumentParser
- Defined in:
- lib/editorconfig/argument_parser.rb
Overview
Parses the arguments passed to the Editor Configurator
Class Method Summary collapse
- .ask_charset(options) ⇒ Object
- .ask_end_of_line(options) ⇒ Object
- .ask_file_type(options) ⇒ Object
- .ask_indent_size(options) ⇒ Object
- .ask_indent_style(options) ⇒ Object
- .ask_insert_newline(options) ⇒ Object
- .ask_question(question, valid_answers, default_value = nil) ⇒ Object
- .ask_trailing_whitespace(options) ⇒ Object
- .ask_user(options) ⇒ Object
- .parse(args) ⇒ Object
- .parse_options(options) ⇒ Object
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() [: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() [: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() [: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() [: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() [: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() [: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() [: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() ask_file_type() ask_indent_style() ask_indent_size() ask_end_of_line() ask_charset() ask_trailing_whitespace() ask_insert_newline() 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 || . = "Generates a .editorconfig file with settings of your choosing. \nUsage: editorconfig generate " () 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.() option_prompt = 'Generate an editorconfig file.' .on('-g', '--generate', option_prompt) do |_answer| configs = [] = {} [:root] = ask_question('Is this for the topmost .editorconfig file? (y/n) [y]', %w(y n), 'y') ask_questions() 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(, configs) loop do add_another_config = ask_user() e = EditorConfig.new() configs.push(e) = {} (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 (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 |