Skip to content

Commit

Permalink
Add option "-e"
Browse files Browse the repository at this point in the history
Option "-e" allows to set the command to be executed:

```
echo '6001d5ff0000000003000000000107000000dcff' | \
	yaegi -e 'import "fmt"; import "os"; import "encoding/hex"; import "io/ioutil"; func main() { in, _ := ioutil.ReadAll(os.Stdin); decoded, _ := hex.DecodeString(string(in));fmt.Println(string(decoded)) }' 2>/dev/null | \
	hexdump -C
00000000  60 01 d5 ff 00 00 00 00  03 00 00 00 00 01 07 00  |`...............|
00000010  00 00 dc ff 0a                                    |.....|
00000015
```
  • Loading branch information
xaionaro authored and ldez committed Mar 9, 2020
1 parent c7c1bea commit d29b0a4
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions cmd/yaegi/yaegi.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ import (
func main() {
var interactive bool
var tags string
var cmd string
flag.BoolVar(&interactive, "i", false, "start an interactive REPL")
flag.StringVar(&tags, "tags", "", "set a list of build tags")
flag.StringVar(&cmd, "e", "", "set the command to be executed (instead of script or/and shell)")
flag.Usage = func() {
fmt.Println("Usage:", os.Args[0], "[options] [script] [args]")
fmt.Println("Options:")
Expand All @@ -62,34 +64,41 @@ func main() {
i.Use(stdlib.Symbols)
i.Use(interp.Symbols)

if len(args) > 0 {
// Skip first os arg to set command line as expected by interpreted main
os.Args = os.Args[1:]
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
if cmd != `` {
i.REPL(strings.NewReader(cmd), os.Stderr)
}

b, err := ioutil.ReadFile(args[0])
if err != nil {
log.Fatal("Could not read file: ", args[0])
if len(args) == 0 {
if interactive || cmd == `` {
i.REPL(os.Stdin, os.Stdout)
}
return
}

s := string(b)
if s[:2] == "#!" {
// Allow executable go scripts, but fix them prior to parse
s = strings.Replace(s, "#!", "//", 1)
}
// Skip first os arg to set command line as expected by interpreted main
os.Args = os.Args[1:]
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)

i.Name = args[0]
if _, err := i.Eval(s); err != nil {
fmt.Println(err)
if p, ok := err.(interp.Panic); ok {
fmt.Println(string(p.Stack))
}
}
b, err := ioutil.ReadFile(args[0])
if err != nil {
log.Fatal("Could not read file: ", args[0])
}

if interactive {
i.REPL(os.Stdin, os.Stdout)
s := string(b)
if s[:2] == "#!" {
// Allow executable go scripts, but fix them prior to parse
s = strings.Replace(s, "#!", "//", 1)
}

i.Name = args[0]
if _, err := i.Eval(s); err != nil {
fmt.Println(err)
if p, ok := err.(interp.Panic); ok {
fmt.Println(string(p.Stack))
}
} else {
}

if interactive {
i.REPL(os.Stdin, os.Stdout)
}
}

0 comments on commit d29b0a4

Please sign in to comment.