Repository: benhoyt/goawk (go)

Issue:
Misleading Error When Header Name Used in BEGIN/END
When `@"Header_Name"` is used in `BEGIN` and `END` blocks, the error message is misleading, e.g.

```
$ goawk --csv  -H 'BEGIN { print @"HEADER" }' /dev/null
@ only supported if header parsing enabled; use -H or add "header" to INPUTMODE
```

Pull request:
Improve error message when missing -H or in BEGIN
Fixes #238 

Patch:
diff --git a/interp/interp.go b/interp/interp.go
index 7f5c8c3..594c111 100644
--- a/interp/interp.go
+++ b/interp/interp.go
@@ -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 {


Test patch:
diff --git a/interp/interp_test.go b/interp/interp_test.go
index 2083b90..5b7262c 100644
--- a/interp/interp_test.go
+++ b/interp/interp_test.go
@@ -1797,7 +1797,9 @@ var csvTests = []csvTest{
 	{`BEGIN { INPUTMODE="csv" } { print $1, $3 }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,37", "name age\nBob 42\nJane 37\n", "", nil},
 	{`BEGIN { INPUTMODE="csv header" } { print @"age", @"name" }`, "name,email,age\nBob,bob@smith.com,42\nJane,jane@brown.com,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,bob@smith.com,42\nJane,jane@brown.com,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,bob@smith.com,42\nJane,jane@brown.com,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,bob@smith.com,42\nJane,jane@brown.com,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,bob@smith.com,42\nJane,jane@brown.com,37", "42 Bob\n", "", nil},
 	{`BEGIN { INPUTMODE="tsv header" } { print $1, $3 }`, "name\temail\tage\nBob,Smith\tbob@smith.com\t42\nJane\tjane@brown.com\t37", "Bob,Smith 42\nJane 37\n", "", nil},
 
 	// OUTPUTMODE combinations