Skip to content

Commit 2ccc8e1

Browse files
authored
🎉 Merge critical pull request to implement #1!
2 parents ca777f7 + 12ba1e4 commit 2ccc8e1

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

‎main.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@ package raspberry
33
import "os"
44
import "fmt"
55

6-
type Cli struct{
6+
type Cli struct {
77
AcceptedCommands []string
8-
HelpMsg string
9-
Version float64
8+
HelpMsg string
9+
Version float64
1010
}
11-
var(
12-
Command string
11+
12+
var (
13+
Command string
1314
argLength int
15+
Args []string
1416
)
17+
1518
type fn func()
16-
func (c *Cli) SetHandler(cmd string,fun fn) {
19+
20+
func (c *Cli) SetHandler(cmd string, fun fn) {
1721
if Command == cmd {
1822
fun()
19-
}
23+
}
2024
}
21-
func contains(str string,s []string) bool {
25+
func contains(str string, s []string) bool {
2226
for _, v := range s {
2327
if v == str {
2428
return true
@@ -27,27 +31,33 @@ func contains(str string,s []string) bool {
2731

2832
return false
2933
}
30-
func (c *Cli) PrintHelp(){
34+
func (c *Cli) PrintHelp() {
3135
fmt.Println(c.HelpMsg)
3236
}
33-
func (c *Cli) PrintVersion(){
37+
func (c *Cli) PrintVersion() {
3438
fmt.Println(c.Version)
3539
}
36-
func (c *Cli) Setup(){
37-
argLength = len(os.Args)
38-
if argLength > 1 {
39-
if(contains(os.Args[1],c.AcceptedCommands)){
40-
Command = os.Args[1]
41-
}else{
42-
fmt.Println("Command not found: ",os.Args[1])
43-
}
44-
}else{
45-
fmt.Println("Not enough arguments")
46-
os.Exit(1)
47-
}
48-
// set default cmd's
49-
c.SetHandler("-v",c.PrintVersion)
50-
c.SetHandler("version",c.PrintVersion)
51-
c.SetHandler("-h",c.PrintHelp)
52-
c.SetHandler("help",c.PrintHelp)
40+
func shiftArgsDownward(arr []string) []string {
41+
arr = arr[2:]
42+
return arr
43+
}
44+
func (c *Cli) Setup() {
45+
argLength = len(os.Args)
46+
if argLength > 1 {
47+
if contains(os.Args[1], c.AcceptedCommands) {
48+
Command = os.Args[1]
49+
Args = os.Args
50+
Args = shiftArgsDownward(Args)
51+
} else {
52+
fmt.Println("Command not found: ", os.Args[1])
53+
}
54+
} else {
55+
fmt.Println("Not enough arguments")
56+
os.Exit(1)
57+
}
58+
// set default cmd's
59+
c.SetHandler("-v", c.PrintVersion)
60+
c.SetHandler("version", c.PrintVersion)
61+
c.SetHandler("-h", c.PrintHelp)
62+
c.SetHandler("help", c.PrintHelp)
5363
}

0 commit comments

Comments
 (0)