ucl/repl/repl.go

54 lines
1 KiB
Go
Raw Normal View History

2024-12-11 09:47:05 +00:00
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),
}
2024-12-11 10:16:08 +00:00
r.SetCommand("help", r.helpBuiltin, Doc{
Brief: "displays help about a command",
2024-12-11 10:23:53 +00:00
Usage: "[command]",
Args: []ArgDoc{
{Name: "command", Brief: "command to display detailed help for"},
},
2024-12-11 10:16:08 +00:00
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.
`,
})
2024-12-11 09:47:05 +00:00
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)
}