Class: Sredder::ArgParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sredder/arg_parser.rb

Constant Summary collapse

FOLDER_NAMES =
{
  :p =>'Programming',
  :s =>'Project Supervision',
  :r =>'Requirements Engineering',
  :a =>'Software architecture and design',
  :t =>'Testing'
}

Instance Method Summary collapse

Instance Method Details

#optionsObject



15
16
17
# File 'lib/sredder/arg_parser.rb', line 15

def options
  @options ||= defaults
end

#parse(args) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sredder/arg_parser.rb', line 34

def parse(args)

  OptionParser.new do |opts|

    opts.banner = "Usage: sredder [options]"

    opts.on("-t", "--title TITLE",
            "The TITLE field to add to the wrike task") do |title|
      options[:title] = title
    end

    opts.on("-m", "--message MESSAGE",
            "The MESSAGE field to add to the wrike task") do |message|
      options[:message] = message
    end

    opts.on("-h", "--hours N", Float, "The number of HOURS spent on the task") do |n|
      options[:hours] = n
    end

    opts.on("-d", "--date DATE", Time, "The DATE (YY/MM/DD) on which the task was performed (default today)") do |date|
      options[:date] = Util.time_stamp(date)
    end

    opts.on("-f", "--folder FOLDER", FOLDER_NAMES.values, FOLDER_NAMES, "The wrike FOLDER in which to place the task (default #{defaults[:folder]})") do |folder|
      options[:folder] = folder
    end

    opts.on("-g", "--github_id ID#",
            "The Github Pull Request ID to fetch and parse") do |github_id|
      options[:github_id] = github_id
    end

    opts.on_tail("--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("--folders", "Show available folders") do
      puts "The available wrike folders are:"
      puts "\t" + FOLDER_NAMES.values.join(', ')
      puts "\nFor convenience you can use the shortest unambiguous string of characters or one of the following mappings:"
      puts ""
      FOLDER_NAMES.each {|k,v| puts "\t#{k} => #{v}" }
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts Sredder::VERSION
      exit
    end

  end.parse(args)

  options
end

#valid?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
# File 'lib/sredder/arg_parser.rb', line 19

def valid?
  ( options[:title]   &&
    options[:message] &&
    options[:hours]
  ) || (
    options[:hours] &&
    options[:github_id]
  )
end

#validate!Object

Print the usage and quit



30
31
32
# File 'lib/sredder/arg_parser.rb', line 30

def validate!
  parse(['--help']) unless valid?
end