/
main.go
116 lines (98 loc) · 2.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright ©2015 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// pawgo is a simple interactive shell to quickly plot hbook histograms from
// rio files.
//
// Example:
//
// $> pawgo
// paw> /file/open f testdata/issue-120.rio
// paw> /file/ls f
// /file/id/f name=testdata/issue-120.rio
// - MonoH_Truth/jets (type="*go-hep.org/x/hep/hbook.H1D")
//
// paw> /hist/open h /file/id/f/MonoH_Truth/jets
// paw> /hist/plot h
// == h1d: name="MonoH_Truth/jets"
// entries=20000
// mean= +2.554
// RMS= +2.891
// paw> /?
// /! -- run a shell command
// /? -- print help
// /file/close -- close a file
// /file/create -- create file for write access
// /file/list -- list a file's content
// /file/open -- open file for read access
// /hist/open -- open a histogram
// /hist/plot -- plot a histogram
// /quit -- quit PAW-Go
package main // import "go-hep.org/x/hep/pawgo"
//go:generate go run ./gen.hsimple.go
import (
"flag"
"fmt"
"io"
"os"
)
func main() {
interactive := flag.Bool(
"i", false,
"enable interactive mode: drop into PAW-Go prompt after processing script files",
)
flag.Parse()
go func() {
rc := xmain(os.Stdout, *interactive, flag.Args())
os.Exit(rc)
}()
appMain()
}
func xmain(stdout io.Writer, interactive bool, args []string) int {
fmt.Fprintf(stdout, `
:::::::::::::::::::::::::::::
::: Welcome to PAW-Go :::
:::::::::::::::::::::::::::::
Type /? for help.
^D or /quit to quit.
`)
icmd := newCmd(stdout)
defer icmd.Close()
defer fmt.Fprintf(stdout, "bye.\n")
if len(args) > 0 {
for _, fname := range args {
rc := func(fname string) int {
f, err := os.Open(fname)
if err != nil {
icmd.msg.Printf("error: %+v\n", err)
return 1
}
defer f.Close()
err = icmd.RunScript(f)
if err == io.EOF {
return 0
}
if err != nil {
icmd.msg.Printf("error running script [%s]: %+v\n", f.Name(), err)
return 1
}
return 0
}(fname)
if rc != 0 {
return rc
}
}
if !interactive {
return 0
}
}
err := icmd.Run()
if err == io.EOF {
err = nil
}
if err != nil {
icmd.msg.Printf("error running interpreter: %v\n", err)
return 1
}
return 0
}