Class: TextFSM::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stdout, error: $stderr) ⇒ CLI

Returns a new instance of CLI.



14
15
16
17
18
# File 'lib/textfsm/cli.rb', line 14

def initialize(input: $stdin, output: $stdout, error: $stderr)
  @input = input
  @output = output
  @error = error
end

Class Method Details

.run(argv = ARGV, **streams) ⇒ Object



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

def self.run(argv = ARGV, **streams)
  new(**streams).run(argv)
end

Instance Method Details

#run(argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/textfsm/cli.rb', line 20

def run(argv)
  settings = { format: "json", rows: false, validate: false, help: false, version: false }
  options = option_parser(settings)
  args = options.parse(argv.dup)
  if settings[:help] || settings[:version]
    @output.puts(settings[:help] ? options : VERSION)
    return 0
  end
  raise OptionParser::InvalidArgument, "expected TEMPLATE and optional INPUT" unless (1..2).cover?(args.length)

  parser = Parser.from_file(args.first)
  if settings[:validate]
    @output.puts("Template OK: #{parser.header.join(', ')}")
    return 0
  end
  text = args[1] && args[1] != "-" ? File.read(args[1], encoding: "UTF-8") : @input.read
  rows = parser.parse(text)
  write_result(parser.header, rows, settings)
  0
rescue Error, OptionParser::ParseError, SystemCallError, IOError, ArgumentError => e
  @error.puts("textfsm: #{e.message}")
  2
end