Class: Furigana::CLI

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/furigana/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



10
11
12
13
# File 'lib/furigana/cli.rb', line 10

def initialize
  @settings = OpenStruct.new
  @settings.format = :text
end

Instance Method Details

#parse_optionsObject



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

def parse_options
  OptionParser.new do |opts|
    opts.banner = "Usage: furigana [options] [file]"

    opts.on("--text", "Add furigana and output text (default)") do
      @settings.format = :text
    end
    opts.on("--html", "Add furigana and output HTML") do
      @settings.format = :html
    end
    opts.on("--yomikata", "Output yomikata only") do
      @settings.format = :yomikata
    end
    opts.on("--json", "Add furigana and output JSON") do
      @settings.format = :json
    end
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
    opts.on_tail("--version", "Show version") do
      puts Furigana::VERSION
      exit
    end
  end.parse!
end

#startObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/furigana/cli.rb', line 42

def start
  parse_options

  input = ARGF.read
  case @settings.format
  when :text
    puts Formatter::Text.new(input, Reader.new.reading(input)).render
  when :html
    puts Formatter::HTML.new(input, Reader.new.reading(input)).render
  when :yomikata
    puts Formatter::Yomikata.new(input, Reader.new.reading(input)).render
  when :json
    puts Formatter::JSON.new(input, Reader.new.reading(input)).render
  end
end