Module: Parser

Defined in:
lib/day/parser.rb

Overview

DayRB Parser Module

Scans and validates ARGV inputs and builds the opts hash. (Performs argument validation, and checks that a specified task really exists.)

Class Method Summary collapse

Class Method Details

.parse_options(config) ⇒ Object

Parse ARGV into opts hash

Parameters:

  • config (Configuration)

    Entire configuration object, needed for task lookups.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/day/parser.rb', line 23

def parse_options(config)
  opts = {}
  @config = config

  opts[:operation] = case ARGV.first
  when nil
    :print
  when "-a", "-A"
    opts[:all] = true
    :print
  when "clear", "c"
    :clear
  when "delete", "rm"
    :delete
  when "info", "i"
    :print_info
  when "help"
    :print_help
  when "version"
    :print_version
  else
    handle_non_command(ARGV.first) # could either be a new task or switch to an existing one
  end

  opts[:task] = case opts[:operation]
  when :clear, :print_info
    check_for_second_argument
  when :delete
    demand_second_argument
  when :switch
    task = lookup_task(ARGV.first)
    if task && @config.context == task
      opts[:operation] = :leave
      nil
    elsif task
      task
    else
      raise ArgumentError, E_NO_SUCH_TASK
    end
  when :new
    ARGV.first
  end

  if opts[:operation] == :new
    opts = handle_new_task(opts)
  end

  opts.delete_if { |k, v| v.nil? }
  return opts
end