Skip to content

Commit

Permalink
Change project struct and add more methods
Browse files Browse the repository at this point in the history
Signed-off-by: aliwoto <[email protected]>
  • Loading branch information
ALiwoto committed Nov 13, 2021
1 parent 382a4a4 commit a7dba40
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 228 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/go-linux.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Go

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Test
run: go test -v ./...
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.env
.docker_build/
bin/
*.exe
vendor/
sibylBinary
PsychoPass
sibylSystem
*.old.go
woto_files/
tmp_files/
labs/
*.old.go
*.db
*.log

# ignore debug binary file
__debug_bin

# ignore config file
config.ini
*.token
triggers.json
16 changes: 12 additions & 4 deletions argTypes.go → argparser/constants.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Bot.go Project
// Copyright (C) 2021 Sayan Biswas, ALiwoto
// argparser Project
// Copyright (C) 2021 wotoTeam, ALiwoto
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.

package argparser

type FlagType uint8

const (
NoneFlagType FlagType = iota
BoolFlagType
Expand Down Expand Up @@ -34,3 +32,13 @@ const (
Int32TypeStr = "int32"
Int64TypeStr = "int64"
)

// common and global HLCs.
const (
TrueHlc = "true"
YesHlc = "yes"
OnHlc = "on"
FalseHlc = "false"
NoHlc = "no"
OffHlc = "off"
)
62 changes: 46 additions & 16 deletions arg.go → argparser/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@ import (
)

// Flag is the options passed along with the commands
// by users. they should send them with prefex "--",
// 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
name string
index int
value interface{}
fType FlagType
emptyT bool
}

type EventArgs struct {
command string // command without '/' or '!'
flags []Flag
rawData string
prefixes []rune
command string // command without '/' or '!'
flags []Flag
rawData string
}

