Class: Djin::RootCliParser

Inherits:
Object
  • Object
show all
Defined in:
lib/djin/root_cli_parser.rb

Overview

This class is responsible to handle options that must be evaluated

before the load of tasks in djin file(eg: djin.yml)

Class Method Summary collapse

Class Method Details

.parse!(args = ARGV) ⇒ Object



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/djin/root_cli_parser.rb', line 8

def parse!(args = ARGV)
  options = {}

  # TODO: Find a better way to handle -f/--file option,
  #       throw, catch and delete in ARGV are necessary
  #       to only remove the -f/--file option
  #       and bypass everything else to Dry::CLI
  catch(:root_cli_exit) do
    OptionParser.new do |opts|
      opts.on('-f FILE', '--file FILE') do |v|
        options[:files] ||= []
        options[:files] << v
      end

      opts.on('-h', '--help') do
        throw :root_cli_exit
      end

      opts.on('--all') do
        throw :root_cli_exit
      end
    end.parse(args)
  end

  remove_file_args!(args)
  options
end

.remove_file_args!(args) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/djin/root_cli_parser.rb', line 36

def remove_file_args!(args)
  file_option = ['-f', '--file']
  args_indexes_to_remove = args.each_with_index.map do |value, index|
    index if (file_option.include?(args[index - 1]) && index.positive?) || file_option.include?(value)
  end.compact

  args_indexes_to_remove.reverse.each { |index| args.delete_at(index) }
end