Improved command line parsing, started on json-based parsing instructions

This commit is contained in:
Christopher Ramey 2019-10-06 20:16:19 -08:00 committed by Christopher Ramey
parent 6a9d3e1a0f
commit e64fae50b0
2 changed files with 65 additions and 27 deletions

View File

@ -1,45 +1,83 @@
package main package main
import ( import (
"git.binarythought.com/cdramey/sutron"
"fmt"
"bytes" "bytes"
"flag"
"fmt"
"encoding/json"
"git.binarythought.com/cdramey/sutron"
"io/ioutil" "io/ioutil"
"os" "os"
) )
type ParserConfig struct {
ParserType string `json:"parser_type"`
}
func main() { func main() {
if len(os.Args) < 2 { flags := flag.NewFlagSet("sutron", flag.ExitOnError)
fmt.Fprintf(os.Stderr, flags.SetOutput(os.Stdout)
"required argument missing\n") flags.Usage = func() {
fmt.Fprintf(flags.Output(),
"Usage: %s [OPTION] <files> ...\nOptions:\n",
os.Args[0])
flags.PrintDefaults()
}
jsonfn := flags.String("json", "sutron.json",
"json parser definition file name")
flags.Parse(os.Args[1:])
if flags.NArg() < 1 {
fmt.Fprintf(os.Stderr, "files parameter cannot be empty\n")
os.Exit(1) os.Exit(1)
} }
var raw []byte jsonsrc, err := ioutil.ReadFile(*jsonfn)
var err error
if os.Args[1] == "-" {
raw, err = ioutil.ReadAll(os.Stdin)
} else {
raw, err = ioutil.ReadFile(os.Args[1])
}
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
"error reading \"%s\": %s\n", "error reading \"%s\": %s\n",
os.Args[1], err.Error()) os.Args[1], err.Error())
} }
i := bytes.Index(raw, []byte("B")) var pcfg ParserConfig
if i != -1 { err = json.Unmarshal(jsonsrc, &pcfg)
sutrondata, err := sutron.ParseSutronB(raw[i:]) if err != nil {
fmt.Fprintf(os.Stderr,
"parser config error: %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Parser type: %s\n", pcfg.ParserType)
for _, fn := range flags.Args() {
var raw []byte
var err error
if fn == "-" {
raw, err = ioutil.ReadAll(os.Stdin)
} else {
raw, err = ioutil.ReadFile(fn)
}
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
"%s\n", err.Error()) "error reading \"%s\": %s\n",
os.Exit(1) os.Args[1], err.Error())
} }
fmt.Printf("Sutron Block: %s\n", string(sutrondata.Block)) i := bytes.Index(raw, []byte("B"))
fmt.Printf("Sutron Group: %s\n", string(sutrondata.Group)) if i != -1 {
fmt.Printf("Sutron Delta: %s\n", string(sutrondata.Delta)) sutrondata, err := sutron.ParseSutronB(raw[i:])
fmt.Printf("Sutron Voltage: %s\n", string(sutrondata.Voltage)) if err != nil {
fmt.Fprintf(os.Stderr,
"%s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Sutron Block: %s\n", string(sutrondata.Block))
fmt.Printf("Sutron Group: %s\n", string(sutrondata.Group))
fmt.Printf("Sutron Delta: %s\n", string(sutrondata.Delta))
fmt.Printf("Sutron Voltage: %s\n", string(sutrondata.Voltage))
}
} }
} }

View File

@ -5,10 +5,10 @@ import (
) )
type SutronB struct { type SutronB struct {
Block byte Block byte
Group byte Group byte
Delta byte Delta byte
Data []byte Data []byte
Voltage byte Voltage byte
} }
@ -40,7 +40,7 @@ func ParseSutronB(raw []byte) (*SutronB, error) {
data.Block = raw[0] data.Block = raw[0]
data.Group = raw[1] data.Group = raw[1]
data.Delta = raw[2] data.Delta = raw[2]
data.Data = raw[3:raw_len - 1] data.Data = raw[3 : raw_len-1]
data.Voltage = raw[raw_len - 1] data.Voltage = raw[raw_len-1]
return &data, nil return &data, nil
} }