diff options
author | Robin Jarry <robin@jarry.cc> | 2023-10-03 22:12:10 +0200 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-10-28 19:24:49 +0200 |
commit | e54486ee40c9cedde9d4dae3e89d8b5f932188ee (patch) | |
tree | 254f032ef94920c28fbb9be8f918e61082a2015d /commands/msg/forward.go | |
parent | 9a4518476d8c8f28340c6b44cd808e6d58fbeb98 (diff) | |
download | aerc-e54486ee40c9cedde9d4dae3e89d8b5f932188ee.tar.gz |
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 <robin@jarry.cc>
Reviewed-by: Koni Marti <koni.marti@gmail.com>
Tested-by: Moritz Poldrack <moritz@poldrack.dev>
Tested-by: Inwit <inwit@sindominio.net>
Diffstat (limited to 'commands/msg/forward.go')
-rw-r--r-- | commands/msg/forward.go | 54 |
1 files changed, 17 insertions, 37 deletions
diff --git a/commands/msg/forward.go b/commands/msg/forward.go index 68f162cc..e6e386a9 100644 --- a/commands/msg/forward.go +++ b/commands/msg/forward.go @@ -20,11 +20,16 @@ import ( "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/types" "github.com/emersion/go-message/mail" - - "git.sr.ht/~sircmpwn/getopt" ) -type forward struct{} +type forward struct { + AttachAll bool `opt:"-A"` + AttachFull bool `opt:"-F"` + Edit bool `opt:"-e"` + NoEdit bool `opt:"-E"` + Template string `opt:"-T"` + To []string `opt:"..." required:"false"` +} func init() { register(forward{}) @@ -38,36 +43,11 @@ func (forward) Complete(args []string) []string { return nil } -func (forward) Execute(args []string) error { - opts, optind, err := getopt.Getopts(args, "AFT:eE") - if err != nil { - return err - } - if len(args) != optind { - return errors.New("Usage: forward [-A|-F] [-T <template>] [-e|-E]") - } - attachAll := false - attachFull := false - template := "" - editHeaders := config.Compose.EditHeaders - for _, opt := range opts { - switch opt.Option { - case 'A': - attachAll = true - case 'F': - attachFull = true - case 'T': - template = opt.Value - case 'e': - editHeaders = true - case 'E': - editHeaders = false - } - } - - if attachAll && attachFull { +func (f forward) Execute(args []string) error { + if f.AttachAll && f.AttachFull { return errors.New("Options -A and -F are mutually exclusive") } + editHeaders := (config.Compose.EditHeaders || f.Edit) && !f.NoEdit widget := app.SelectedTabContent().(app.ProvidesMessage) acct := widget.SelectedAccount() @@ -89,7 +69,7 @@ func (forward) Execute(args []string) error { h.SetSubject(subject) var tolist []*mail.Address - to := strings.Join(args[optind:], ", ") + to := strings.Join(f.To, ", ") if strings.Contains(to, "@") { tolist, err = mail.ParseAddressList(to) if err != nil { @@ -109,7 +89,7 @@ func (forward) Execute(args []string) error { addTab := func() (*app.Composer, error) { composer, err := app.NewComposer(acct, acct.AccountConfig(), acct.Worker(), editHeaders, - template, h, &original, nil) + f.Template, h, &original, nil) if err != nil { app.PushError("Error: " + err.Error()) return nil, err @@ -124,7 +104,7 @@ func (forward) Execute(args []string) error { return composer, nil } - if attachFull { + if f.AttachFull { tmpDir, err := os.MkdirTemp("", "aerc-tmp-attachment") if err != nil { return err @@ -158,8 +138,8 @@ func (forward) Execute(args []string) error { }) }) } else { - if template == "" { - template = config.Templates.Forwards + if f.Template == "" { + f.Template = config.Templates.Forwards } part := lib.FindPlaintext(msg.BodyStructure, nil) @@ -186,7 +166,7 @@ func (forward) Execute(args []string) error { } // add attachments - if attachAll { + if f.AttachAll { var mu sync.Mutex parts := lib.FindAllNonMultipart(msg.BodyStructure, nil, nil) for _, p := range parts { |