Skip to content

Commit

Permalink
🎉 Merge critical pull request to implement #1!
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandademic committed May 27, 2022
2 parents ca777f7 + 12ba1e4 commit 2ccc8e1
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +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

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
Expand All @@ -27,27 +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]
}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
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)
}

0 comments on commit 2ccc8e1

Please sign in to comment.