// ParseArg will parse the whole text into an EventArg and will return it.
func ParseArg(text string) (e *EventArgs, err error) {
if ws.IsEmpty(&text) {
func ParseArg(text string, prefixes []rune) (e *EventArgs, err error) {
if text == "" {
return nil, errors.New("text cannot be empty")
}

if len(prefixes) == 0 {
prefixes = DefaultPrefixes
}

ss := ws.Ss(text)
if !ss.HasPrefix(ws.COMMAND_PREFIX1, ws.COMMAND_PREFIX2,
ws.SUDO_PREFIX1) {
if !ss.HasRunePrefix(prefixes...) {
return nil, errors.New("this message is not a command at all")
}

Expand All @@ -50,16 +55,16 @@ func ParseArg(text string) (e *EventArgs, err error) {
return nil, errors.New("length of the command cannot be zero")
}

cmdSs := cmd.TrimStr(ws.COMMAND_PREFIX1, ws.COMMAND_PREFIX2,
ws.SUDO_PREFIX1, ws.SPACE_VALUE)
cmdSs := cmd.TrimStr(toStrArray(prefixes)...)
if cmdSs.IsEmpty() {
return nil, errors.New("command cannot be only whitespace")
}

cmdStr := cmdSs.GetValue()

e = &EventArgs{
command: cmdStr,
command: cmdStr,
prefixes: prefixes,
}

// lock the special characters such as "--", ":", "=".
Expand Down Expand Up @@ -130,14 +135,26 @@ func ParseArg(text string) (e *EventArgs, err error) {
tmpFlag.setRealValue(fixTmpStr(tmp))

myFlags = append(myFlags, tmpFlag)

}

e.setFlags(myFlags)

return e, nil
}

func ParseArgDefault(text string) (e *EventArgs, err error) {
return ParseArg(text, DefaultPrefixes)
}

func toStrArray(r []rune) []string {
var s []string
for _, v := range r {
s = append(s, string(v))
}
s = append(s, ws.SPACE_VALUE)
return s
}

func fixTmpStr(tmp string) string {
tmp = strings.TrimSpace(tmp)
if strings.HasPrefix(tmp, ws.EqualStr) {
Expand All @@ -162,3 +179,16 @@ func lookRaw(text *string, e *EventArgs) {

e.rawData = tmp
}

func ToBoolType(value string) (v, isBool bool) {
value = strings.TrimSpace(value)
value = strings.ToLower(value)
switch value {
case TrueHlc, YesHlc, OnHlc:
return true, true
case FalseHlc, NoHlc, OffHlc:
return false, true
default:
return false, false
}
}
100 changes: 86 additions & 14 deletions getMethods.go → argparser/methods.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Bot.go Project
// argparser Project
// Copyright (C) 2021 Sayan Biswas, ALiwoto
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.
Expand All @@ -14,6 +14,72 @@ import (

//---------------------------------------------------------

func (f *Flag) setName(name string) {
name = strings.TrimSpace(name)
f.name = name
}

func (f *Flag) setNameQ(q ws.QString) {
f.setName(q.GetValue())
}

func (f *Flag) setNilValue() {
f.value = nil
f.fType = NoneFlagType
}

func (f *Flag) setRealValue(value string) {
tmp := strings.TrimSpace(value)
if len(tmp) == ws.BaseIndex {
f.setAsBool(true)
f.setAsEmpty()
return
}

myI, err := strconv.ParseInt(value, ws.BaseTen, ws.Base64Bit)
if err == nil {
f.setAsInt(&myI)
return
}

if tmp[ws.BaseIndex] == ws.CHAR_STR {
myInt := len(tmp) - ws.BaseOneIndex
if tmp[myInt] == ws.CHAR_STR {
tmp = strings.TrimPrefix(tmp, ws.STR_SIGN)
tmp = strings.TrimSuffix(tmp, ws.STR_SIGN)
f.setAsString(&tmp)
return
}
}

v, isBool := ToBoolType(value)
if isBool {
f.setAsBool(v)
return
}

f.setAsString(&value)
}

func (f *Flag) setAsBool(value bool) {
f.value = value
f.fType = BoolFlagType
}

func (f *Flag) setAsEmpty() {
f.emptyT = true
}

func (f *Flag) setAsString(value *string) {
f.value = *value
f.fType = StringFlagType
}

func (f *Flag) setAsInt(value *int64) {
f.value = *value
f.fType = Int64FlagType
}

// GetValue will return you the value of this flag.
// remember that value can be
func (f *Flag) GetValue() interface{} {
Expand Down Expand Up @@ -90,6 +156,10 @@ func (f *Flag) GetAsString() string {
return NoneTypeStr
}

func (f *Flag) isEmpty() bool {
return f.emptyT || f.fType == NoneFlagType
}

func (f *Flag) GetAsInteger() (vI int64, ok bool) {
v, t := f.GetValueAndType()
if t == StringFlagType {
Expand Down Expand Up @@ -318,6 +388,17 @@ func (e *EventArgs) GetAsIntegerOrRaw(name ...string) (vI int64, ok bool) {
}
}

func (e *EventArgs) GetFirstNoneEmptyValue() string {
for _, current := range e.flags {
if !current.isEmpty() {
return current.GetAsString()
}
}

// lets return raw data here; as it is the last resort.
return e.rawData
}

// GetAsString will give you the boolean value of the flag
// with the specified name.
// if there is no flag with this name, or there is an
Expand All @@ -331,21 +412,12 @@ func (e *EventArgs) GetAsBool(name ...string) bool {
return f.GetAsBool()
}

//---------------------------------------------------------

func ToBoolType(value string) (v, isBool bool) {
value = strings.TrimSpace(value)
value = strings.ToLower(value)
switch value {
case TrueHlc, YesHlc, OnHlc:
return true, true
case FalseHlc, NoHlc, OffHlc:
return false, true
default:
return false, false
}
func (e *EventArgs) setFlags(f []Flag) {
e.flags = f
}

//---------------------------------------------------------

func (t *FlagType) ToString() string {
switch *t {
case NoneFlagType:
Expand Down
6 changes: 4 additions & 2 deletions constants.go → argparser/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// argparser Project
// Copyright (C) 2021 wotoTeam, ALiwoto
// Bot.go Project
// Copyright (C) 2021 Sayan Biswas, ALiwoto
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of the source code.

package argparser

type FlagType uint8
8 changes: 8 additions & 0 deletions argparser/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package argparser

var (
DefaultPrefixes = []rune{
'/',
'!',
}
)
2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
examples
examples.exe
Binary file removed examples/bools/examples
Binary file not shown.
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/bools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package main
import (
"log"

"github.com/ALiwoto/argparser"
"github.com/ALiwoto/argparser/argparser"
)

const boolTest = "/test --flag1 true --flag2 on" +
Expand All @@ -17,7 +17,7 @@ const boolTest = "/test --flag1 true --flag2 on" +
" --flag5 off"

func main() {
args, err := argparser.ParseArg(boolTest)
args, err := argparser.ParseArgDefault(boolTest)
if err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ module github.com/ALiwoto/argparser

go 1.16

require github.com/ALiwoto/StrongStringGo v1.0.4

require github.com/ALiwoto/StrongStringGo v1.0.6
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/ALiwoto/StrongStringGo v1.0.4 h1:QqlO3bkbLxPq/B4iKhBRF48FYIoApNHQnFQBkCPoT00=
github.com/ALiwoto/StrongStringGo v1.0.4/go.mod h1:t7WxtiphnwdgJaq443DSCAw7q+Ut3V8Pg1kT6IcdKmE=
github.com/ALiwoto/StrongStringGo v1.0.6 h1:O2pZ58qu2gGXA57JjDqtleWSCHP9kkm4vZO/hdLJunI=
github.com/ALiwoto/StrongStringGo v1.0.6/go.mod h1:t7WxtiphnwdgJaq443DSCAw7q+Ut3V8Pg1kT6IcdKmE=
Loading

0 comments on commit a7dba40

Please sign in to comment.