Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] May I ask how the command line tool nests grouping? #2838

Closed
shuqingzai opened this issue Aug 4, 2023 · 10 comments
Closed

[Question] May I ask how the command line tool nests grouping? #2838

shuqingzai opened this issue Aug 4, 2023 · 10 comments

Comments

@shuqingzai
Copy link

我看到 gf 的是可以按以下方式分组

7121691139084_ pic

但是我这边尝试的时候,添加一个子命令可以分组的,多个就不行,多个的时候,子命令直接丢失

7141691139128_ pic

7151691139187_ pic

7171691139264_ pic

加两个直接没了子命令

7161691139264_ pic

1. What version of Go and system type/arch are you using?

#-> % go version
go version go1.20.6 darwin/amd64

2. What version of GoFrame are you using?

#-> % gf --version
GoFrame CLI Tool v2.5.1, https://goframe.org
GoFrame Version: v2.5.1 in current go.mod

3. Can this issue be re-produced with the latest release?

4. What did you do?

5. What did you expect to see?

6. What did you see instead?

@Issues-translate-bot Issues-translate-bot changed the title 请问命令行工具如何嵌套分组? May I ask how the command line tool nests grouping? Aug 4, 2023
@shuqingzai shuqingzai changed the title May I ask how the command line tool nests grouping? [Question] May I ask how the command line tool nests grouping? Aug 4, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


https://goframe.org/pages/viewpage.action?pageId=35357523, you can read this document description

@wln32
Copy link
Member

wln32 commented Aug 4, 2023

https://goframe.org/pages/viewpage.action?pageId=35357523
可以看下文档说明

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


https://goframe.org/pages/viewpage.action?pageId=35357523
You can see the documentation

@shuqingzai
Copy link
Author

https://goframe.org/pages/viewpage.action?pageId=35357523 可以看下文档说明

就是看了,没搞明白才提问呀

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


https://goframe.org/pages/viewpage.action?pageId=35357523 You can see the document description

I just saw it, I didn't understand it, so I asked.

@shuqingzai
Copy link
Author

https://goframe.org/pages/viewpage.action?pageId=35357523 可以看下文档说明

我想的实现的是和 gf gen xxx 类似的风格,但是文档并没有给出这样的示例,看了 gf gen 源码,我自己模拟,似乎只能加一个,多个不行,参考:#2838 (comment) @gqcn

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


https://goframe.org/pages/viewpage.action?pageId=35357523 You can see the document description

What I want to achieve is a style similar to gf gen xxx, but the document does not give such an example. After reading the source code of gf gen, I simulated it myself. It seems that I can only add one, not multiple. Reference: https:// github.com//issues/2838#issue-1836357144 @gqcn

@hailaz
Copy link
Member

hailaz commented Aug 16, 2023

@shuqingzai
我知道原因,对象的方法名不能重复。
image

嵌套形式本质上是同层的方法。Http Grpc AAA BBB Hello是同层的东西,只是人为用对象区分了一下,所以他们并不能重复。

package main

import (
	"context"
	"fmt"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gctx"
)

type cMain struct {
	g.Meta `name:"main"`
	CTest
	cHello
}

type cMainHttpInput struct {
	g.Meta `name:"http" brief:"start http server"`
}
type cMainHttpOutput struct{}

type cMainGrpcInput struct {
	g.Meta `name:"grpc" brief:"start grpc server"`
}
type cMainGrpcOutput struct{}

func (c *cMain) Http(ctx context.Context, in cMainHttpInput) (out *cMainHttpOutput, err error) {
	fmt.Println("start http server")
	return
}

func (c *cMain) Grpc(ctx context.Context, in cMainGrpcInput) (out *cMainGrpcOutput, err error) {
	fmt.Println("start grpc server")
	return
}

type CTest struct {
}

type cTestInput struct {
	g.Meta `name:"test" brief:"我是test命令"` // 主要看这个name
}
type cTestOutput struct{}

func (c CTest) AAA(ctx context.Context, in cTestInput) (out *cTestOutput, err error) { // 这里的方法名字千万不能和其它重复
	fmt.Println("Test")
	return
}

type cTomInput struct {
	g.Meta `name:"tom" brief:"我是tom命令"` // 主要看这个name
}
type cTomOutput struct{}

func (c CTest) BBB(ctx context.Context, in cTomInput) (out *cTomOutput, err error) {
	fmt.Println("Test")
	return
}

type cHello struct {
}

type cHelloInput struct {
	g.Meta `name:"hello" brief:"我是hello命令"` // 主要看这个name
}
type cHelloOutput struct{}

func (c *cHello) Hello(ctx context.Context, in cHelloInput) (out *cHelloOutput, err error) {
	fmt.Println("Hello")
	return
}

