Merge pull request 'Improved command error messages' (#189) from improve-command-error-messages into release2.3.3

Reviewed-on: #189
This commit is contained in:
Sloom Sloum Sluom IV 2020-10-24 16:42:19 +00:00
commit d2e238baac
2 changed files with 60 additions and 22 deletions

View File

@ -346,21 +346,16 @@ func (c *client) simpleCommand(action string) {
case "HELP", "?": case "HELP", "?":
go c.Visit(helplocation) go c.Visit(helplocation)
default: default:
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true) c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage() c.DrawMessage()
} }
} }
func (c *client) doCommand(action string, values []string) { func (c *client) doCommand(action string, values []string) {
if length := len(values); length != 1 {
c.SetMessage(fmt.Sprintf("Expected 1 argument, received %d", len(values)), true)
c.DrawMessage()
return
}
switch action { switch action {
case "CHECK", "C": case "C", "CHECK":
c.displayConfigValue(values[0]) c.displayConfigValue(values[0])
c.DrawMessage()
case "PURGE", "P": case "PURGE", "P":
err := c.Certs.Purge(values[0]) err := c.Certs.Purge(values[0])
if err != nil { if err != nil {
@ -405,24 +400,22 @@ func (c *client) doCommand(action string, values []string) {
c.saveFile(u, fn) c.saveFile(u, fn)
default: default:
c.SetMessage(fmt.Sprintf("Unknown action %q", action), true) c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage() c.DrawMessage()
} }
} }
func (c *client) doCommandAs(action string, values []string) { func (c *client) doCommandAs(action string, values []string) {
if len(values) < 2 {
c.SetMessage(fmt.Sprintf("Expected 2+ arguments, received %d", len(values)), true)
c.DrawMessage()
return
}
if values[0] == "." {
values[0] = c.PageState.History[c.PageState.Position].Location.Full
}
switch action { switch action {
case "ADD", "A": case "ADD", "A":
if len(values) < 2 {
c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage()
return
}
if values[0] == "." {
values[0] = c.PageState.History[c.PageState.Position].Location.Full
}
msg, err := c.BookMarks.Add(values) msg, err := c.BookMarks.Add(values)
if err != nil { if err != nil {
c.SetMessage(err.Error(), true) c.SetMessage(err.Error(), true)
@ -441,8 +434,18 @@ func (c *client) doCommandAs(action string, values []string) {
c.Draw() c.Draw()
} }
case "SEARCH": case "SEARCH":
if len(values) < 2 {
c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage()
return
}
c.search(strings.Join(values, " "), "", "") c.search(strings.Join(values, " "), "", "")
case "SET", "S": case "SET", "S":
if len(values) < 2 {
c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage()
return
}
if _, ok := c.Options[values[0]]; ok { if _, ok := c.Options[values[0]]; ok {
val := strings.Join(values[1:], " ") val := strings.Join(values[1:], " ")
if !validateOpt(values[0], val) { if !validateOpt(values[0], val) {
@ -473,7 +476,7 @@ func (c *client) doCommandAs(action string, values []string) {
c.SetMessage(fmt.Sprintf("Unable to set %s, it does not exist", values[0]), true) c.SetMessage(fmt.Sprintf("Unable to set %s, it does not exist", values[0]), true)
c.DrawMessage() c.DrawMessage()
default: default:
c.SetMessage(fmt.Sprintf("Unknown command structure"), true) c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage() c.DrawMessage()
} }
} }
@ -523,7 +526,7 @@ func (c *client) doLinkCommandAs(action, target string, values []string) {
out = append(out, values...) out = append(out, values...)
c.doCommandAs(action, out) c.doCommandAs(action, out)
default: default:
c.SetMessage(fmt.Sprintf("Unknown command structure"), true) c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage() c.DrawMessage()
} }
} }
@ -655,7 +658,7 @@ func (c *client) doLinkCommand(action, target string) {
} }
c.saveFile(u, fn) c.saveFile(u, fn)
default: default:
c.SetMessage("Unknown command structure", true) c.SetMessage(syntaxErrorMessage(action), true)
c.DrawMessage() c.DrawMessage()
} }
@ -1203,6 +1206,13 @@ func findAvailableFileName(fpath, fname string) (string, error) {
return savePath, nil return savePath, nil
} }
func syntaxErrorMessage(action string) string {
if val, ok := ERRS[action]; ok {
return fmt.Sprintf("Incorrect syntax. Try: %s", val)
}
return fmt.Sprintf("Unknown command %q", action)
}
func updateTimeouts(timeoutString string) error { func updateTimeouts(timeoutString string) error {
sec, err := strconv.Atoi(timeoutString) sec, err := strconv.Atoi(timeoutString)
if err != nil { if err != nil {

28
help.go Normal file
View File

@ -0,0 +1,28 @@
package main
// ERRS maps commands to their syntax error message
var ERRS = map[string]string{
"A": "`a [target] [name...]`",
"ADD": "`add [target] [name...]`",
"D": "`d [bookmark-id]`",
"DELETE": "`delete [bookmark-id]`",
"B": "`b [[bookmark-id]]`",
"BOOKMARKS": "`bookmarks [[bookmark-id]]`",
"C": "`c [link_id]` or `check [setting]`",
"CHECK": "`check [link_id]` or `check [setting]`",
"H": "`h`",
"HOME": "`home`",
"P": "`p [host]`",
"PURGE": "`purge [host]`",
"Q": "`q`",
"QUIT": "`quit`",
"R": "`r`",
"RELOAD": "`reload`",
"SEARCH": "`search [[keyword(s)...]]`",
"S": "`s [setting] [value]`",
"SET": "`set [setting] [value]`",
"W": "`w [target]`",
"WRITE": "`write [target]`",
"?": "`?`",
"HELP": "`help`",
}