Improved command line parsing, started on json-based parsing instructions
This commit is contained in:
parent
6a9d3e1a0f
commit
e64fae50b0
@ -1,45 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"git.binarythought.com/cdramey/sutron"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"git.binarythought.com/cdramey/sutron"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ParserConfig struct {
|
||||
ParserType string `json:"parser_type"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"required argument missing\n")
|
||||
flags := flag.NewFlagSet("sutron", flag.ExitOnError)
|
||||
flags.SetOutput(os.Stdout)
|
||||
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)
|
||||
}
|
||||
|
||||
var raw []byte
|
||||
var err error
|
||||
if os.Args[1] == "-" {
|
||||
raw, err = ioutil.ReadAll(os.Stdin)
|
||||
} else {
|
||||
raw, err = ioutil.ReadFile(os.Args[1])
|
||||
}
|
||||
jsonsrc, err := ioutil.ReadFile(*jsonfn)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"error reading \"%s\": %s\n",
|
||||
os.Args[1], err.Error())
|
||||
}
|
||||
|
||||
i := bytes.Index(raw, []byte("B"))
|
||||
if i != -1 {
|
||||
sutrondata, err := sutron.ParseSutronB(raw[i:])
|
||||
var pcfg ParserConfig
|
||||
err = json.Unmarshal(jsonsrc, &pcfg)
|
||||
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 {
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"%s\n", err.Error())
|
||||
os.Exit(1)
|
||||
"error reading \"%s\": %s\n",
|
||||
os.Args[1], err.Error())
|
||||
}
|
||||
|
||||
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))
|
||||
i := bytes.Index(raw, []byte("B"))
|
||||
if i != -1 {
|
||||
sutrondata, err := sutron.ParseSutronB(raw[i:])
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
12
sutron.go
12
sutron.go
@ -5,10 +5,10 @@ import (
|
||||
)
|
||||
|
||||
type SutronB struct {
|
||||
Block byte
|
||||
Group byte
|
||||
Delta byte
|
||||
Data []byte
|
||||
Block byte
|
||||
Group byte
|
||||
Delta byte
|
||||
Data []byte
|
||||
Voltage byte
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ func ParseSutronB(raw []byte) (*SutronB, error) {
|
||||
data.Block = raw[0]
|
||||
data.Group = raw[1]
|
||||
data.Delta = raw[2]
|
||||
data.Data = raw[3:raw_len - 1]
|
||||
data.Voltage = raw[raw_len - 1]
|
||||
data.Data = raw[3 : raw_len-1]
|
||||
data.Voltage = raw[raw_len-1]
|
||||
return &data, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user