Skip to content

Commit 28e2692

Browse files
committed
- add feature EmptyMain
1 parent e1a087b commit 28e2692

File tree

4 files changed

+58
-33
lines changed

4 files changed

+58
-33
lines changed

application.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Application interface {
4848
Detail() (string, string)
4949
Help(...string)
5050
Main(Arguments) (*TabbyContainer, error)
51+
EmptyMain() (*TabbyContainer, error)
5152
SetParam(string, string, DefaultValue, ...string)
5253
Params() []Parameter
5354
SubApplication(string) (Application, bool)
@@ -57,7 +58,6 @@ type Application interface {
5758
type BaseApplication struct {
5859
apps map[string]Application
5960
params []Parameter
60-
width, height uint
6161
ignoreUnsupportedArgs bool
6262
}
6363

@@ -97,6 +97,11 @@ func (ba *BaseApplication) Help(parts ...string) {
9797

9898
}
9999

100+
func (ba *BaseApplication) EmptyMain() (*TabbyContainer, error) {
101+
ba.Help("Usage:")
102+
return nil, nil
103+
}
104+
100105
// SetParam default设置为nil则必须提供
101106
func (ba *BaseApplication) SetParam(identify, help string, defaultValue DefaultValue, alias ...string) {
102107
ba.params = append(ba.params, Parameter{
@@ -127,11 +132,9 @@ func (ba *BaseApplication) IgnoreUnsupportedArgs() bool {
127132
return ba.ignoreUnsupportedArgs
128133
}
129134

130-
func NewBaseApplication(ignoreUnsupportedArgs bool, width, height uint, apps []Application) *BaseApplication {
135+
func NewBaseApplication(ignoreUnsupportedArgs bool, apps []Application) *BaseApplication {
131136
ba := &BaseApplication{
132137
apps: make(map[string]Application),
133-
width: width,
134-
height: height,
135138
ignoreUnsupportedArgs: ignoreUnsupportedArgs,
136139
}
137140
for _, app := range apps {

tabby.go

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,9 @@ import (
44
"errors"
55
"fmt"
66
"os"
7-
"strconv"
87
"strings"
98
)
109

11-
var (
12-
Int = NewTransfer("int", func(s string) (any, error) {
13-
if i, err := strconv.ParseInt(s, 10, 0); err != nil {
14-
return 0, err
15-
} else {
16-
return int(i), nil
17-
}
18-
})
19-
20-
String = NewTransfer("string", func(s string) (any, error) {
21-
return s, nil
22-
})
23-
24-
Bool = NewTransfer("int", func(s string) (any, error) {
25-
if len(s) == 0 {
26-
return true, nil
27-
} else {
28-
return false, errors.New("boolean requires no value")
29-
}
30-
})
31-
)
32-
3310
type DefaultValue struct {
3411
name string
3512
value any
@@ -119,7 +96,7 @@ func (t *Tabby) Run(rawArgs []string) (*TabbyContainer, error) {
11996
subApp, ok = app.SubApplication(appName)
12097
if !ok {
12198
if t.unknownApp == nil {
122-
return nil, fmt.Errorf("App '%s' not exists", strings.Join(appPath, "/"))
99+
return nil, fmt.Errorf("application '%s' not exists", strings.Join(appPath, "/"))
123100
} else {
124101
app = t.unknownApp
125102
if err := app.Init(t.mainApp); err != nil {
@@ -151,7 +128,7 @@ func (t *Tabby) Run(rawArgs []string) (*TabbyContainer, error) {
151128
for _, alia := range param.alias {
152129
if v, ok := strArgs[alia]; ok {
153130
if value, err1 := param.defaultValue.transfer(v); err1 != nil {
154-
return nil, fmt.Errorf("App '%s': argument '%s(%s)' :error: %s", finalAppPath, param.identify, param.defaultValue.name, err1.Error())
131+
return nil, fmt.Errorf("application '%s': argument '%s(%s)' :error: %s", finalAppPath, param.identify, param.defaultValue.name, err1.Error())
155132
} else {
156133
args[param.identify] = value
157134
empty = false
@@ -161,11 +138,19 @@ func (t *Tabby) Run(rawArgs []string) (*TabbyContainer, error) {
161138
}
162139
}
163140

141+
if empty {
142+
tc, err := app.EmptyMain()
143+
if err != nil {
144+
return nil, errors.New("App '" + finalAppPath + "' error:" + err.Error())
145+
}
146+
return tc, nil
147+
}
148+
164149
if len(strArgs) > 0 && !app.IgnoreUnsupportedArgs() {
165150
return nil, fmt.Errorf(
166-
"App '%s': unsupported parameters '%s'",
151+
"application '%s': unsupported parameters '%s'",
167152
finalAppPath,
168-
strings.Join(AddPrefix(MapKeys[string, string](strArgs), "-"), ","))
153+
strings.Join(AddPrefix(MapKeys(strArgs), "-"), ","))
169154
}
170155

171156
//DefaultArgs
@@ -175,7 +160,7 @@ func (t *Tabby) Run(rawArgs []string) (*TabbyContainer, error) {
175160
args[param.identify] = param.defaultValue.value
176161
} else if !empty {
177162
return nil, fmt.Errorf(
178-
"App '%s': required parameter '%s' not provided(%s)",
163+
"application '%s': required parameter '%s' not provided(%s)",
179164
finalAppPath, param.identify,
180165
strings.Join(AddPrefix(param.alias, "-"), ","))
181166
}

tabby_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (t TestApplication) Main(args Arguments) (*TabbyContainer, error) {
2020

2121
func NewTestApplication() *TestApplication {
2222
return &TestApplication{
23-
NewBaseApplication(true, 0, 0, nil),
23+
NewBaseApplication(true, nil),
2424
}
2525
}
2626
func TestTabby(t *testing.T) {

types.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tabby
2+
3+
import (
4+
"errors"
5+
"strconv"
6+
"time"
7+
)
8+
9+
var (
10+
Int = NewTransfer("int", func(s string) (any, error) {
11+
if i, err := strconv.ParseInt(s, 10, 0); err != nil {
12+
return 0, err
13+
} else {
14+
return int(i), nil
15+
}
16+
})
17+
18+
String = NewTransfer("string", func(s string) (any, error) {
19+
return s, nil
20+
})
21+
22+
Bool = NewTransfer("bool", func(s string) (any, error) {
23+
if len(s) == 0 {
24+
return true, nil
25+
} else {
26+
return false, errors.New("boolean requires no value")
27+
}
28+
})
29+
30+
Duration = NewTransfer("duration", func(s string) (any, error) {
31+
if i, err := time.ParseDuration(s); err != nil {
32+
return time.Duration(0), err
33+
} else {
34+
return time.Duration(i), nil
35+
}
36+
})
37+
)

0 commit comments

Comments
 (0)