Skip to content

Commit 5401a39

Browse files
committed
added csv2tab cli
1 parent 29ba267 commit 5401a39

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

cmd/csv2tab/csv2tab.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// tabs2csv converts a tab delimited file to a CSV formatted file.
2+
//
3+
// @author R. S. Doiel, <[email protected]>
4+
package main
5+
6+
import (
7+
"encoding/csv"
8+
"flag"
9+
"fmt"
10+
"io"
11+
"os"
12+
"path"
13+
14+
// Caltech Library Packages
15+
"github.com/caltechlibrary/datatools"
16+
)
17+
18+
var (
19+
description = `
20+
USAGE
21+
22+
%s is a simple conversion utility to convert from CSV to tab separated values.
23+
%s reads from standard input and writes to standard out.
24+
25+
`
26+
27+
examples = `
28+
If my.tab contained
29+
30+
"name","email","age"
31+
"Doe, Jane","[email protected]",42
32+
33+
Concert this to a tab separated values
34+
35+
%s < my.csv
36+
37+
This would yield
38+
39+
name email age
40+
Doe, Jane [email protected] 42
41+
42+
`
43+
44+
// Standard Options
45+
showHelp bool
46+
showLicense bool
47+
showVersion bool
48+
49+
// CSV Reader Options
50+
lazyQuotes bool
51+
trimLeadingSpace bool
52+
reuseRecord bool
53+
fieldsPerRecord int
54+
)
55+
56+
func main() {
57+
appName := path.Base(os.Args[0])
58+
59+
flag.BoolVar(&showHelp, "h", false, "display help")
60+
flag.BoolVar(&showHelp, "help", false, "display help")
61+
flag.BoolVar(&showLicense, "license", false, "display license")
62+
flag.BoolVar(&showVersion, "version", false, "display version")
63+
64+
// CSV Reader options
65+
flag.IntVar(&fieldsPerRecord, "fields-per-record", 0, "sets the number o fields expected in each row, -1 turns this off")
66+
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quoting for reader")
67+
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trims leading space read")
68+
flag.BoolVar(&reuseRecord, "reuse-record", false, "re-uses the backing array on reader")
69+
70+
// Parse Environment and Options
71+
flag.Parse()
72+
73+
if showHelp {
74+
fmt.Fprintf(os.Stdout, description, appName, appName)
75+
fmt.Fprintf(os.Stdout, examples, appName)
76+
os.Exit(0)
77+
}
78+
if showLicense {
79+
fmt.Fprintln(os.Stdout, datatools.LicenseText, appName, datatools.Version)
80+
os.Exit(0)
81+
}
82+
if showVersion {
83+
fmt.Fprintln(os.Stdout, datatools.Version)
84+
os.Exit(0)
85+
}
86+
87+
// Setup the CSV output
88+
r := csv.NewReader(os.Stdin)
89+
r.Comma = ','
90+
r.Comment = '#'
91+
r.FieldsPerRecord = fieldsPerRecord
92+
r.LazyQuotes = lazyQuotes
93+
r.TrimLeadingSpace = trimLeadingSpace
94+
r.ReuseRecord = reuseRecord
95+
96+
exitCode := 0
97+
w := csv.NewWriter(os.Stdout)
98+
w.Comma = '\t'
99+
/*
100+
if delimiter != "" {
101+
w.Comma = datatools.NormalizeDelimiterRune(delimiter)
102+
}
103+
*/
104+
for {
105+
row, err := r.Read()
106+
if err == io.EOF {
107+
break
108+
} else if err != nil {
109+
fmt.Fprintln(os.Stderr, err)
110+
exitCode = 1
111+
} else {
112+
if err := w.Write(row); err != nil {
113+
fmt.Fprintln(os.Stderr, err)
114+
exitCode = 1
115+
}
116+
w.Flush()
117+
if err := w.Error(); err != nil {
118+
fmt.Fprintln(os.Stderr, err)
119+
exitCode = 1
120+
}
121+
}
122+
}
123+
os.Exit(exitCode)
124+
}

0 commit comments

Comments
 (0)