tb 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/README +62 -50
  2. data/bin/tb +22 -18
  3. data/lib/tb.rb +35 -19
  4. data/lib/tb/basic.rb +85 -86
  5. data/lib/tb/catreader.rb +33 -116
  6. data/lib/tb/cmd_cat.rb +31 -27
  7. data/lib/tb/cmd_consecutive.rb +45 -35
  8. data/lib/tb/cmd_crop.rb +86 -52
  9. data/lib/tb/cmd_cross.rb +113 -71
  10. data/lib/tb/cmd_cut.rb +49 -44
  11. data/lib/tb/cmd_git_log.rb +193 -0
  12. data/lib/tb/cmd_grep.rb +43 -32
  13. data/lib/tb/cmd_group.rb +63 -39
  14. data/lib/tb/cmd_gsub.rb +53 -43
  15. data/lib/tb/cmd_help.rb +51 -24
  16. data/lib/tb/cmd_join.rb +32 -35
  17. data/lib/tb/cmd_ls.rb +233 -205
  18. data/lib/tb/cmd_mheader.rb +47 -37
  19. data/lib/tb/cmd_nest.rb +94 -0
  20. data/lib/tb/cmd_newfield.rb +29 -33
  21. data/lib/tb/cmd_rename.rb +40 -32
  22. data/lib/tb/cmd_shape.rb +31 -24
  23. data/lib/tb/cmd_sort.rb +46 -25
  24. data/lib/tb/cmd_svn_log.rb +47 -28
  25. data/lib/tb/cmd_tar_tvf.rb +447 -0
  26. data/lib/tb/cmd_to_csv.rb +60 -0
  27. data/lib/tb/cmd_to_json.rb +60 -0
  28. data/lib/tb/cmd_to_pnm.rb +48 -0
  29. data/lib/tb/cmd_to_pp.rb +71 -0
  30. data/lib/tb/cmd_to_tsv.rb +48 -0
  31. data/lib/tb/cmd_to_yaml.rb +52 -0
  32. data/lib/tb/cmd_unnest.rb +118 -0
  33. data/lib/tb/cmdmain.rb +24 -20
  34. data/lib/tb/cmdtop.rb +33 -25
  35. data/lib/tb/cmdutil.rb +26 -66
  36. data/lib/tb/csv.rb +46 -34
  37. data/lib/tb/enum.rb +294 -0
  38. data/lib/tb/enumerable.rb +198 -7
  39. data/lib/tb/enumerator.rb +73 -0
  40. data/lib/tb/fieldset.rb +27 -19
  41. data/lib/tb/fileenumerator.rb +365 -0
  42. data/lib/tb/json.rb +50 -0
  43. data/lib/tb/pager.rb +6 -6
  44. data/lib/tb/pairs.rb +227 -0
  45. data/lib/tb/pnm.rb +23 -22
  46. data/lib/tb/reader.rb +52 -49
  47. data/lib/tb/record.rb +48 -19
  48. data/lib/tb/revcmp.rb +38 -0
  49. data/lib/tb/ropen.rb +74 -57
  50. data/lib/tb/search.rb +25 -21
  51. data/lib/tb/tsv.rb +31 -34
  52. data/sample/excel2csv +24 -20
  53. data/sample/poi-xls2csv.rb +24 -20
  54. data/sample/poi-xls2csv.sh +22 -18
  55. data/sample/tbplot +185 -127
  56. data/test-all-cov.rb +3 -3
  57. data/test-all.rb +1 -1
  58. data/test/test_basic.rb +26 -10
  59. data/test/test_catreader.rb +7 -6
  60. data/test/test_cmd_cat.rb +32 -0
  61. data/test/test_cmd_consecutive.rb +10 -0
  62. data/test/test_cmd_crop.rb +4 -4
  63. data/test/test_cmd_cross.rb +16 -4
  64. data/test/test_cmd_git_log.rb +46 -0
  65. data/test/test_cmd_help.rb +17 -12
  66. data/test/test_cmd_join.rb +21 -1
  67. data/test/test_cmd_ls.rb +3 -4
  68. data/test/test_cmd_mheader.rb +17 -11
  69. data/test/test_cmd_nest.rb +49 -0
  70. data/test/test_cmd_sort.rb +15 -0
  71. data/test/test_cmd_tar_tvf.rb +281 -0
  72. data/test/{test_cmd_csv.rb → test_cmd_to_csv.rb} +35 -21
  73. data/test/{test_cmd_json.rb → test_cmd_to_json.rb} +31 -3
  74. data/test/{test_cmd_pnm.rb → test_cmd_to_pnm.rb} +2 -2
  75. data/test/{test_cmd_pp.rb → test_cmd_to_pp.rb} +4 -4
  76. data/test/{test_cmd_tsv.rb → test_cmd_to_tsv.rb} +4 -4
  77. data/test/{test_cmd_yaml.rb → test_cmd_to_yaml.rb} +3 -3
  78. data/test/test_cmd_unnest.rb +89 -0
  79. data/test/test_cmdtty.rb +19 -13
  80. data/test/test_enumerable.rb +83 -1
  81. data/test/test_fileenumerator.rb +265 -0
  82. data/test/test_json.rb +15 -0
  83. data/test/test_pager.rb +3 -4
  84. data/test/test_pairs.rb +122 -0
  85. data/test/test_pnm.rb +24 -24
  86. data/test/test_reader.rb +35 -13
  87. data/test/test_revcmp.rb +10 -0
  88. data/test/test_tbenum.rb +173 -0
  89. metadata +51 -23
  90. data/lib/tb/cmd_csv.rb +0 -42
  91. data/lib/tb/cmd_json.rb +0 -60
  92. data/lib/tb/cmd_pnm.rb +0 -43
  93. data/lib/tb/cmd_pp.rb +0 -70
  94. data/lib/tb/cmd_tsv.rb +0 -43
  95. data/lib/tb/cmd_yaml.rb +0 -47
