Skip to content

Commit

Permalink
优化subcommand写法 (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Greyh4t authored Jun 8, 2022
1 parent ed3731d commit ec09cf3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
7 changes: 6 additions & 1 deletion clop.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,18 @@ func (c *Clop) getRoot() (root *Clop) {
func (c *Clop) parseSubcommandTag(clop string, v reflect.Value, usage string, fieldName string) (newClop *Clop, haveSubcommand bool) {
options := strings.Split(clop, ";")
for _, opt := range options {
var name string
switch {
case strings.HasPrefix(opt, "subcommand="):
name = opt[len("subcommand="):]
case opt == "subcommand":
name = strings.ToLower(fieldName)
}
if name != "" {
if c.subcommand == nil {
c.subcommand = make(map[string]*Subcommand, 3)
}

name := opt[len("subcommand="):]
newClop := New(nil)
//newClop.exit = c.exit //继承exit属性
newClop.SetProcName(name)
Expand Down
44 changes: 40 additions & 4 deletions clop_subcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ func (m *mv) SubMain() {
m.isSet = true
}

type touch struct {
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
isSet bool
}

func (t *touch) SubMain() {
t.isSet = true
}

type git struct {
Add add `clop:"subcommand=add" usage:"Add file contents to the index"`
Mv mv `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`
Add add `clop:"subcommand=add" usage:"Add file contents to the index"`
Mv mv `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`
Touch touch `clop:"subcommand" usage:"touch file to the index"`
}

// 方法1
Expand Down Expand Up @@ -59,6 +69,15 @@ func Test_API_subcommand_SubMain(t *testing.T) {
assert.NoError(t, err)
return g
}(), git{Mv: mv{Force: true, isSet: true}}},
{
// 测试touch子命令
func() git {
g := git{}
p := New([]string{"touch", "-f"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
return g
}(), git{Touch: touch{Force: true, isSet: true}}},
{
// 测试-h 输出的Usage
func() git {
Expand Down Expand Up @@ -92,9 +111,15 @@ func Test_API_subcommand(t *testing.T) {
isSet bool
}

type touch struct {
Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`
isSet bool
}

type git struct {
Add add `clop:"subcommand=add" usage:"Add file contents to the index"`
Mv mv `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`
Add add `clop:"subcommand=add" usage:"Add file contents to the index"`
Mv mv `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`
Touch touch `clop:"subcommand" usage:"touch file to the index"`
}
// 测试正确的情况
for _, test := range []testAPI{
Expand All @@ -120,6 +145,17 @@ func Test_API_subcommand(t *testing.T) {
assert.True(t, p.IsSetSubcommand("mv"))
return g
}(), git{Mv: mv{Force: true}}},
{
// 测试touch子命令
func() git {
g := git{}
p := New([]string{"touch", "-f"}).SetExit(false)
err := p.Bind(&g)
assert.NoError(t, err)
assert.False(t, p.IsSetSubcommand("add"))
assert.True(t, p.IsSetSubcommand("touch"))
return g
}(), git{Touch: touch{Force: true}}},
{
// 测试-h 输出的Usage
func() git {
Expand Down

0 comments on commit ec09cf3

Please sign in to comment.