aboutsummaryrefslogtreecommitdiffstats
path: root/commands/msg/mark.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-10-03 22:12:10 +0200
committerRobin Jarry <robin@jarry.cc>2023-10-28 19:24:49 +0200
commite54486ee40c9cedde9d4dae3e89d8b5f932188ee (patch)
tree254f032ef94920c28fbb9be8f918e61082a2015d /commands/msg/mark.go
parent9a4518476d8c8f28340c6b44cd808e6d58fbeb98 (diff)
downloadaerc-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/mark.go')
-rw-r--r--commands/msg/mark.go68
1 files changed, 22 insertions, 46 deletions
diff --git a/commands/msg/mark.go b/commands/msg/mark.go
index eeaa5485..c2c21cf2 100644
--- a/commands/msg/mark.go
+++ b/commands/msg/mark.go
@@ -2,11 +2,15 @@ package msg
import (
"fmt"
-
- "git.sr.ht/~sircmpwn/getopt"
)
-type Mark struct{}
+type Mark struct {
+ All bool `opt:"-a" aliases:"mark,unmark"`
+ Toggle bool `opt:"-t" aliases:"mark,unmark"`
+ Visual bool `opt:"-v" aliases:"mark,unmark"`
+ VisualClear bool `opt:"-V" aliases:"mark,unmark"`
+ Thread bool `opt:"-T" aliases:"mark,unmark"`
+}
func init() {
register(Mark{})
@@ -20,7 +24,7 @@ func (Mark) Complete(args []string) []string {
return nil
}
-func (Mark) Execute(args []string) error {
+func (m Mark) Execute(args []string) error {
h := newHelper()
OnSelectedMessage := func(fn func(uint32)) error {
if fn == nil {
@@ -38,63 +42,38 @@ func (Mark) Execute(args []string) error {
return err
}
marker := store.Marker()
- opts, _, err := getopt.Getopts(args, "atvVT")
- if err != nil {
- return err
- }
- var all bool
- var toggle bool
- var visual bool
- var clearVisual bool
- var thread bool
- for _, opt := range opts {
- switch opt.Option {
- case 'a':
- all = true
- case 'v':
- visual = true
- clearVisual = true
- case 'V':
- visual = true
- case 't':
- toggle = true
- case 'T':
- thread = true
- }
- }
- if thread && all {
+ if m.Thread && m.All {
return fmt.Errorf("-a and -T are mutually exclusive")
}
- if thread && visual {
+ if m.Thread && (m.Visual || m.VisualClear) {
return fmt.Errorf("-v and -T are mutually exclusive")
}
+ if m.Visual && m.All {
+ return fmt.Errorf("-a and -v are mutually exclusive")
+ }
switch args[0] {
case "mark":
- if all && visual {
- return fmt.Errorf("-a and -v are mutually exclusive")
- }
-
var modFunc func(uint32)
- if toggle {
+ if m.Toggle {
modFunc = marker.ToggleMark
} else {
modFunc = marker.Mark
}
switch {
- case all:
+ case m.All:
uids := store.Uids()
for _, uid := range uids {
modFunc(uid)
}
return nil
- case visual:
- marker.ToggleVisualMark(clearVisual)
+ case m.Visual || m.VisualClear:
+ marker.ToggleVisualMark(m.VisualClear)
return nil
default:
- if thread {
+ if m.Thread {
threadPtr, err := store.SelectedThread()
if err != nil {
return err
@@ -109,22 +88,22 @@ func (Mark) Execute(args []string) error {
}
case "unmark":
- if visual {
+ if m.Visual || m.VisualClear {
return fmt.Errorf("visual mode not supported for this command")
}
switch {
- case all && toggle:
+ case m.All && m.Toggle:
uids := store.Uids()
for _, uid := range uids {
marker.ToggleMark(uid)
}
return nil
- case all && !toggle:
+ case m.All && !m.Toggle:
marker.ClearVisualMark()
return nil
default:
- if thread {
+ if m.Thread {
threadPtr, err := store.SelectedThread()
if err != nil {
return err
@@ -138,9 +117,6 @@ func (Mark) Execute(args []string) error {
return nil
}
case "remark":
- if all || visual || toggle || thread {
- return fmt.Errorf("Usage: :remark")
- }
marker.Remark()
return nil
}