Added help command
This commit is contained in:
parent
23be2593f3
commit
b9f349d1d7
31
repl/docs.go
31
repl/docs.go
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/lmika/gopkgs/fp/maps"
|
||||
"os"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
@ -13,8 +14,15 @@ import (
|
|||
"unicode"
|
||||
)
|
||||
|
||||
type ArgDoc struct {
|
||||
Name string
|
||||
Brief string
|
||||
}
|
||||
|
||||
type Doc struct {
|
||||
Brief string
|
||||
Usage string
|
||||
Args []ArgDoc
|
||||
Detailed string
|
||||
}
|
||||
|
||||
|
@ -54,9 +62,28 @@ func (r *REPL) helpBuiltin(ctx context.Context, args ucl.CallArgs) (any, error)
|
|||
fmt.Printf("%v\n", cmdName)
|
||||
fmt.Printf(" %v\n", docs.Brief)
|
||||
|
||||
if docs.Usage != "" {
|
||||
fmt.Println("\nUsage:")
|
||||
fmt.Printf(" %v %v\n", cmdName, docs.Usage)
|
||||
}
|
||||
|
||||
if len(docs.Args) > 0 {
|
||||
fmt.Println("\nArguments:")
|
||||
|
||||
docArgs := slices.Clone(docs.Args)
|
||||
sort.Slice(docArgs, func(i, j int) bool {
|
||||
return strings.TrimPrefix(docs.Args[i].Name, "-") < strings.TrimPrefix(docs.Args[j].Name, "-")
|
||||
})
|
||||
|
||||
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||
for _, arg := range docArgs {
|
||||
fmt.Fprintf(tw, " %v\t %v\n", arg.Name, arg.Brief)
|
||||
}
|
||||
tw.Flush()
|
||||
}
|
||||
|
||||
if docs.Detailed != "" {
|
||||
fmt.Println("")
|
||||
fmt.Println("Details:")
|
||||
fmt.Println("\nDetails:")
|
||||
|
||||
lines := strings.Split(docs.Detailed, "\n")
|
||||
|
||||
|
|
|
@ -23,6 +23,10 @@ func New(opts ...ucl.InstOption) *REPL {
|
|||
}
|
||||
r.SetCommand("help", r.helpBuiltin, Doc{
|
||||
Brief: "displays help about a command",
|
||||
Usage: "[command]",
|
||||
Args: []ArgDoc{
|
||||
{Name: "command", Brief: "command to display detailed help for"},
|
||||
},
|
||||
Detailed: `
|
||||
When used without arguments, 'help' will display the list of known commands,
|
||||
along with a brief description on what each one does.
|
||||
|
|
Loading…
Reference in a new issue