Class: Morsify::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/morsify/cli.rb

Class Method Summary collapse

Class Method Details

.handler(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/morsify/cli.rb', line 36

def self.handler(options)
  puts VERSION if options[:version]

  encode = ->(text) { Telegraph.text_to_morse(text) }
  decode = ->(morse) { Telegraph.morse_to_text(morse) }
  to_cyrillic = ->(morse, lang) { Telegraph.morse_to_text(morse, lang) }
  wave = ->(text) { MorseWave.text_to_wave(text) }

  puts encode.call(options[:encode]) if options[:encode]
  puts decode.call(options[:decode]) if options[:decode] && !options[:ru]

  if options[:decode] && options[:ru]
    puts to_cyrillic.call(options[:decode], options[:ru])
  end

  puts wave.call(options[:wave]) if options[:wave]
end

.parse(_args) ⇒ Object



7
8
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
# File 'lib/morsify/cli.rb', line 7

def self.parse(_args)
  options = {}

  optparse = OptionParser.new do |opts|
    opts.banner = 'Usage: morsify [options]'

    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      exit
    end

    opts.on('-e TEXT', '--encode TEXT', 'Text to Morse') { |o| options[:encode] = o }
    opts.on('-d MORSE_CODE', '--decode MORSE_CODE', 'Morse to Text') { |o| options[:decode] = o }
    opts.on('-c', '--cyrillic') { |o| options[:ru] = o }
    opts.on('-w MORSE_CODE', '--wave MORSE_CODE', 'Morse to WAV File') { |o| options[:wave] = o }
    opts.on('-v', '--version') { |o| options[:version] = o }
  end

  begin
    optparse.parse!
  rescue OptionParser::InvalidOption, OptionParser::MissingArgument
    puts 'Нераспознанный параметр!'
    puts 'По команде «morsify -h» можно получить дополнительную информацию.'
    exit
  end

  options
end