Added help command

This commit is contained in:
Leon Mika 2024-12-11 21:16:08 +11:00
parent 5b4e45ff80
commit e0db900e4e
4 changed files with 120 additions and 28 deletions

View file

@ -1,10 +1,6 @@
package repl
import (
"context"
"fmt"
"github.com/lmika/gopkgs/fp/maps"
"sort"
"ucl.lmika.dev/ucl"
)
@ -25,7 +21,16 @@ func New(opts ...ucl.InstOption) *REPL {
inst: inst,
commandDocs: make(map[string]Doc),
}
inst.SetBuiltin("help", r.helpBuiltin)
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
}
@ -42,23 +47,3 @@ func (r *REPL) SetCommand(name string, fn ucl.BuiltinHandler, opts ...CommandOpt
r.inst.SetBuiltin(name, fn)
}
func (r *REPL) helpBuiltin(ctx context.Context, args ucl.CallArgs) (any, error) {
switch {
case args.NArgs() == 0:
// TEMP
names := maps.Keys(r.commandDocs)
sort.Strings(names)
for _, name := range names {
cdoc := r.commandDocs[name]
if cdoc.Brief != "" {
fmt.Println("%v\t%v", name, r.commandDocs[name])
} else {
fmt.Println("%v", name)
}
}
// END TEMP
}
return nil, nil
}