Skip to content

Commit 76e3f6a

Browse files
committed
added tab2csv cli
1 parent e71af8a commit 76e3f6a

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

cmd/tab2csv/tab2csv.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/csv"
1010
"flag"
1111
"fmt"
12+
"io"
1213
"os"
1314
"path"
1415

@@ -46,6 +47,12 @@ This would yield
4647
showHelp bool
4748
showLicense bool
4849
showVersion bool
50+
51+
// CSV Reader Options
52+
lazyQuotes bool
53+
trimLeadingSpaces bool
54+
reuseRecord bool
55+
fieldsPerRecord int
4956
)
5057

5158
func main() {
@@ -56,6 +63,12 @@ func main() {
5663
flag.BoolVar(&showLicense, "license", false, "display license")
5764
flag.BoolVar(&showVersion, "version", false, "display version")
5865

66+
// CSV Reader options
67+
flag.IntVar(&fieldsPerRecord, "fields-per-record", 0, "sets the number o fields expected in each row, -1 turns this off")
68+
flag.BoolVar(&lazyQuotes, "lazy-quotes", false, "use lazy quoting for reader")
69+
flag.BoolVar(&trimLeadingSpaces, "left-trim", false, "trims leading space read")
70+
flag.BoolVar(&reuseRecord, "reuse-record", false, "re-uses the backing array on reader")
71+
5972
// Parse Environment and Options
6073
flag.Parse()
6174

@@ -73,11 +86,16 @@ func main() {
7386
os.Exit(0)
7487
}
7588

76-
exitCode := 0
7789
// Setup the CSV output
7890
r := csv.NewReader(os.Stdin)
7991
r.Comma = '\t'
8092
r.Comment = '#'
93+
r.FieldsPerRecord = fieldsPerRecord
94+
r.LazyQuotes = lazyQuotes
95+
r.TrimLeadingSpace = trimLeadingSpaces
96+
r.ReuseRecord = reuseRecord
97+
98+
exitCode := 0
8199
w := csv.NewWriter(os.Stdout)
82100
/*
83101
if delimiter != "" {
@@ -86,10 +104,11 @@ func main() {
86104
*/
87105
for {
88106
row, err := r.Read()
89-
if err != nil {
107+
if err == io.EOF {
108+
break
109+
} else if err != nil {
90110
fmt.Fprintln(os.Stderr, err)
91111
exitCode = 1
92-
break
93112
} else {
94113
if err := w.Write(row); err != nil {
95114
fmt.Fprintln(os.Stderr, err)

0 commit comments

Comments
 (0)