ucl/repl/repl.go
2024-12-11 21:16:08 +11:00

50 lines
938 B
Go

package repl
import (
"ucl.lmika.dev/ucl"
)
type CommandOpt interface {
config(cmdName string, r *REPL)
}
type REPL struct {
inst *ucl.Inst
commandDocs map[string]Doc
}
func New(opts ...ucl.InstOption) *REPL {
inst := ucl.New(opts...)
r := &REPL{
inst: inst,
commandDocs: make(map[string]Doc),
}
r.SetCommand("help", r.helpBuiltin, Doc{
Brief: "displays help about a command",
Detailed: `
When used without arguments, 'help' will display the list of known commands,
along with a brief description on what each one does.
When used with an argument, 'help' will display a more detailed explanation
of what each command does.
`,
})
return r
}
func (r *REPL) Inst() *ucl.Inst {
return r.inst
}
func (r *REPL) SetCommand(name string, fn ucl.BuiltinHandler, opts ...CommandOpt) {
r.commandDocs[name] = Doc{}
for _, opt := range opts {
opt.config(name, r)
}
r.inst.SetBuiltin(name, fn)
}