1
0
Fork 0

Main program parses stdin

This commit is contained in:
Gregory Eremin 2015-02-18 23:06:15 +07:00
parent 23577d7274
commit 75ff8bf4d5
2 changed files with 44 additions and 22 deletions

44
main.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/localhots/punk/buffer"
"github.com/localhots/punk/parser"
)
func main() {
var (
sel string
verbose bool
)
flag.StringVar(&sel, "s", "", "Selector")
flag.BoolVar(&verbose, "v", false, "Verbose parsing")
flag.Parse()
if len(sel) == 0 && !verbose {
fmt.Println("No selectors given and parser is not verbose")
os.Exit(1)
}
sels := strings.Split(sel, " ")
if len(sel) == 0 {
sels = []string{}
}
buf := buffer.NewStreamBuffer(os.Stdin)
pars := parser.New(buf, sels)
if verbose {
pars.Debug()
}
res := pars.ParseStream()
for {
if m, ok := <-res; ok {
fmt.Println(m.Sel, m.Val)
} else {
break
}
}
}

22
run.go
View File

@ -1,22 +0,0 @@
package main
import (
"os"
"github.com/kr/pretty"
"github.com/localhots/punk/buffer"
"github.com/localhots/punk/parser"
)
func main() {
f, _ := os.Open("test.json")
// b, _ := ioutil.ReadAll(f)
buf := buffer.NewStreamBuffer(f)
p := parser.New(buf, []string{
"/prices/*",
"/bananas/[*]/weight",
})
res := p.Parse()
pretty.Println(res)
}