ucl/cmd/cmsh/main.go
2024-12-11 21:16:08 +11:00

48 lines
831 B
Go

package main
import (
"context"
"fmt"
"github.com/chzyer/readline"
"log"
"ucl.lmika.dev/repl"
"ucl.lmika.dev/ucl"
"ucl.lmika.dev/ucl/builtins"
)
func main() {
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
instRepl := repl.New(
ucl.WithModule(builtins.OS()),
ucl.WithModule(builtins.FS(nil)),
)
ctx := context.Background()
instRepl.SetCommand("hello", func(ctx context.Context, args ucl.CallArgs) (any, error) {
fmt.Println("hello")
return nil, nil
}, repl.Doc{
Brief: "displays hello",
Detailed: `
This displays the message 'hello' to the terminal.
It then terminates.
`,
})
for {
line, err := rl.Readline()
if err != nil { // io.EOF
break
}
if err := instRepl.EvalAndDisplay(ctx, line); err != nil {
log.Printf("%T: %v", err, err)
}
}
}