Skip to content

Commit

Permalink
updated: Argument parsed added and flags added.
Browse files Browse the repository at this point in the history
  • Loading branch information
yigit433 committed Aug 2, 2022
1 parent 408e78d commit 07e2b7b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func main() {

app.AddCommand(
&types.Command{
Name: "test",
Name: "test",
Description: "Hello world test example!",
Execute: func(res *types.CmdResponse) {
Flags: []string{"isbool"}
Execute: func(res *types.CmdResponse) {
fmt.Println("Hello world!")
},
},
Expand Down
1 change: 1 addition & 0 deletions types/commandSheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
type Command struct {
Name string
Description string
Flags []string
Execute func(res *CmdResponse)
}

Expand Down
49 changes: 48 additions & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ func (c *Config) Run() {

for _, cmd := range c.commands {
if cmd.Name == args[0] {
parsedArgs := c.argParser(args[1:])

cmd.Execute(&CmdResponse{
Command: cmd,
Args: args,
Args: parsedArgs,
})
break
}
Expand All @@ -64,3 +66,48 @@ func (c *Config) createCommandList() {

fmt.Println(logmsg)
}

func (c *Config) argParser(args []string) map[string]interface{} {
output := make(map[string]interface{})

output["args"] = []string{}

for ind, arg := range args {
if strings.Contains(arg, "--") {
vals := strings.Split(arg, "--")

if strings.Contains(vals[1], "=") {
parsed := strings.Split(vals[1], "=")

output[parsed[0]] = parsed[1]
} else {
output[vals[1]] = args[ind+1]
}
} else if strings.Contains(arg, "-") {
vals := strings.Split(arg, "-")

if strings.Contains(vals[1], "=") {
parsed := strings.Split(vals[1], "=")

output[parsed[0]] = parsed[1]
} else {
output[vals[1]] = args[ind+1]
}
} else {
if (ind - 1) >= 0 {
cont1 := strings.Contains(args[ind-1], "--")
cont2 := strings.Contains(args[ind-1], "-")

if !cont1 || !cont2 || ((cont1 || cont2) && strings.Contains(args[ind-1], "=")) {
args := output["args"].([]string)

args = append(args, arg)

output["args"] = args
}
}
}
}

return output
}

0 comments on commit 07e2b7b

Please sign in to comment.