Skip to content

Commit

Permalink
Merge pull request #5 from Clever/add-support-for-more-delimiters
Browse files Browse the repository at this point in the history
Add support for more delimiters
  • Loading branch information
Colin Schimmelfing committed Jan 14, 2015
2 parents be8a7b0 + c6f9e43 commit aa5b8bb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ ifneq ($(NOLINT),1)
@echo ""
endif
ifeq ($(COVERAGE),1)
@echo "TESTING WITH COVERAGE... $@"
@go test -cover -coverprofile=$(GOPATH)/src/$@/c.out $@ -test.v
@go tool cover -html=$(GOPATH)/src/$@/c.out
else
@echo "TESTING..."
@echo "TESTING... $@"
@go test $@ -test.v
endif

Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ go get github.com/Clever/csvlint/cmd/csvlint

### Options

* delimiter: the field delimiter to default with
* default: comma
* valid options: comma, tab
* if you want anything else, you're probably doing CSVs wrong
_*NOTE*: The default settings validate that a CSV conforms to [RFC 4180](https://tools.ietf.org/html/rfc4180). By changing the settings, you can no longer strictly guarantee a CSV conforms to RFC 4180._

* delimiter: the field delimiter, can be any single unicode character
* default: "," (comma)
* valid options: "\t", "|", "ஃ", etc
* if you want multi-character delimiters, you're probably doing CSVs wrong
* lazyquotes: allow a quote to appear in an unquoted field and a non-doubled quote to appear in a quoted field. _WARNING: your file may pass linting, but not parse in the way you would expect_

### Examples
Expand All @@ -44,7 +46,7 @@ $ csvlint mult_long_columns.csv
Record #2 has error: wrong number of fields in line
Record #4 has error: wrong number of fields in line

$ csvlint --delimiter=tab mult_long_columns_tabs.csv
$ csvlint --delimiter='\t' mult_long_columns_tabs.csv
Record #2 has error: wrong number of fields in line
Record #4 has error: wrong number of fields in line

Expand Down
21 changes: 12 additions & 9 deletions cmd/csvlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"github.com/Clever/csvlint"
"os"
"strconv"
"unicode/utf8"
)

func printHelpAndExit(code int) {
Expand All @@ -13,7 +15,7 @@ func printHelpAndExit(code int) {
}

func main() {
delimiter := flag.String("delimiter", "comma", "field delimiter in the file. options: comma, tab")
delimiter := flag.String("delimiter", ",", "field delimiter in the file, for instance '\\t' or '|'")
lazyquotes := flag.Bool("lazyquotes", false, "try to parse improperly escaped quotes")
help := flag.Bool("help", false, "print help and exit")
flag.Parse()
Expand All @@ -22,16 +24,17 @@ func main() {
printHelpAndExit(0)
}

var comma rune
switch *delimiter {
case "comma":
comma = ','
case "tab":
comma = '\t'
default:
fmt.Printf("unrecognized delimiter '%s'\n\n", *delimiter)
if flag.NFlag() > 0 {
fmt.Fprintln(os.Stderr, "Warning: not using defaults, may not validate CSV to RFC 4180")
}

convertedDelimiter, err := strconv.Unquote(`'` + *delimiter + `'`)
if err != nil {
fmt.Printf("error unquoting delimiter '%s', note that only one-character delimiters are supported\n\n", *delimiter)
printHelpAndExit(1)
}
// don't need to check size since Unquote returns one-character string
comma, _ := utf8.DecodeRuneInString(convertedDelimiter)

if len(flag.Args()) != 1 {
fmt.Println("csvlint accepts a single filepath as an argument\n")
Expand Down
4 changes: 4 additions & 0 deletions linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ var validationTable = []struct {
halted bool
}{
{file: "./test_data/perfect.csv", err: nil, invalids: []CSVError{}},
{file: "./test_data/perfect_tab.csv", err: nil, comma: '\t', invalids: []CSVError{}},
{file: "./test_data/perfect_pipe.csv", err: nil, comma: '|', invalids: []CSVError{}},
{file: "./test_data/perfect_colon.csv", err: nil, comma: ':', invalids: []CSVError{}},
{file: "./test_data/perfect_semicolon.csv", err: nil, comma: ';', invalids: []CSVError{}},
{file: "./test_data/one_long_column.csv", err: nil, invalids: []CSVError{{
Record: []string{"d", "e", "f", "g"},
err: csv.ErrFieldCount,
Expand Down
3 changes: 3 additions & 0 deletions test_data/perfect_colon.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
field1:field2:field3
a:b:c,notafield
d:e:f
3 changes: 3 additions & 0 deletions test_data/perfect_pipe.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
field1|field2|field3
a|b|c,notafield
d|e|f
3 changes: 3 additions & 0 deletions test_data/perfect_semicolon.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
field1;field2;field3
a;b;c,notafield
d;e;f
3 changes: 3 additions & 0 deletions test_data/perfect_tab.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
field1 field2 field3
a b c,notafield
d e f

0 comments on commit aa5b8bb

Please sign in to comment.