Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid commands #1053

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/execute/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
anonymizedInvalidVerb = "{invalid verb}"

lineLimitToShowFilter = 16

invalidCmdWithUsage = "error: unknown option `%s`\nusage: %s"
)

var newLinePattern = regexp.MustCompile(`\r?\n`)
Expand Down Expand Up @@ -168,8 +170,9 @@ func (e *DefaultExecutor) Execute(ctx context.Context) interactive.CoreMessage {
reportedCmd = fmt.Sprintf("%s {invalid feature}", reportedCmd)
}
e.reportCommand(ctx, "", reportedCmd, false, cmdCtx)
msg := e.cmdsMapping.HelpMessageForVerb(cmdVerb)
return respond(msg, cmdCtx)
helpMsg := e.cmdsMapping.HelpMessageForVerb(cmdVerb)
responseMsg := fmt.Sprintf(invalidCmdWithUsage, cmdRes, helpMsg)
return respond(responseMsg, cmdCtx)
} else {
cmdToReport := string(cmdVerb)
if cmdRes != "" {
Expand Down
9 changes: 7 additions & 2 deletions pkg/execute/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

const (
helpMsgHeader = "%s %s [feature]\n\nAvailable features:\n"
helpMsgHeaderWithFeatures = "%s %s [feature]\n\nAvailable features:\n"
helpMsgHeader = "%s %s"
// noFeature is used for commands that have no features defined
noFeature = ""
// incompleteCmdMsg incomplete command response message
Expand Down Expand Up @@ -127,8 +128,12 @@ func (m *CommandMapping) HelpMessageForVerb(verb command.Verb) string {
}
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 3, 0, 1, ' ', 0)
if len(cmd) > 0 && cmd[0].Name != "" {
fmt.Fprintf(w, helpMsgHeaderWithFeatures, api.MessageBotNamePlaceholder, verb)
} else {
fmt.Fprintf(w, helpMsgHeader, api.MessageBotNamePlaceholder, verb)
}

fmt.Fprintf(w, helpMsgHeader, api.MessageBotNamePlaceholder, verb)
for _, feature := range cmd {
aliases := removeEmptyFeatures(feature.Aliases)
fmtStr := fmt.Sprintf("%s\t", feature.Name)
Expand Down