Class: Irctc::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/irctc/command.rb

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object

Return a structure describing the options.



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
35
36
37
38
39
40
41
42
43
44
# File 'lib/irctc/command.rb', line 9

def self.parse(args)
  # The options specified on the cmd line will be collected in *options*.
  # We set default values here.
  @options = OpenStruct.new
  @options.present_date = nil
  @options.future_date = nil

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: irctc OPTIONS"

    opts.separator ""

    opts.on("-p", "--present DATE",
            "Which day's tickets are available for DATE?", 
            "Format(with quotes): 'July 2, 2012'") do |date|
      @options.present_date = Date.parse date
    end

    opts.on("-f", "--future DATE", 
            "When will the counter open for the given DATE?", 
            "Format(with quotes): 'July 13, 2012'") do |date|
      @options.future_date = Date.parse date
    end

    opts.separator ""
    opts.separator "Common options:"

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

  opts.parse!(args)
  @options
end

.validateObject

parse()



46
47
48
49
50
51
52
# File 'lib/irctc/command.rb', line 46

def self.validate
  if @options.present_date.nil? and @options.future_date.nil?
    raise OptionParser::MissingArgument, "See --help."
  else
    true
  end
end