From b9f349d1d7478fb6ef0a4ea338fcfd7d47cdd6a9 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Wed, 11 Dec 2024 21:23:53 +1100 Subject: [PATCH] Added help command --- repl/docs.go | 31 +++++++++++++++++++++++++++++-- repl/repl.go | 4 ++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/repl/docs.go b/repl/docs.go index b3ea160..40e3485 100644 --- a/repl/docs.go +++ b/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") diff --git a/repl/repl.go b/repl/repl.go index 387354d..b9cf19e 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -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.