commit 073f395

Hubert Hirtz  ·  2021-02-18 16:17:04 +0000 UTC
parent fc101c7
Fix command argument parsing
1 files changed,  +19, -13
+19, -13
  1@@ -10,8 +10,9 @@ import (
  2 )
  3 
  4 type command struct {
  5-	MinArgs   int
  6 	AllowHome bool
  7+	MinArgs   int
  8+	MaxArgs   int
  9 	Usage     string
 10 	Desc      string
 11 	Handle    func(app *App, buffer string, args []string) error
 12@@ -25,17 +26,20 @@ func init() {
 13 	commands = commandSet{
 14 		"": {
 15 			MinArgs: 1,
 16+			MaxArgs: 1,
 17 			Handle:  commandDo,
 18 		},
 19 		"HELP": {
 20 			AllowHome: true,
 21+			MaxArgs:   1,
 22 			Usage:     "[command]",
 23 			Desc:      "show the list of commands, or how to use the given one",
 24 			Handle:    commandDoHelp,
 25 		},
 26 		"JOIN": {
 27-			MinArgs:   1,
 28 			AllowHome: true,
 29+			MinArgs:   1,
 30+			MaxArgs:   2,
 31 			Usage:     "<channels> [keys]",
 32 			Desc:      "join a channel",
 33 			Handle:    commandDoJoin,
 34@@ -43,6 +47,7 @@ func init() {
 35 		"ME": {
 36 			AllowHome: true,
 37 			MinArgs:   1,
 38+			MaxArgs:   1,
 39 			Usage:     "<message>",
 40 			Desc:      "send an action (reply to last query if sent from home)",
 41 			Handle:    commandDoMe,
 42@@ -50,6 +55,7 @@ func init() {
 43 		"MSG": {
 44 			AllowHome: true,
 45 			MinArgs:   2,
 46+			MaxArgs:   2,
 47 			Usage:     "<target> <message>",
 48 			Desc:      "send a message to the given target",
 49 			Handle:    commandDoMsg,
 50@@ -60,19 +66,22 @@ func init() {
 51 		},
 52 		"PART": {
 53 			AllowHome: true,
 54+			MaxArgs:   2,
 55 			Usage:     "[channel] [reason]",
 56 			Desc:      "part a channel",
 57 			Handle:    commandDoPart,
 58 		},
 59 		"QUIT": {
 60 			AllowHome: true,
 61+			MaxArgs:   1,
 62 			Usage:     "[reason]",
 63 			Desc:      "quit senpai",
 64 			Handle:    commandDoQuit,
 65 		},
 66 		"QUOTE": {
 67-			MinArgs:   1,
 68 			AllowHome: true,
 69+			MinArgs:   1,
 70+			MaxArgs:   1,
 71 			Usage:     "<raw message>",
 72 			Desc:      "send raw protocol data",
 73 			Handle:    commandDoQuote,
 74@@ -80,14 +89,16 @@ func init() {
 75 		"REPLY": {
 76 			AllowHome: true,
 77 			MinArgs:   1,
 78+			MaxArgs:   1,
 79 			Usage:     "<message>",
 80 			Desc:      "reply to the last query",
 81 			Handle:    commandDoR,
 82 		},
 83 		"TOPIC": {
 84-			Usage:  "[topic]",
 85-			Desc:   "show or set the topic of the current channel",
 86-			Handle: commandDoTopic,
 87+			MaxArgs: 1,
 88+			Usage:   "[topic]",
 89+			Desc:    "show or set the topic of the current channel",
 90+			Handle:  commandDoTopic,
 91 		},
 92 	}
 93 }
 94@@ -109,7 +120,6 @@ func commandDo(app *App, buffer string, args []string) (err error) {
 95 }
 96 
 97 func commandDoHelp(app *App, buffer string, args []string) (err error) {
 98-	// TODO
 99 	t := time.Now()
100 	if len(args) == 0 {
101 		app.win.AddLine(app.win.CurrentBuffer(), false, ui.Line{
102@@ -352,12 +362,8 @@ func (app *App) handleInput(buffer, content string) error {
103 	cmd := commands[chosenCMDName]
104 
105 	var args []string
106-	if rawArgs == "" {
107-		args = nil
108-	} else if cmd.MinArgs == 0 {
109-		args = []string{rawArgs}
110-	} else {
111-		args = strings.SplitN(rawArgs, " ", cmd.MinArgs)
112+	if rawArgs != "" && cmd.MaxArgs != 0 {
113+		args = strings.SplitN(rawArgs, " ", cmd.MaxArgs)
114 	}
115 
116 	if len(args) < cmd.MinArgs {