From 0c2d116708502b9f0a2588c035a5ef1ecc7c0579 Mon Sep 17 00:00:00 2001 From: krau <71133316+krau@users.noreply.github.com> Date: Sun, 9 Nov 2025 11:43:28 +0800 Subject: [PATCH] feat: parse rule command with quotes respecting --- client/bot/handlers/rule.go | 3 ++- common/utils/strutil/string.go | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/client/bot/handlers/rule.go b/client/bot/handlers/rule.go index 2dfd085..df71228 100644 --- a/client/bot/handlers/rule.go +++ b/client/bot/handlers/rule.go @@ -10,13 +10,14 @@ import ( "github.com/charmbracelet/log" "github.com/duke-git/lancet/v2/slice" "github.com/krau/SaveAny-Bot/client/bot/handlers/utils/msgelem" + "github.com/krau/SaveAny-Bot/common/utils/strutil" "github.com/krau/SaveAny-Bot/database" "github.com/krau/SaveAny-Bot/pkg/rule" ) func handleRuleCmd(ctx *ext.Context, update *ext.Update) error { logger := log.FromContext(ctx) - args := strings.Split(update.EffectiveMessage.Text, " ") + args := strutil.ParseArgsRespectQuotes(update.EffectiveMessage.Text) userChatID := update.GetUserChat().GetID() user, err := database.GetUserByChatID(ctx, userChatID) if err != nil { diff --git a/common/utils/strutil/string.go b/common/utils/strutil/string.go index 8eb7f1f..a3e4f24 100644 --- a/common/utils/strutil/string.go +++ b/common/utils/strutil/string.go @@ -48,3 +48,41 @@ func ParseIntStrRange(input string, sep string) (int64, int64, error) { } return min, max, nil } + +func ParseArgsRespectQuotes(input string) []string { + var args []string + var current strings.Builder + inQuotes := false + escaped := false + + for _, r := range input { + switch { + case escaped: + current.WriteRune(r) + escaped = false + + case r == '\\': + escaped = true + + case r == '"': + inQuotes = !inQuotes + + case r == ' ' || r == '\t': + if inQuotes { + current.WriteRune(r) + } else if current.Len() > 0 { + args = append(args, current.String()) + current.Reset() + } + + default: + current.WriteRune(r) + } + } + + if current.Len() > 0 { + args = append(args, current.String()) + } + + return args +}