Class: Singer::OptionParsing
- Inherits:
-
Object
- Object
- Singer::OptionParsing
- Defined in:
- lib/singer/option_parsing.rb
Overview
extracts what we need from ARGV
Constant Summary collapse
- DEFAULT_TEMPLATE_NAME =
'tdd'.freeze
Class Method Summary collapse
- .columnize_hash(hash) ⇒ Object
-
.configure(argvies) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- .encase_name(name) ⇒ Object
- .list_all_templates ⇒ Object
- .list_variables ⇒ Object
- .show_paths ⇒ Object
Class Method Details
.columnize_hash(hash) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/singer/option_parsing.rb', line 44 def self.columnize_hash(hash) first_column_width = hash.keys.map(&:length).max hash.each do |k, v| puts "#{k.to_s.ljust(first_column_width)} #{v}" end end |
.configure(argvies) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
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 |
# File 'lib/singer/option_parsing.rb', line 6 def self.configure(argvies) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength = {} OptionParser.new do |parser| parser. = " Usage: \#{$PROGRAM_NAME} [OPTION]... [TEMPLATE_NAME] PROJECT_NAME\n Generates a project from a multi-file template.\n PROJECT_NAME can be in snake_case or CamelCase.\n\n Options:\n USAGE\n parser.on('--list-templates', 'list all available templates') do\n list_all_templates\n Kernel.exit\n end\n parser.on('--show-paths', 'show paths used to load files') do\n show_paths\n Kernel.exit\n end\n parser.on('--list-variables', 'list all variables available to templates') do\n list_variables\n Kernel.exit\n end\n end.parse!(argvies, into: options)\n\n raise NameMissingError, 'Missing mandatory argument PROJECT_NAME' if argvies.empty?\n\n encase_name(argvies.pop)\n CONFIGURATION.template_name = argvies.shift || DEFAULT_TEMPLATE_NAME\nend\n" |
.encase_name(name) ⇒ Object
55 56 57 58 59 |
# File 'lib/singer/option_parsing.rb', line 55 def self.encase_name(name) CONFIGURATION.project_name_original = name CONFIGURATION.project_name_snakecase = name.gsub(/(\w)([A-Z])/){ "#{$1}_#{$2}" }.gsub(/_+/, '_').downcase CONFIGURATION.project_name_camelcase = CONFIGURATION.project_name_snakecase.split('_').map(&:capitalize).join end |
.list_all_templates ⇒ Object
36 37 38 |
# File 'lib/singer/option_parsing.rb', line 36 def self.list_all_templates columnize_hash(Template.all.transform_values(&:path)) end |
.list_variables ⇒ Object
51 52 53 |
# File 'lib/singer/option_parsing.rb', line 51 def self.list_variables puts CONFIGURATION.members end |
.show_paths ⇒ Object
40 41 42 |
# File 'lib/singer/option_parsing.rb', line 40 def self.show_paths columnize_hash(%w[singer_config_dir templates_from_user templates_from_gem].to_h{ [_1, Paths.send(_1)] }) end |