From 85c13265265d21268f03a8106f61aa0e465b9f4e Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Tue, 5 May 2020 23:23:52 -0700 Subject: [PATCH] Bot: Added ArgsParser for each Context instead of global --- bot/arguments.go | 6 ------ bot/ctx.go | 15 ++++++++++++++- bot/ctx_call.go | 2 +- bot/ctx_test.go | 1 + bot/{ => extras}/shellwords/LICENSE | 0 bot/{ => extras}/shellwords/shellwords.go | 0 6 files changed, 16 insertions(+), 8 deletions(-) rename bot/{ => extras}/shellwords/LICENSE (100%) rename bot/{ => extras}/shellwords/shellwords.go (100%) diff --git a/bot/arguments.go b/bot/arguments.go index 8a62454..a12baee 100644 --- a/bot/arguments.go +++ b/bot/arguments.go @@ -5,8 +5,6 @@ import ( "reflect" "strconv" "strings" - - "github.com/diamondburned/arikawa/bot/shellwords" ) type argumentValueFn func(string) (reflect.Value, error) @@ -117,10 +115,6 @@ var ShellwordsEscaper = strings.NewReplacer( "\\", "\\\\", ) -var ParseArgs = func(args string) ([]string, error) { - return shellwords.Parse(args) -} - // nilV, only used to return an error var nilV = reflect.Value{} diff --git a/bot/ctx.go b/bot/ctx.go index 582cd59..8b6da4d 100644 --- a/bot/ctx.go +++ b/bot/ctx.go @@ -7,6 +7,7 @@ import ( "strings" "sync" + "github.com/diamondburned/arikawa/bot/extras/shellwords" "github.com/diamondburned/arikawa/gateway" "github.com/diamondburned/arikawa/state" "github.com/pkg/errors" @@ -29,7 +30,15 @@ func NewPrefix(prefixes ...string) Prefixer { } } -// TODO: add variadic arguments +// ArgsParser is the function type for parsing message content into fields, +// usually delimited by spaces. +type ArgsParser func(content string) ([]string, error) + +// DefaultArgsParser implements a parser similar to that of shell's, +// implementing quotes as well as escapes. +func DefaultArgsParser() ArgsParser { + return shellwords.Parse +} // Context is the bot state for commands and subcommands. // @@ -73,6 +82,9 @@ type Context struct { // Descriptive help body Description string + // Called to parse message content, default to DefaultArgsParser(). + ParseArgs ArgsParser + // Called to check a message's prefix. The default prefix is "!". Refer to // NewPrefix(). HasPrefix Prefixer @@ -178,6 +190,7 @@ func New(s *state.State, cmd interface{}) (*Context, error) { ctx := &Context{ Subcommand: c, State: s, + ParseArgs: DefaultArgsParser(), HasPrefix: NewPrefix("~"), FormatError: func(err error) string { // Escape all pings, including @everyone. diff --git a/bot/ctx_call.go b/bot/ctx_call.go index 1db3d4a..d50326c 100644 --- a/bot/ctx_call.go +++ b/bot/ctx_call.go @@ -154,7 +154,7 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent) error { } // parse arguments - parts, err := ParseArgs(content) + parts, err := ctx.ParseArgs(content) if err != nil { return errors.Wrap(err, "Failed to parse command") } diff --git a/bot/ctx_test.go b/bot/ctx_test.go index c7702da..5a835a4 100644 --- a/bot/ctx_test.go +++ b/bot/ctx_test.go @@ -90,6 +90,7 @@ func TestContext(t *testing.T) { var ctx = &Context{ Subcommand: s, State: state, + ParseArgs: DefaultArgsParser(), } t.Run("init commands", func(t *testing.T) { diff --git a/bot/shellwords/LICENSE b/bot/extras/shellwords/LICENSE similarity index 100% rename from bot/shellwords/LICENSE rename to bot/extras/shellwords/LICENSE diff --git a/bot/shellwords/shellwords.go b/bot/extras/shellwords/shellwords.go similarity index 100% rename from bot/shellwords/shellwords.go rename to bot/extras/shellwords/shellwords.go