Method: Toolshed::Commands::Base.parse

Defined in:
lib/toolshed/commands/base.rb

.parse(command_class, cli_options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength, Metrics/CyclomaticComplexity



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/toolshed/commands/base.rb', line 11

def self.parse(command_class, cli_options = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength, Metrics/CyclomaticComplexity
  options = {}
  options_parser = OptionParser.new do |opts|
    opts.banner = cli_options[:banner] if cli_options[:banner]
    cli_options[:options].each do |option_name, option_variables|
      letter_map = ('a'..'z').map { |letter| letter }.delete_if { |letter| letter == 'h' }
      short_on = (option_variables[:short_on]) ? option_variables[:short_on] : letter_map[rand(letter_map.length)] # rubocop:disable Metrics/LineLength
      on = (option_variables[:on]) ? option_variables[:on] : "--#{option_name.to_s.split('_').join('-')} [ARG]" # rubocop:disable Metrics/LineLength
      opts.on(short_on, on) do |opt|
        value = (option_variables[:default].nil?) ? opt : option_variables[:default]
        options.merge!(option_name => value)
      end
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end
  options_parser.order! if options_parser
  begin
    cli = Toolshed::CLI.new
    cli.execute(command_class, ARGV, options)
  rescue Toolshed::CommandNotFound => e
    Toolshed.logger.fatal e.message
    Toolshed.die
  end
end