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/compose/multipart.go | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) (limited to 'commands/compose/multipart.go') diff --git a/commands/compose/multipart.go b/commands/compose/multipart.go index 0ad1dc4d..96941062 100644 --- a/commands/compose/multipart.go +++ b/commands/compose/multipart.go @@ -7,10 +7,12 @@ import ( "git.sr.ht/~rjarry/aerc/app" "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/config" - "git.sr.ht/~sircmpwn/getopt" ) -type Multipart struct{} +type Multipart struct { + Remove bool `opt:"-d"` + Mime string `opt:"mime" metavar:""` +} func init() { register(Multipart{}) @@ -29,37 +31,21 @@ func (Multipart) Complete(args []string) []string { return commands.CompletionFromList(completions, args) } -func (a Multipart) Execute(args []string) error { +func (m Multipart) Execute(args []string) error { composer, ok := app.SelectedTabContent().(*app.Composer) if !ok { return fmt.Errorf(":multipart is only available on the compose::review screen") } - opts, optind, err := getopt.Getopts(args, "d") - if err != nil { - return fmt.Errorf("Usage: :multipart [-d] ") - } - var remove bool = false - for _, opt := range opts { - if opt.Option == 'd' { - remove = true - } - } - args = args[optind:] - if len(args) != 1 { - return fmt.Errorf("Usage: :multipart [-d] ") - } - mime := args[0] - - if remove { - return composer.RemovePart(mime) + if m.Remove { + return composer.RemovePart(m.Mime) } else { - _, found := config.Converters[mime] + _, found := config.Converters[m.Mime] if !found { - return fmt.Errorf("no command defined for MIME type: %s", mime) + return fmt.Errorf("no command defined for MIME type: %s", m.Mime) } - err = composer.AppendPart( - mime, + err := composer.AppendPart( + m.Mime, map[string]string{"Charset": "UTF-8"}, // the actual content of the part will be rendered // every time the body of the email is updated -- cgit