Class: Sijka::SijkaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sijka/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv, stdin) ⇒ SijkaParser

Returns a new instance of SijkaParser.



5
6
7
8
# File 'lib/sijka/parser.rb', line 5

def initialize(argv, stdin)
  @argv = argv
  @stdin = stdin
end

Instance Method Details

#parse_flagsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sijka/parser.rb', line 10

def parse_flags
  options = {}
  OptionParser.new do |opts|
    opts.banner = 'Usage: sijka [name] [-h] [-f sijkafile] [-l]'
    opts.on('-l', 'List available sijka files') do |_|
      options['list'] = true
    end
    opts.on('-f SIJKAFILE', 'Specify a sijka file') do |sijkafile|
      options['sijkafile'] = sijkafile
    end
    opts.on('-r', 'Random file') do |_|
      options['sijkafile'] = Sijka::FILE_LIST.sample
    end
  end.parse!
  options
end

#parse_messageObject



27
28
29
30
31
32
33
# File 'lib/sijka/parser.rb', line 27

def parse_message
  if @argv.any?
    @argv.join(' ')
  else
    @stdin.tty? ? '' : @argv.read.chomp
  end
end