diff --git a/README.md b/README.md index 648f2be..f23f669 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,8 @@ default value is 10. `-f fieldlist, --fields fieldlist` Specifies which fields should be extracted from incoming records and used in computing occurrence counts. The fieldlist must be a comma‐separated list of integers identifying field numbers, -which start at one, for example 3 and 2,5,6. +which start at one, for example 3 and 2,5,6. The fields +must be provided in order, so 3,1,7 is an error. If no fieldlist is provided, **tf** treats the whole input record as a single field. diff --git a/doc/tf.1 b/doc/tf.1 index 4ff745e..a7ffb98 100644 --- a/doc/tf.1 +++ b/doc/tf.1 @@ -39,7 +39,7 @@ value is 10. Specifies which fields should be extracted from incoming records and used in computing occurrence counts. The fieldlist must be a comma-separated list of integers identifying field numbers, which start at one, for example \fB3\fR and -\fB2,5,6\fR. +\fB2,5,6\fR. The fields must be provided in order, so \fB3,1,7\fR is an error. If no fieldlist is provided, \fBtf\fR treats the whole input record as a single field. diff --git a/main.go b/main.go index b032fc8..149e7d9 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "os" "runtime/pprof" @@ -23,7 +24,8 @@ Usage: tf -h, -help, --help (filename) [optional, stdin if omitted] -Field list is comma-separated integers, e.g. -f 3 or --fields 1,3,7 +Field list is comma-separated integers, e.g. -f 3 or --fields 1,3,7. The fields +must be provided in order, so 3,1,7 is an error. The regexp-valued fields work as follows: -g/--grep discardsrecords that don't match the regexp (g for grep) @@ -132,7 +134,11 @@ func main() { return } // The generated trace can be analyzed with: go tool trace - trace.Start(f) + err = trace.Start(f) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "can't start tracing: %s", err.Error()) + return + } defer trace.Stop() } var kf = topfew.NewKeyFinder(fields) @@ -169,11 +175,17 @@ func main() { func parseFields(spec string) ([]uint, error) { parts := strings.Split(spec, ",") var fields []uint + lastNum := -1 for _, part := range parts { num, err := strconv.Atoi(part) if err != nil { return nil, fmt.Errorf("Illegal field spec: %v", err) } + if num <= lastNum { + return nil, errors.New(fmt.Sprintf( "field-number list must be in order; problem at \"%d\":", num)) + } else { + lastNum = num + } fields = append(fields, uint(num)) } return fields, nil