func main() {
	cmd, err := gcmd.NewFromObject(cMain{}) // 这是单层
	if err != nil {
		panic(err)
	}
	// cmd.AddObject(cMain{}) // 这是多层,懒得写新的了,直接叠加main,就变成main main test
	cmd.Run(gctx.New())
}

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@shuqingzai
I know the reason, the method name of the object cannot be repeated.
image

Nested forms are essentially methods at the same level. Http Grpc AAA BBB Hello is the same layer, but it is artificially distinguished by objects, so they cannot be repeated.

package main

import (
"context"
"fmt"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/os/gctx"
)

type cMain struct {
g.Meta `name: "main"`
CTest
Hello
}

type cMainHttpInput struct {
g.Meta `name:"http" brief:"start http server"`
}
type cMainHttpOutput struct{}

type cMainGrpcInput struct {
g.Meta `name:"grpc" brief:"start grpc server"`
}
type cMainGrpcOutput struct{}

func (c *cMain) Http(ctx context.Context, in cMainHttpInput) (out *cMainHttpOutput, err error) {
fmt.Println("start http server")
return
}

func (c *cMain) Grpc(ctx context.Context, in cMainGrpcInput) (out *cMainGrpcOutput, err error) {
fmt.Println("start grpc server")
return
}

type CTest struct {
}

type cTestInput struct {
g.Meta `name:"test" brief: "I am the test command"` // Mainly look at this name
}
type cTestOutput struct{}

func (c CTest) AAA(ctx context.Context, in cTestInput) (out *cTestOutput, err error) { // The method name here must not be the same as other
fmt.Println("Test")
return
}

type cTomInput struct {
g.Meta `name:"tom" brief: "I am the tom command"` // Mainly look at this name
}
type cTomOutput struct{}

func (c CTest) BBB(ctx context.Context, in cTomInput) (out *cTomOutput, err error) {
fmt.Println("Test")
return
}

type cHello struct {
}

type cHelloInput struct {
g.Meta `name:"hello" brief: "I am the hello command"` // Mainly look at this name
}
type cHelloOutput struct{}

func (c *cHello) Hello(ctx context.Context, in cHelloInput) (out *cHelloOutput, err error) {
fmt.Println("Hello")
return
}

func main() {
cmd, err := gcmd.NewFromObject(cMain{}) // this is a single layer
if err != nil {
panic(err)
}
// cmd.AddObject(cMain{}) // This is multi-layer, too lazy to write a new one, directly superimpose main, it becomes main main test
cmd.Run(gctx.New())
}

@shuqingzai
Copy link
Author

shuqingzai commented Aug 16, 2023

@shuqingzai 我知道原因,对象的方法名不能重复。 image

嵌套形式本质上是同层的方法。Http Grpc AAA BBB Hello是同层的东西,只是人为用对象区分了一下,所以他们并不能重复。

package main

import (
	"context"
	"fmt"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gctx"
)

type cMain struct {
	g.Meta `name:"main"`
	CTest
	cHello
}

type cMainHttpInput struct {
	g.Meta `name:"http" brief:"start http server"`
}
type cMainHttpOutput struct{}

type cMainGrpcInput struct {
	g.Meta `name:"grpc" brief:"start grpc server"`
}
type cMainGrpcOutput struct{}

func (c *cMain) Http(ctx context.Context, in cMainHttpInput) (out *cMainHttpOutput, err error) {
	fmt.Println("start http server")
	return
}

func (c *cMain) Grpc(ctx context.Context, in cMainGrpcInput) (out *cMainGrpcOutput, err error) {
	fmt.Println("start grpc server")
	return
}

type CTest struct {
}

type cTestInput struct {
	g.Meta `name:"test" brief:"我是test命令"` // 主要看这个name
}
type cTestOutput struct{}

func (c CTest) AAA(ctx context.Context, in cTestInput) (out *cTestOutput, err error) { // 这里的方法名字千万不能和其它重复
	fmt.Println("Test")
	return
}

type cTomInput struct {
	g.Meta `name:"tom" brief:"我是tom命令"` // 主要看这个name
}
type cTomOutput struct{}

func (c CTest) BBB(ctx context.Context, in cTomInput) (out *cTomOutput, err error) {
	fmt.Println("Test")
	return
}

type cHello struct {
}

type cHelloInput struct {
	g.Meta `name:"hello" brief:"我是hello命令"` // 主要看这个name
}
type cHelloOutput struct{}

func (c *cHello) Hello(ctx context.Context, in cHelloInput) (out *cHelloOutput, err error) {
	fmt.Println("Hello")
	return
}

func main() {
	cmd, err := gcmd.NewFromObject(cMain{}) // 这是单层
	if err != nil {
		panic(err)
	}
	// cmd.AddObject(cMain{}) // 这是多层,懒得写新的了,直接叠加main,就变成main main test
	cmd.Run(gctx.New())
}

@hailaz 谢谢你的回答,我已经把命令行改用 cobra 了,使用 cobra 进行项目开发对于我们比较熟悉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants