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

os/gcmd: Parsing error when creating a subcommand from a structure and the subcommand has multiple parameters. #3670

Closed
wsy9981999 opened this issue Jun 30, 2024 · 2 comments · Fixed by #3807
Assignees
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@wsy9981999
Copy link

Go version

go version go1.22.4 windows/amd64

GoFrame version

2.7.2

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

// main.go
func main() {
	gcmd.Init("main.go gen controller app data")
	ctx := gctx.GetInitCtx()
	object, err := gcmd.NewFromObject(cmd.Wg)
	if err != nil {
		g.Log().Fatalf(ctx, "%+v", err)
	}
	if err := object.AddObject(cmd.Gen); err != nil {
		g.Log().Fatalf(ctx, "%+v", err)
	}
	if path, _ := gfile.Search(cliFolderName); path != "" {
		if adapter, ok := g.Cfg().GetAdapter().(*gcfg.AdapterFile); ok {
			if err := adapter.SetPath(path); err != nil {
				util.Fatal(err)
			}
		}
	}
	object.Run(ctx)

}
// internal/ cmd/wg_gen.go
package cmd

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
)

var Wg = &cWg{}

type (
	cWg struct {
		g.Meta `name:"wg"`
	}
	cWgInput struct {
		g.Meta `name:"wg"`
	}
	cWgOutput struct {
	}
)

func (receiver *cWg) Index(ctx context.Context, in cWgInput) (out *cWgOutput, err error) {
	gcmd.CommandFromCtx(ctx).Print()
	return nil, err
}
// internal/ cmd/wg_gen.go
package cmd

import (
	"github.com/gogf/gf/v2/frame/g"
)

var Gen = cGen{}

type (
	cGen struct {
		g.Meta `name:"gen"`
		cGenController
	}
)
// internal/ cmd/wg_gen_controller.go
package cmd

import "wsy-generator/internal/cmd/gen_controller"

type cGenController = gen_controller.GenController
// internal/cmd/gen_controller/gen_controller.go
package gen_controller

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/text/gstr"
	"net/http"
	"sync"
	"wsy-generator/internal/consts"
	"wsy-generator/internal/util"
)

type (
	GenController struct {
		g.Meta `name:"controller"`
	}
	GenControllerInput struct {
		g.Meta         `name:"controller" config:"gfcli.gen.ctrl" `
		A       string `name:"a" arg:"true"`
		B string `name:"b" arg:"true"`
	}
	GenControllerOutput    struct{}
)
func (receiver GenController) Index(ctx context.Context, in GenControllerInput) (out *GenControllerOutput, err error) {
  return 
}

What did you see happen?

当命令行为wg gen controller a b时,最后一个代码片段中in.A=="b",in.B==""

What did you expect to see?

当命令行为wg gen controller a b时,最后一个代码片段中in.A=="a" ,in.B=="b"

@wsy9981999 wsy9981999 added the bug It is confirmed a bug, but don't worry, we'll handle it. label Jun 30, 2024
@Issues-translate-bot Issues-translate-bot changed the title os/gcmd: 当从结构体创建子命令时,且子命令带有多个参数时解析出错 os/gcmd: Parsing error when creating a subcommand from a structure and the subcommand has multiple parameters. Jun 30, 2024
@wsy9981999
Copy link
Author

初步定位在os/gcmd/gcmd_command_object.go的 272行

arguments = arguments[argIndex:]

截取后argIndex没有改变导致的

@gqcn
Copy link
Member

gqcn commented Sep 28, 2024

@wsy9981999 @wsy9981999 Here I managed writing the codes that can reproduce this issue, do not thank me, it'a pleasure:

package main

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gctx"
	_ "net/http/pprof"
)

var (
	Wg  = &cWg{}
	Gen = cGen{}
)

type (
	// cWg manages the root binary name.
	cWg struct {
		g.Meta `name:"wg"`
	}
	cWgInput struct {
		g.Meta `name:"wg"`
	}
	cWgOutput struct{}

	// cGen manages the first command.
	cGen struct {
		g.Meta `name:"gen"`
		cGenController
	}

	// cGenController manages the second command.
	cGenController struct {
		g.Meta `name:"controller"`
	}
	cGenControllerInput struct {
		g.Meta `name:"controller"`
		A      string `name:"a" arg:"true"`
		B      string `name:"b" arg:"true"`
	}
	cGenControllerOutput struct{}
)

func (receiver cGenController) Index(ctx context.Context, in cGenControllerInput) (out *cGenControllerOutput, err error) {
	g.Dump(in)
	return
}

func (receiver *cWg) Index(ctx context.Context, in cWgInput) (out *cWgOutput, err error) {
	return
}

func main() {
	ctx := gctx.GetInitCtx()
	object, err := gcmd.NewFromObject(Wg)
	if err != nil {
		g.Log().Fatalf(ctx, "%+v", err)
	}
	if err = object.AddObject(Gen); err != nil {
		g.Log().Fatalf(ctx, "%+v", err)
	}
	object.Run(ctx)
}

QQ_1727495374548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants