Skip to content

Commit

Permalink
Improve error message when missing -H or in BEGIN (#239)
Browse files Browse the repository at this point in the history
Fixes #238
  • Loading branch information
benhoyt authored Sep 14, 2024
1 parent f39db9b commit 6655acf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ func (p *interp) getFieldByName(name string) (value, error) {
if p.fieldIndexes == nil {
// Lazily create map of field names to indexes.
if p.fieldNames == nil {
return null(), newError(`@ only supported if header parsing enabled; use -H or add "header" to INPUTMODE`)
return null(), newError(`no field names for @; use -H or add "header" to INPUTMODE, and use "getline" first if in BEGIN`)
}
p.fieldIndexes = make(map[string]int, len(p.fieldNames))
for i, n := range p.fieldNames {
Expand Down
4 changes: 3 additions & 1 deletion interp/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,9 @@ var csvTests = []csvTest{
{`BEGIN { INPUTMODE="csv" } { print $1, $3 }`, "name,email,age\nBob,[email protected],42\nJane,[email protected],37", "name age\nBob 42\nJane 37\n", "", nil},
{`BEGIN { INPUTMODE="csv header" } { print @"age", @"name" }`, "name,email,age\nBob,[email protected],42\nJane,[email protected],37", "42 Bob\n37 Jane\n", "", nil},
{`BEGIN { INPUTMODE="csv header" } { x="name"; print @"age", @x }`, "name,age\nBob,42", "42 Bob\n", "", nil},
{`BEGIN { INPUTMODE="csv" } { print @"age", @"name" }`, "name,email,age\nBob,[email protected],42\nJane,[email protected],37", "", `@ only supported if header parsing enabled; use -H or add "header" to INPUTMODE`, nil},
{`BEGIN { INPUTMODE="csv" } { print @"age", @"name" }`, "name,email,age\nBob,[email protected],42\nJane,[email protected],37", "", `no field names for @; use -H or add "header" to INPUTMODE, and use "getline" first if in BEGIN`, nil},
{`BEGIN { INPUTMODE="csv header"; print @"age", @"name" }`, "name,email,age\nBob,[email protected],42\nJane,[email protected],37", "", `no field names for @; use -H or add "header" to INPUTMODE, and use "getline" first if in BEGIN`, nil},
{`BEGIN { INPUTMODE="csv header"; getline; print @"age", @"name" }`, "name,email,age\nBob,[email protected],42\nJane,[email protected],37", "42 Bob\n", "", nil},
{`BEGIN { INPUTMODE="tsv header" } { print $1, $3 }`, "name\temail\tage\nBob,Smith\t[email protected]\t42\nJane\t[email protected]\t37", "Bob,Smith 42\nJane 37\n", "", nil},

// OUTPUTMODE combinations
Expand Down

0 comments on commit 6655acf

Please sign in to comment.