From e54486ee40c9cedde9d4dae3e89d8b5f932188ee Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Tue, 3 Oct 2023 22:12:10 +0200 Subject: commands: parse arguments with go-opt Use the argument parsing framework introduced earlier to unify the parsing of (almost) all command options. Remove custom parsing code and to avoid extraneous types, add fields with `opt` tags on command structs that have options and arguments. Commands that take no argument do not need anything. Since the command objects now carry data, create a new temporary instance of them before passing them to opt.ArgsToStruct when executing a command. A few of the commands use specific semantics for parsing (:choose), or are delegating argument parsing to another function (:sort, :search, :filter). For these commands, simply add a dummy "-" passthrough argument. Since all commands still have the argument list (after split) nothing needs to be changed in this area. There should be no functional change besides the Usage strings and reported errors which are now generated automatically. Signed-off-by: Robin Jarry Reviewed-by: Koni Marti Tested-by: Moritz Poldrack Tested-by: Inwit --- commands/help.go | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'commands/help.go') diff --git a/commands/help.go b/commands/help.go index 5cc0aacc..b2bcdf7c 100644 --- a/commands/help.go +++ b/commands/help.go @@ -1,12 +1,14 @@ package commands import ( - "errors" + "fmt" "git.sr.ht/~rjarry/aerc/app" ) -type Help struct{} +type Help struct { + Topic string `opt:"topic" action:"ParseTopic" default:"aerc"` +} var pages = []string{ "aerc", @@ -37,15 +39,21 @@ func (Help) Complete(args []string) []string { return CompletionFromList(pages, args) } -func (Help) Execute(args []string) error { - page := "aerc" - if len(args) == 2 && args[1] != "aerc" { - page = "aerc-" + args[1] - } else if len(args) > 2 { - return errors.New("Usage: help [topic]") +func (h *Help) ParseTopic(arg string) error { + for _, page := range pages { + if arg == page { + if arg != "aerc" { + arg = "aerc-" + arg + } + h.Topic = arg + return nil + } } + return fmt.Errorf("unknown topic %q", arg) +} - if page == "aerc-keys" { +func (h Help) Execute(args []string) error { + if h.Topic == "aerc-keys" { app.AddDialog(app.NewDialog( app.NewListBox( "Bindings: Press or to close. "+ @@ -61,6 +69,6 @@ func (Help) Execute(args []string) error { )) return nil } - - return TermCore([]string{"term", "man", page}) + term := Term{Cmd: []string{"man", h.Topic}} + return term.Execute(args) } -- cgit