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"
|
"fmt"
|
||||||
"github.com/lmika/gopkgs/fp/maps"
|
"github.com/lmika/gopkgs/fp/maps"
|
||||||
"os"
|
"os"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
@ -13,8 +14,15 @@ import (
|
||||||
"unicode"
|
"unicode"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ArgDoc struct {
|
||||||
|
Name string
|
||||||
|
Brief string
|
||||||
|
}
|
||||||
|
|
||||||
type Doc struct {
|
type Doc struct {
|
||||||
Brief string
|
Brief string
|
||||||
|
Usage string
|
||||||
|
Args []ArgDoc
|
||||||
Detailed string
|
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", cmdName)
|
||||||
fmt.Printf(" %v\n", docs.Brief)
|
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 != "" {
|
if docs.Detailed != "" {
|
||||||
fmt.Println("")
|
fmt.Println("\nDetails:")
|
||||||
fmt.Println("Details:")
|
|
||||||
|
|
||||||
lines := strings.Split(docs.Detailed, "\n")
|
lines := strings.Split(docs.Detailed, "\n")
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,10 @@ func New(opts ...ucl.InstOption) *REPL {
|
||||||
}
|
}
|
||||||
r.SetCommand("help", r.helpBuiltin, Doc{
|
r.SetCommand("help", r.helpBuiltin, Doc{
|
||||||
Brief: "displays help about a command",
|
Brief: "displays help about a command",
|
||||||
|
Usage: "[command]",
|
||||||
|
Args: []ArgDoc{
|
||||||
|
{Name: "command", Brief: "command to display detailed help for"},
|
||||||
|
},
|
||||||
Detailed: `
|
Detailed: `
|
||||||
When used without arguments, 'help' will display the list of known commands,
|
When used without arguments, 'help' will display the list of known commands,
|
||||||
along with a brief description on what each one does.
|
along with a brief description on what each one does.
|
||||||
|
|
Loading…
Reference in a new issue