From 4e9aba9b585a73ceecb17db692d9c16a7ee33365 Mon Sep 17 00:00:00 2001 From: atha G <74566464+Pandademic@users.noreply.github.com> Date: Thu, 26 May 2022 12:43:57 -0400 Subject: [PATCH 1/3] This need testing , but start implementation of #1 --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index 6f20ed2..30b20d9 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ type Cli struct{ } var( Command string + FirstArg string + SecondArg string argLength int ) type fn func() @@ -38,6 +40,8 @@ func (c *Cli) Setup(){ if argLength > 1 { if(contains(os.Args[1],c.AcceptedCommands)){ Command = os.Args[1] + FirstArg = os.Args[2] + SecondArg = os.Args[3] }else{ fmt.Println("Command not found: ",os.Args[1]) } From 794060c2442fd54388a67db703c36d65bae5a774 Mon Sep 17 00:00:00 2001 From: Pandademic Date: Fri, 27 May 2022 11:08:52 -0400 Subject: [PATCH 2/3] diffrent , more sensible implementation of #1. How? by having args as a slice that the CLI can acess , so an unlimetd number of arguments can be passed.[WIP] --- main.go | 68 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index 30b20d9..62c2735 100644 --- a/main.go +++ b/main.go @@ -3,24 +3,26 @@ package raspberry import "os" import "fmt" -type Cli struct{ +type Cli struct { AcceptedCommands []string - HelpMsg string - Version float64 + HelpMsg string + Version float64 } -var( - Command string - FirstArg string - SecondArg string + +var ( + Command string argLength int + Args []string ) + type fn func() -func (c *Cli) SetHandler(cmd string,fun fn) { + +func (c *Cli) SetHandler(cmd string, fun fn) { if Command == cmd { fun() - } + } } -func contains(str string,s []string) bool { +func contains(str string, s []string) bool { for _, v := range s { if v == str { return true @@ -29,29 +31,33 @@ func contains(str string,s []string) bool { return false } -func (c *Cli) PrintHelp(){ +func (c *Cli) PrintHelp() { fmt.Println(c.HelpMsg) } -func (c *Cli) PrintVersion(){ +func (c *Cli) PrintVersion() { fmt.Println(c.Version) } -func (c *Cli) Setup(){ - argLength = len(os.Args) - if argLength > 1 { - if(contains(os.Args[1],c.AcceptedCommands)){ - Command = os.Args[1] - FirstArg = os.Args[2] - SecondArg = os.Args[3] - }else{ - fmt.Println("Command not found: ",os.Args[1]) - } - }else{ - fmt.Println("Not enough arguments") - os.Exit(1) - } - // set default cmd's - c.SetHandler("-v",c.PrintVersion) - c.SetHandler("version",c.PrintVersion) - c.SetHandler("-h",c.PrintHelp) - c.SetHandler("help",c.PrintHelp) +func shiftArgsDownward(arr []string) []string { + arr = arr[2:] + return arr +} +func (c *Cli) Setup() { + argLength = len(os.Args) + if argLength > 1 { + if contains(os.Args[1], c.AcceptedCommands) { + Command = os.Args[1] + Args = os.Args + shiftArgsDownward(Args) + } else { + fmt.Println("Command not found: ", os.Args[1]) + } + } else { + fmt.Println("Not enough arguments") + os.Exit(1) + } + // set default cmd's + c.SetHandler("-v", c.PrintVersion) + c.SetHandler("version", c.PrintVersion) + c.SetHandler("-h", c.PrintHelp) + c.SetHandler("help", c.PrintHelp) } From 12ba1e4e9d34386cf80274d26f355f46e4b27979 Mon Sep 17 00:00:00 2001 From: Pandademic Date: Fri, 27 May 2022 11:17:08 -0400 Subject: [PATCH 3/3] Fix a minor bug.This branch should now be stable By stable I mean ready to merge. What was the minor bug , you may ask? Function shiftArgsDownward returns the slice , and does not modify it. Previously Args was not set to shiftArgsDownward(Args) , so it stayed unmodified Yes , future me I ran go fmt --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 62c2735..21dfd7f 100644 --- a/main.go +++ b/main.go @@ -47,7 +47,7 @@ func (c *Cli) Setup() { if contains(os.Args[1], c.AcceptedCommands) { Command = os.Args[1] Args = os.Args - shiftArgsDownward(Args) + Args = shiftArgsDownward(Args) } else { fmt.Println("Command not found: ", os.Args[1]) }