data/test/test_reader.rb CHANGED
@@ -79,19 +79,38 @@ class TestTbReader < Test::Unit::TestCase
79
79
  a\tb
80
80
  1\t3
81
81
  End
82
- Tb::Reader.open("csv:#{ic}") {|r|
83
- assert_equal(%w[a b], r.header)
84
- assert_equal([%w[1 3]], r.read_all)
82
+ open(ij="#{d}/j", "w") {|f| f << <<-"End".gsub(/^[ \t]+/, '') }
83
+ [{"a":1,"b":3}]
84
+ End
85
+ Tb.open_reader("csv:#{ic}") {|r|
86
+ header = nil
87
+ all = []
88
+ r.with_header {|h| header = h}.each {|pairs| all << pairs.map {|f, v| v } }
89
+ assert_equal(%w[a b], header)
90
+ assert_equal([%w[1 3]], all)
91
+ }
92
+ Tb.open_reader("tsv:#{it}") {|r|
93
+ header = nil
94
+ all = []
95
+ r.with_header {|h| header = h}.each {|pairs| all << pairs.map {|f, v| v } }
96
+ assert_equal(%w[a b], header)
97
+ assert_equal([%w[1 3]], all)
85
98
  }
86
- Tb::Reader.open("tsv:#{it}") {|r|
87
- assert_equal(%w[a b], r.header)
88
- assert_equal([%w[1 3]], r.read_all)
99
+ Tb.open_reader("json:#{ij}") {|r|
100
+ header = nil
101
+ all = []
102
+ r.with_header {|h| header = h}.each {|pairs| all << pairs.map {|f, v| v } }
103
+ assert_equal(nil, header)
104
+ assert_equal([[1, 3]], all)
89
105
  }
90
- Tb::Reader.open(ic) {|r|
91
- assert_equal(%w[a b], r.header)
92
- assert_equal([%w[1 3]], r.read_all)
106
+ Tb.open_reader(ic) {|r|
107
+ header = nil
108
+ all = []
109
+ r.with_header {|h| header = h}.each {|pairs| all << pairs.map {|f, v| v } }
110
+ assert_equal(%w[a b], header)
111
+ assert_equal([%w[1 3]], all)
93
112
  }
94
- assert_raise(ArgumentError) { Tb::Reader.open(Object.new) }
113
+ assert_raise(ArgumentError) { Tb.open_reader(Object.new) }
95
114
  }
96
115
  end
97
116
 
@@ -101,9 +120,12 @@ class TestTbReader < Test::Unit::TestCase
101
120
  a,b
102
121
  1,3
103
122
  End
104
- Tb::Reader.open(i) {|r|
105
- assert_equal(%w[a b], r.header)
106
- assert_equal([%w[1 3]], r.read_all)
123
+ Tb.open_reader(i) {|r|
124
+ header = nil
125
+ all = []
126
+ r.with_header {|h| header = h}.each {|pairs| all << pairs.map {|f, v| v } }
127
+ assert_equal(%w[a b], header)
128
+ assert_equal([%w[1 3]], all)
107
129
  assert_equal("a", r.field_from_index(0))
108
130
  assert_equal("b", r.field_from_index(1))
109
131
  assert_raise(ArgumentError) { r.field_from_index(2) }
@@ -0,0 +1,10 @@
1
+ require 'tb'
2
+ require 'test/unit'
3
+
4
+ class TestRevCmp < Test::Unit::TestCase
5
+ def test_cmp
6
+ assert_equal(2 <=> 1, Tb::RevCmp.new(1) <=> Tb::RevCmp.new(2))
7
+ assert_equal(1 <=> 2, Tb::RevCmp.new(2) <=> Tb::RevCmp.new(1))
8
+ assert_equal(3 <=> 3, Tb::RevCmp.new(3) <=> Tb::RevCmp.new(3))
9
+ end
10
+ end
@@ -0,0 +1,173 @@
1
+ require 'tb'
2
+ require 'test/unit'
3
+ require 'tmpdir'
4
+
5
+ class TestTbEnum < Test::Unit::TestCase
6
+ def test_cat_without_block
7
+ t1 = Tb.new(%w[a b], [1, 2], [3, 4])
8
+ t2 = Tb.new(%w[c d], [5, 6], [7, 8])
9
+ e = t1.cat(t2)
10
+ result = []
11
+ e.with_header {|header|
12
+ result << [:header, header]
13
+ }.each {|x|
14
+ assert_kind_of(Tb::Record, x)
15
+ result << [x.to_a]
16
+ }
17
+ assert_equal(
18
+ [[:header, %w[a b c d]],
19
+ [[['a', 1], ['b', 2]]],
20
+ [[['a', 3], ['b', 4]]],
21
+ [[['c', 5], ['d', 6]]],
22
+ [[['c', 7], ['d', 8]]]],
23
+ result)
24
+ end
25
+
26
+ def test_cat_with_block
27
+ t1 = Tb.new(%w[a b], [1, 2], [3, 4])
28
+ t2 = Tb.new(%w[c d], [5, 6], [7, 8])
29
+ result = []
30
+ t1.cat(t2) {|x|
31
+ assert_kind_of(Tb::Record, x)
32
+ result << x.to_a
33
+ }
34
+ assert_equal(
35
+ [[['a', 1], ['b', 2]],
36
+ [['a', 3], ['b', 4]],
37
+ [['c', 5], ['d', 6]],
38
+ [['c', 7], ['d', 8]]],
39
+ result)
40
+ end
41
+
42
+ def test_write_to_csv_basic
43
+ obj = [
44
+ [['a', 0], ['b', 1], ['c', 2]],
45
+ [['a', 3], ['b', 4], ['c', 5]],
46
+ [['a', 6], ['b', 7], ['c', 8]],
47
+ ]
48
+ def obj.header_and_each(header_proc, &block)
49
+ header_proc.call(nil) if header_proc
50
+ self.each(&block)
51
+ end
52
+ obj.extend Tb::Enum
53
+ Dir.mktmpdir {|d|
54
+ open("#{d}/foo.csv", 'w') {|f|
55
+ obj.write_to_csv(f)
56
+ }
57
+ assert_equal(<<-'End'.gsub(/^\s*/, ''), File.read("#{d}/foo.csv"))
58
+ a,b,c
59
+ 0,1,2
60
+ 3,4,5
61
+ 6,7,8
62
+ End
63
+ }
64
+ end
65
+
66
+ def test_write_to_csv_header_extension
67
+ obj = [
68
+ [['a', 0]],
69
+ [['b', 1]],
70
+ [['c', 2]],
71
+ ]
72
+ def obj.header_and_each(header_proc, &block)
73
+ header_proc.call(nil) if header_proc
74
+ self.each(&block)
75
+ end
76
+ obj.extend Tb::Enum
77
+ Dir.mktmpdir {|d|
78
+ open("#{d}/foo.csv", 'w') {|f|
79
+ obj.write_to_csv(f)
80
+ }
81
+ assert_equal(<<-'End'.gsub(/^\s*/, ''), File.read("#{d}/foo.csv"))
82
+ a,b,c
83
+ 0
84
+ ,1
85
+ ,,2
86
+ End
87
+ }
88
+ end
89
+
90
+ def test_write_to_csv_without_header
91
+ obj = [
92
+ [['a', 0]],
93
+ [['b', 1]],
94
+ [['c', 2]],
95
+ ]
96
+ def obj.header_and_each(header_proc, &block)
97
+ header_proc.call(nil) if header_proc
98
+ self.each(&block)
99
+ end
100
+ obj.extend Tb::Enum
101
+ Dir.mktmpdir {|d|
102
+ open("#{d}/foo.csv", 'w') {|f|
103
+ obj.write_to_csv(f, false)
104
+ }
105
+ assert_equal(<<-'End'.gsub(/^\s*/, ''), File.read("#{d}/foo.csv"))
106
+ 0
107
+ ,1
108
+ ,,2
109
+ End
110
+ }
111
+ end
112
+
113
+ def test_extsort_by
114
+ er = Tb::Enumerator.new {|y|
115
+ y.yield("a" => 1, "b" => 2)
116
+ y.yield("b" => 3, "c" => 4)
117
+ }
118
+ er2 = er.extsort_by {|pairs| -pairs["b"] }
119
+ result = []
120
+ er2.with_header {|h|
121
+ result << h
122
+ }.each {|pairs|
123
+ result << pairs
124
+ }
125
+ assert_equal([%w[a b c], {"b" => 3, "c" => 4}, {"a" => 1, "b" => 2}], result)
126
+ end
127
+
128
+ def test_with_cumulative_header
129
+ er = Tb::Enumerator.new {|y|
130
+ y.set_header %w[a c]
131
+ y.yield("c" => 1, "d" => 2)
132
+ y.yield("b" => 1, "c" => 2)
133
+ y.yield("a" => 3, "b" => 4)
134
+ }
135
+ result = []
136
+ er.with_cumulative_header {|header|
137
+ result << header
138
+ }.each {|pairs, cheader|
139
+ result << pairs << cheader
140
+ }
141
+ assert_equal(
142
+ [%w[a c],
143
+ { "c" => 1, "d" => 2 },
144
+ %w[a c d],
145
+ { "b" => 1, "c" => 2 },
146
+ %w[a c d b],
147
+ { "a" => 3, "b" => 4 },
148
+ %w[a c d b]],
149
+ result)
150
+ end
151
+
152
+ def test_with_cumulative_header_no_header_proc
153
+ er = Tb::Enumerator.new {|y|
154
+ y.set_header %w[a c]
155
+ y.yield("c" => 1, "d" => 2)
156
+ y.yield("b" => 1, "c" => 2)
157
+ y.yield("a" => 3, "b" => 4)
158
+ }
159
+ result = []
160
+ er.with_cumulative_header.each {|pairs, cheader|
161
+ result << pairs << cheader
162
+ }
163
+ assert_equal(
164
+ [{ "c" => 1, "d" => 2 },
165
+ %w[a c d],
166
+ { "b" => 1, "c" => 2 },
167
+ %w[a c d b],
168
+ { "a" => 3, "b" => 4 },
169
+ %w[a c d b]],
170
+ result)
171
+ end
172
+
173
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tb
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-29 00:00:00.000000000 Z
12
+ date: 2012-01-29 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: ! 'tb is a manipulation tool for table: CSV, TSV, etc.
14
+ description: ! 'tb is a manipulation tool for table: CSV, TSV, JSON, etc.
15
15
 
16
16
 
17
17
  tb provides a command and a library for manipulating tables:
@@ -38,35 +38,45 @@ files:
38
38
  - lib/tb/cmd_consecutive.rb
39
39
  - lib/tb/cmd_crop.rb
40
40
  - lib/tb/cmd_cross.rb
41
- - lib/tb/cmd_csv.rb
42
41
  - lib/tb/cmd_cut.rb
42
+ - lib/tb/cmd_git_log.rb
43
43
  - lib/tb/cmd_grep.rb
44
44
  - lib/tb/cmd_group.rb
45
45
  - lib/tb/cmd_gsub.rb
46
46
  - lib/tb/cmd_help.rb
47
47
  - lib/tb/cmd_join.rb
48
- - lib/tb/cmd_json.rb
49
48
  - lib/tb/cmd_ls.rb
50
49
  - lib/tb/cmd_mheader.rb
50
+ - lib/tb/cmd_nest.rb
51
51
  - lib/tb/cmd_newfield.rb
52
- - lib/tb/cmd_pnm.rb
53
- - lib/tb/cmd_pp.rb
54
52
  - lib/tb/cmd_rename.rb
55
53
  - lib/tb/cmd_shape.rb
56
54
  - lib/tb/cmd_sort.rb
57
55
  - lib/tb/cmd_svn_log.rb
58
- - lib/tb/cmd_tsv.rb
59
- - lib/tb/cmd_yaml.rb
56
+ - lib/tb/cmd_tar_tvf.rb
57
+ - lib/tb/cmd_to_csv.rb
58
+ - lib/tb/cmd_to_json.rb
59
+ - lib/tb/cmd_to_pnm.rb
60
+ - lib/tb/cmd_to_pp.rb
61
+ - lib/tb/cmd_to_tsv.rb
62
+ - lib/tb/cmd_to_yaml.rb
63
+ - lib/tb/cmd_unnest.rb
60
64
  - lib/tb/cmdmain.rb
61
65
  - lib/tb/cmdtop.rb
62
66
  - lib/tb/cmdutil.rb
63
67
  - lib/tb/csv.rb
68
+ - lib/tb/enum.rb
64
69
  - lib/tb/enumerable.rb
70
+ - lib/tb/enumerator.rb
65
71
  - lib/tb/fieldset.rb
72
+ - lib/tb/fileenumerator.rb
73
+ - lib/tb/json.rb
66
74
  - lib/tb/pager.rb
75
+ - lib/tb/pairs.rb
67
76
  - lib/tb/pnm.rb
68
77
  - lib/tb/reader.rb
69
78
  - lib/tb/record.rb
79
+ - lib/tb/revcmp.rb
70
80
  - lib/tb/ropen.rb
71
81
  - lib/tb/search.rb
72
82
  - lib/tb/tsv.rb
@@ -85,34 +95,43 @@ files:
85
95
  - test/test_cmd_consecutive.rb
86
96
  - test/test_cmd_crop.rb
87
97
  - test/test_cmd_cross.rb
88
- - test/test_cmd_csv.rb
89
98
  - test/test_cmd_cut.rb
99
+ - test/test_cmd_git_log.rb
90
100
  - test/test_cmd_grep.rb
91
101
  - test/test_cmd_group.rb
92
102
  - test/test_cmd_gsub.rb
93
103
  - test/test_cmd_help.rb
94
104
  - test/test_cmd_join.rb
95
- - test/test_cmd_json.rb
96
105
  - test/test_cmd_ls.rb
97
106
  - test/test_cmd_mheader.rb
107
+ - test/test_cmd_nest.rb
98
108
  - test/test_cmd_newfield.rb
99
- - test/test_cmd_pnm.rb
100
- - test/test_cmd_pp.rb
101
109
  - test/test_cmd_rename.rb
102
110
  - test/test_cmd_shape.rb
103
111
  - test/test_cmd_sort.rb
104
- - test/test_cmd_tsv.rb
105
- - test/test_cmd_yaml.rb
112
+ - test/test_cmd_tar_tvf.rb
113
+ - test/test_cmd_to_csv.rb
114
+ - test/test_cmd_to_json.rb
115
+ - test/test_cmd_to_pnm.rb
116
+ - test/test_cmd_to_pp.rb
117
+ - test/test_cmd_to_tsv.rb
118
+ - test/test_cmd_to_yaml.rb
119
+ - test/test_cmd_unnest.rb
106
120
  - test/test_cmdtty.rb
107
121
  - test/test_cmdutil.rb
108
122
  - test/test_csv.rb
109
123
  - test/test_enumerable.rb
110
124
  - test/test_fieldset.rb
125
+ - test/test_fileenumerator.rb
126
+ - test/test_json.rb
111
127
  - test/test_pager.rb
128
+ - test/test_pairs.rb
112
129
  - test/test_pnm.rb
113
130
  - test/test_reader.rb
114
131
  - test/test_record.rb
132
+ - test/test_revcmp.rb
115
133
  - test/test_search.rb
134
+ - test/test_tbenum.rb
116
135
  - test/test_tsv.rb
117
136
  homepage: https://github.com/akr/tb
118
137
  licenses: []
@@ -125,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
144
  requirements:
126
145
  - - ! '>='
127
146
  - !ruby/object:Gem::Version
128
- version: '0'
147
+ version: 1.9.2
129
148
  required_rubygems_version: !ruby/object:Gem::Requirement
130
149
  none: false
131
150
  requirements:
@@ -137,7 +156,7 @@ rubyforge_project:
137
156
  rubygems_version: 1.8.11
138
157
  signing_key:
139
158
  specification_version: 3
140
- summary: ! 'manipulation tool for table: CSV, TSV, etc.'
159
+ summary: ! 'manipulation tool for tables: CSV, TSV, JSON, etc.'
141
160
  test_files:
142
161
  - test/test_basic.rb
143
162
  - test/test_catreader.rb
@@ -145,32 +164,41 @@ test_files:
145
164
  - test/test_cmd_consecutive.rb
146
165
  - test/test_cmd_crop.rb
147
166
  - test/test_cmd_cross.rb
148
- - test/test_cmd_csv.rb
149
167
  - test/test_cmd_cut.rb
168
+ - test/test_cmd_git_log.rb
150
169
  - test/test_cmd_grep.rb
151
170
  - test/test_cmd_group.rb
152
171
  - test/test_cmd_gsub.rb
153
172
  - test/test_cmd_help.rb
154
173
  - test/test_cmd_join.rb
155
- - test/test_cmd_json.rb
156
174
  - test/test_cmd_ls.rb
157
175
  - test/test_cmd_mheader.rb
176
+ - test/test_cmd_nest.rb
158
177
  - test/test_cmd_newfield.rb
159
- - test/test_cmd_pnm.rb
160
- - test/test_cmd_pp.rb
161
178
  - test/test_cmd_rename.rb
162
179
  - test/test_cmd_shape.rb
163
180
  - test/test_cmd_sort.rb
164
- - test/test_cmd_tsv.rb
165
- - test/test_cmd_yaml.rb
181
+ - test/test_cmd_tar_tvf.rb
182
+ - test/test_cmd_to_csv.rb
183
+ - test/test_cmd_to_json.rb
184
+ - test/test_cmd_to_pnm.rb
185
+ - test/test_cmd_to_pp.rb
186
+ - test/test_cmd_to_tsv.rb
187
+ - test/test_cmd_to_yaml.rb
188
+ - test/test_cmd_unnest.rb
166
189
  - test/test_cmdtty.rb
167
190
  - test/test_cmdutil.rb
168
191
  - test/test_csv.rb
169
192
  - test/test_enumerable.rb
170
193
  - test/test_fieldset.rb
194
+ - test/test_fileenumerator.rb
195
+ - test/test_json.rb
171
196
  - test/test_pager.rb
197
+ - test/test_pairs.rb
172
198
  - test/test_pnm.rb
173
199
  - test/test_reader.rb
174
200
  - test/test_record.rb
201
+ - test/test_revcmp.rb
175
202
  - test/test_search.rb
203
+ - test/test_tbenum.rb
176
204
  - test/test_tsv.rb
data/lib/tb/cmd_csv.rb DELETED
@@ -1,42 +0,0 @@
1
- # Copyright (C) 2011 Tanaka Akira <akr@fsij.org>
2
- #
3
- # Redistribution and use in source and binary forms, with or without
4
- # modification, are permitted provided that the following conditions are met:
5
- #
6
- # 1. Redistributions of source code must retain the above copyright notice, this
7
- # list of conditions and the following disclaimer.
8
- # 2. Redistributions in binary form must reproduce the above copyright notice,
9
- # this list of conditions and the following disclaimer in the documentation
10
- # and/or other materials provided with the distribution.
11
- # 3. The name of the author may not be used to endorse or promote products
12
- # derived from this software without specific prior written permission.
13
- #
14
- # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
- # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17
- # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
19
- # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20
- # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21
- # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22
- # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
23
- # OF SUCH DAMAGE.
24
-
25
- Tb::Cmd.subcommands << 'csv'
26
-
27
- def (Tb::Cmd).op_csv
28
- op = OptionParser.new
29
- op.banner = 'Usage: tb csv [OPTS] [TABLE ...]'
30
- define_common_option(op, "hNo", "--no-pager")
31
- op
32
- end
33
-
34
- def (Tb::Cmd).main_csv(argv)
35
- op_csv.parse!(argv)
36
- exit_if_help('csv')
37
- argv = ['-'] if argv.empty?
38
- tbl = Tb::CatReader.open(argv, Tb::Cmd.opt_N) {|creader| build_table(creader) }
39
- with_output {|out|
40
- tbl_generate_csv(tbl, out)
41
- }
42
- end