From 48cf155c3231ea9849c505d3c73bbcdbc86f851b Mon Sep 17 00:00:00 2001 From: Aliwoto Date: Mon, 23 Sep 2024 23:49:50 +0330 Subject: [PATCH] Upgrade dependencies to their latest versions. Add ParseArgWithOptions function and support parse options. Signed-off-by: Aliwoto --- argparser/helpers.go | 71 +++++++++++++++++++++----------------------- argparser/methods.go | 2 +- argparser/types.go | 23 ++++++++++++++ go.mod | 4 +-- go.sum | 10 ++++--- 5 files changed, 66 insertions(+), 44 deletions(-) diff --git a/argparser/helpers.go b/argparser/helpers.go index bcd44ba..ca4f924 100644 --- a/argparser/helpers.go +++ b/argparser/helpers.go @@ -9,63 +9,54 @@ import ( "errors" "strings" - ws "github.com/AnimeKaizoku/ssg/ssg" + ws "github.com/ALiwoto/ssg/ssg" ) -// Flag is the options passed along with the commands -// by users. they should send them with prefix "--", -// but we will remove them in the pTools. -type Flag struct { - name string - index int - value interface{} - fType FlagType - emptyT bool -} - -type EventArgs struct { - prefixes []rune - command string // command without '/' or '!' - flags []Flag - rawData string - firstValue string +func ParseArg(text string, prefixes []rune) (e *EventArgs, err error) { + return ParseArgWithOptions(text, &ParseOptions{ + Prefixes: prefixes, + }) } -// ParseArg will parse the whole text into an EventArg and will return it. -func ParseArg(text string, prefixes []rune) (e *EventArgs, err error) { +// ParseArgWithOptions will parse the whole text into an EventArg and will return it. +func ParseArgWithOptions(text string, options *ParseOptions) (e *EventArgs, err error) { if text == "" { - return nil, errors.New("text cannot be empty") + return nil, errors.New("ParseArgWithOptions: text cannot be empty") + } + + if options == nil { + options = GetDefaultParseOptions() } - if len(prefixes) == 0 { - prefixes = DefaultPrefixes + if len(options.Prefixes) == 0 { + options.Prefixes = DefaultPrefixes } ss := ws.Ss(text) - if !ss.HasRunePrefix(prefixes...) { - return nil, errors.New("this message is not a command at all") + if !ss.HasRunePrefix(options.Prefixes...) { + return nil, errors.New("ParseArgWithOptions: input text is not a command") } cmdR := ss.SplitStr(ws.SPACE_VALUE) if len(cmdR) == ws.BaseIndex { - return nil, errors.New("wasn't able to get the command") + return nil, errors.New("ParseArgWithOptions: unable to get the command") } cmd := cmdR[ws.BaseIndex] if cmd.IsEmpty() { - return nil, errors.New("length of the command cannot be zero") + return nil, errors.New("ParseArgWithOptions: length of the command cannot be zero") } - cmdSs := cmd.TrimStr(toStrArray(prefixes)...) + cmdSs := cmd.TrimStr(toStrArray(options.Prefixes)...) if cmdSs.IsEmpty() { - return nil, errors.New("command cannot be only whitespace") + return nil, errors.New("ParseArgWithOptions: command cannot be only whitespace") } cmdStr := cmdSs.GetValue() e = &EventArgs{ - command: cmdStr, - prefixes: prefixes, + command: cmdStr, + options: options, } // lock the special characters such as "--", ":", "=". @@ -73,13 +64,13 @@ func ParseArg(text string, prefixes []rune) (e *EventArgs, err error) { tmpOSs := ss.SplitStr(ws.FLAG_PREFIX) // check if we have any flags or not. - // I think this if is not necessary actually, + // I think this is not necessary actually, // but I just added it to prevent some cases of // panics. and also it will reduce the time order // I guess. if len(tmpOSs) < ws.BaseTwoIndex { // please notice that we should send the original - // text to this method. + // text to this function. // because our locked QString contains JA characters // and should not be used here. lookRaw(&text, e) @@ -151,7 +142,13 @@ func ParseArg(text string, prefixes []rune) (e *EventArgs, err error) { } func ParseArgDefault(text string) (e *EventArgs, err error) { - return ParseArg(text, DefaultPrefixes) + return ParseArgWithOptions(text, GetDefaultParseOptions()) +} + +func GetDefaultParseOptions() *ParseOptions { + return &ParseOptions{ + Prefixes: DefaultPrefixes, + } } func toStrArray(r []rune) []string { @@ -177,12 +174,12 @@ func fixTmpStr(tmp string) string { // please use this function when and only when // no flags are provided for our commands. func lookRaw(text *string, e *EventArgs) { - owoStr := strings.SplitN(*text, e.command, ws.BaseTwoIndex) - if len(owoStr) < ws.BaseTwoIndex { + myStr := strings.SplitN(*text, e.command, ws.BaseTwoIndex) + if len(myStr) < ws.BaseTwoIndex { return } - tmp := strings.Join(owoStr[ws.BaseOneIndex:], ws.EMPTY) + tmp := strings.Join(myStr[ws.BaseOneIndex:], ws.EMPTY) tmp = strings.TrimSpace(tmp) e.rawData = tmp diff --git a/argparser/methods.go b/argparser/methods.go index 4ac282b..9ac2f78 100644 --- a/argparser/methods.go +++ b/argparser/methods.go @@ -9,7 +9,7 @@ import ( "strconv" "strings" - ws "github.com/AnimeKaizoku/ssg/ssg" + ws "github.com/ALiwoto/ssg/ssg" ) //--------------------------------------------------------- diff --git a/argparser/types.go b/argparser/types.go index 9a2f31b..5489737 100644 --- a/argparser/types.go +++ b/argparser/types.go @@ -6,3 +6,26 @@ package argparser type FlagType uint8 + +// Flag is the options passed along with the commands +// by users. they should send them with prefix "--", +// but we will remove them in the pTools. +type Flag struct { + name string + index int + value interface{} + fType FlagType + emptyT bool +} + +type EventArgs struct { + options *ParseOptions + command string // command without '/' or '!' + flags []Flag + rawData string + firstValue string +} + +type ParseOptions struct { + Prefixes []rune +} diff --git a/go.mod b/go.mod index 98048a4..efa7a40 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,6 @@ module github.com/ALiwoto/argparser go 1.18 -require github.com/AnimeKaizoku/ssg v1.1.3 +require github.com/ALiwoto/ssg v1.1.38 -require golang.org/x/text v0.3.7 // indirect +require golang.org/x/text v0.18.0 // indirect diff --git a/go.sum b/go.sum index be95890..1b801e2 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ -github.com/AnimeKaizoku/ssg v1.1.3 h1:J33RtTU54sdAiabG8vo9Nqo4lITVOw0IkhIVol36RbM= -github.com/AnimeKaizoku/ssg v1.1.3/go.mod h1:pe9BzQLW45VHgs4Puq1WcvnY+G5r07ecg2+MIycC1OM= -golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +github.com/ALiwoto/ssg v1.1.38 h1:nVGsObrzLv174qzNva29Ki9x7xkmtUWsX3cMEimT/Bo= +github.com/ALiwoto/ssg v1.1.38/go.mod h1:977/edS++uXWeyaTA9giTWPi8QubiIkb6ij0+0tC9gU= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224= +golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=