Skip to content

Commit

Permalink
Upgrade dependencies to their latest versions.
Browse files Browse the repository at this point in the history
Add ParseArgWithOptions function and support parse options.

Signed-off-by: Aliwoto <[email protected]>
  • Loading branch information
ALiwoto committed Sep 23, 2024
1 parent ae5cf95 commit 48cf155
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
71 changes: 34 additions & 37 deletions argparser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,68 @@ 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 "--", ":", "=".
ss.LockSpecial()

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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion argparser/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strconv"
"strings"

ws "github.com/AnimeKaizoku/ssg/ssg"
ws "github.com/ALiwoto/ssg/ssg"
)

//---------------------------------------------------------
Expand Down
23 changes: 23 additions & 0 deletions argparser/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 48cf155

Please sign in to comment.