This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-from-stdin
executable file
·62 lines (46 loc) · 1.91 KB
/
table-from-stdin
1
2
3
4
5
6
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
require 'optparse'
require_relative File.join('..', 'lib', 'snmp_table_viewer')
def parse_command_line
options = {
SNMPTableViewer::Formatter::Table => {
transpose: false,
},
headings: [],
formatter: SNMPTableViewer::Formatter::Table,
}
parser = OptionParser.new do |opts|
opts.banner = "Usage: table-from-stdin [options]"
opts.on('-h', '--help', '-?', 'Prints this help.') do
puts opts
exit
end
opts.on('--headings HEADINGS', Array, 'Headings to use in the table (e.g. "A, Bc, D").') do |v|
options[:headings] += v.map(&:strip)
end
opts.on('--headings-for TYPE', String, "Use headings for this table type (#{SNMPTableViewer::HEADINGS_FOR.keys.join('|')}).") do |v|
options[:headings] += SNMPTableViewer::HEADINGS_FOR[v] || []
end
opts.on('--format FORMAT', String, "How to format the output (#{SNMPTableViewer::FORMATTERS.keys.join('|')}) (default table).") do |v|
options[:formatter] = SNMPTableViewer::FORMATTERS[v.downcase]
end
opts.on('--converter CONVERTER', String, "A converter to run on the data before formatting (#{SNMPTableViewer::CONVERTERS.keys.join('|')}).") do |v|
options[:converter] = SNMPTableViewer::CONVERTERS[v.downcase]
end
opts.separator "\nTable formatter options:"
opts.on('--[no-]transpose-table', 'Transpose the output table (swap rows and columns).') do |v|
options[SNMPTableViewer::Formatter::Table][:transpose] = v
end
end # create parser
parser.parse!(ARGV)
options
end
options = parse_command_line
data = STDIN.readlines.map(&:strip)
data = SNMPTableViewer::Fetcher.from_array(data)
converter = options[:converter]
data = converter.convert(data) unless converter.nil?
formatter = options[:formatter]
formatter_options = options[formatter] || {}
formatter = formatter.new(data: data, headings: options[:headings])
puts formatter.output(**formatter_options)