class Djin::RootCliParser

This class is responsible to handle options that must be evaluated before the load of tasks in djin file(eg: djin.yml)

Public Class Methods

parse!(args = ARGV) click to toggle source
# 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
    parser(options).parse(args)
  end

  remove_file_args!(args)
  options
end

Private Class Methods

parser(options) click to toggle source
# File lib/djin/root_cli_parser.rb, line 25
def parser(options)
  OptionParser.new do |opts|
    opts.on('-f FILE', '--file FILE') do |v|
      options[:files] ||= []
      options[:files] << v
    end

    opts.on('-v', '--version') do
      throw :root_cli_exit
    end

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

    opts.on('--all') do
      throw :root_cli_exit
    end
  end
end
remove_file_args!(args) click to toggle source
# File lib/djin/root_cli_parser.rb, line 46
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