Class: Opener::TreeTagger::CLI

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • options (Hash) (defaults to: {})


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/opener/tree_tagger/cli.rb', line 9

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)

  @option_parser = OptionParser.new do |opts|
    opts.program_name   = 'tree-tagger'
    opts.summary_indent = '  '

    opts.on('-l', '--log', 'Enable logging to STDERR') do
      @options[:logging] = true
    end

    opts.separator "\nExamples:\n\n  cat example.kaf | \#{opts.program_name}    # Basic usage\n  cat example.kaf | \#{opts.program_name} -l # Logs information to STDERR\n    EOF\n  end\nend\n"

Instance Attribute Details

#option_parserObject (readonly)

Returns the value of attribute option_parser.



4
5
6
# File 'lib/opener/tree_tagger/cli.rb', line 4

def option_parser
  @option_parser
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/opener/tree_tagger/cli.rb', line 4

def options
  @options
end

Instance Method Details

#run(input) ⇒ Object

Parameters:

  • input (String)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/opener/tree_tagger/cli.rb', line 33

def run(input)
  option_parser.parse!(options[:args])

  tagger = TreeTagger.new(options)

  stdout, stderr, process = tagger.run(input)

  if process.success?
    puts stdout

    if options[:logging] and !stderr.empty?
      STDERR.puts(stderr)
    end
  else
    abort stderr
  end
end