Skip to content

Commit 880dd45

Browse files
committed
- Adding helper method to parse the incoming flags array into map
1 parent ed980d7 commit 880dd45

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

‎cmd/packman/controllers/unpack.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var UnpackCommand = cli.Command{
3535
func flagsArray(ctx *cli.Context) []string {
3636
out := make([]string, ctx.NumFlags())
3737
for _, flagName := range ctx.FlagNames() {
38-
out = append(out, ctx.String(flagName))
38+
out = append(out, flagName, ctx.String(flagName))
3939
}
4040

4141
return out

‎internal/business/git_project_init.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ type MyData struct {
6767
}
6868
6969
func main() {
70-
// Args sent by packman's driver will be forwarded to here:
71-
args := os.Args[2:]
70+
// flags sent by packman's driver will be forwarded to here:
71+
flags := ParseFlags(os.Args[2:])
7272
7373
// Build your own model to represent the templating you need
7474
model := MyData{PackageName: "my_pkg", Args: args}

‎pkg/flags.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pkg
2+
3+
import "fmt"
4+
5+
func ParseFlags(cmdArgs []string) map[string]string {
6+
7+
if len(cmdArgs)%2 != 0 {
8+
panic(fmt.Sprintf("incoming flags must be an even array but got %v", cmdArgs))
9+
}
10+
11+
out := make(map[string]string)
12+
for idx, item := range cmdArgs {
13+
if idx%2 == 0 {
14+
out[item] = cmdArgs[idx+1]
15+
}
16+
}
17+
18+
return out
19+
}

0 commit comments

Comments
 (0)
⌂ 换网址