Skip to content

Commit b698a52

Browse files
committed
handle some error
1 parent bc0e68c commit b698a52

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
/dataSources.local.xml
99
/.idea/
1010
.DS_Store
11-
go.work
11+
go.work*

application.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ type Arguments struct {
2020
func (a *Arguments) Get(key string) any {
2121
if v, ok := a.args[key]; ok {
2222
return v
23-
} else {
23+
} else if !a.empty {
2424
panic("argument '" + key + "' not registered")
25+
} else {
26+
panic("argument '" + key + "' is required,please handle empty arguments")
2527
}
2628
}
2729

tabby.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import (
1010

1111
var (
1212
Int = NewTransfer("int", func(s string) (any, error) {
13-
return strconv.ParseInt(s, 10, 64)
13+
if i, err := strconv.ParseInt(s, 10, 0); err != nil {
14+
return 0, err
15+
} else {
16+
return int(i), nil
17+
}
1418
})
1519

1620
String = NewTransfer("string", func(s string) (any, error) {
@@ -76,14 +80,17 @@ func (t *Tabby) Run(rawArgs []string) (*TabbyContainer, error) {
7680
if rawArgs == nil {
7781
rawArgs = os.Args[1:]
7882
}
83+
7984
//Apps
8085
var apps []string
8186
i := 0
8287
for _, argv := range rawArgs {
83-
if argv[0] == '-' {
88+
89+
if len(argv) > 0 && argv[0] == '-' {
8490
break
91+
} else {
92+
apps = append(apps, argv)
8593
}
86-
apps = append(apps, argv)
8794
i += 1
8895
}
8996
rawArgs = rawArgs[i:]
@@ -161,6 +168,8 @@ func (t *Tabby) Run(rawArgs []string) (*TabbyContainer, error) {
161168
"App '%s': required parameter '%s' not provided(%s)",
162169
finalAppPath, param.identify,
163170
strings.Join(AddPrefix(param.alias, "-"), ","))
171+
} else {
172+
164173
}
165174
}
166175
}

tabby_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (t TestApplication) Detail() (string, string) {
1414
}
1515

1616
func (t TestApplication) Main(args Arguments) (*TabbyContainer, error) {
17-
fmt.Println("ok")
17+
fmt.Println(args.Get("aa").(int))
1818
return nil, nil
1919
}
2020

@@ -24,6 +24,11 @@ func NewTestApplication() *TestApplication {
2424
}
2525
}
2626
func TestTabby(t *testing.T) {
27-
tb := NewTabby("test", NewTestApplication())
28-
tb.Run(nil)
27+
ta := NewTestApplication()
28+
ta.SetParam("aa", "", Int(10))
29+
tb := NewTabby("test", ta)
30+
_, err := tb.Run([]string{"-aa", "30"})
31+
if err != nil {
32+
fmt.Println(err)
33+
}
2934
}

0 commit comments

Comments
 (0)