Skip to content

Commit

Permalink
updated: Flag parser required activated.
Browse files Browse the repository at this point in the history
  • Loading branch information
yigit433 committed Aug 3, 2022
1 parent 265db87 commit 2c20cea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
60 changes: 39 additions & 21 deletions types/commandSheets.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package types

import (
"fmt"
"reflect"
"strconv"
"strings"
)

Expand Down Expand Up @@ -43,31 +43,39 @@ func (c *Command) isValidAliase(aliase string) *bool {
func (c *Command) isValidFlag(fname string, fvalue interface{}) *bool {
var output bool = false

for i, flag := range c.Flags {
if flag.Name == fname && reflect.TypeOf(fvalue).Name() == flag.ValueType {
output = true
break
} else if !output && i == len(c.Flags)-1 {
c.createFlagList()
}
}

return &output
}
for _, flag := range c.Flags {
if flag.Name == fname {
if flag.ValueType == "bool" {
_, err := strconv.ParseBool(fvalue.(string))
if err != nil {
panic(err)
break
}

func (c *Command) createFlagList() {
list := []string{}
output = true
} else if flag.ValueType == "int" {
_, err := strconv.ParseInt(fvalue.(string), 10, 64)
if err != nil {
panic(err)
break
}

for _, flag := range c.Flags {
usage := fmt.Sprintf("--%s", flag.Name)
msg := strings.Replace(CMD_FLAG, "{FlagUsage}", usage, -1)
msg = strings.Replace(msg, "{FlagType}", flag.ValueType, -1)
msg = strings.Replace(msg, "{FlagDescription}", flag.Description, -1)
output = true
} else if flag.ValueType == "float" {
_, err := strconv.ParseFloat(fvalue.(string), 64)
if err != nil {
panic(err)
break
}

list = append(list, msg)
output = true
} else if reflect.TypeOf(fvalue).Name() == "string" {
output = true
}
}
}

fmt.Println(strings.Join(list, "\n"))
return &output
}

func (c *Command) argParser(args []string) map[string]interface{} {
Expand Down Expand Up @@ -122,5 +130,15 @@ func (c *Command) argParser(args []string) map[string]interface{} {
}
}

if len(output) >= 1 {
for _, flags := range c.Flags {
_, ok := output[flags.Name]

if *flags.Required && !ok {
panic("Required flag not specified!")
}
}
}

return output
}
1 change: 0 additions & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const (
MAIN_TEMPLATE string = "Welcome to {AppName}! That's a command list. Type 'help <command name>' to get help with any command.\n{CmdList}"
CMD_LIST string = "{CmdName} |> {CmdDescription}"
CMD_HELP string = "{CmdName} | Info\nDescription |> {CmdDescription}\nFlags |> {CmdFlags}\nAliases |> {CmdAliases}"
CMD_FLAG string = "{FlagUsage} |{FlagType}> {FlagDescription}"
)

type Config struct {
Expand Down

0 comments on commit 2c20cea

Please sign in to comment.