os/gcmd: Command can't get args, for all args are parsed as SubCommand, if SubCommand not found, return error.
Go version
go 1.22
GoFrame version
2.7.2
Can this bug be reproduced with the latest release?
Option Yes
What did you do?
package cmd
import (
"context"
"fmt"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
)
var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "...",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
fmt.Println("main")
g.Dump(parser.GetArgAll())
println("-----")
g.Dump(parser.GetOptAll())
return nil
},
}
)
then go run main.go abc
### What did you see happen?
error...
### What did you expect to see?
print the args
If you want to call a subcommand, add the implementation of the subcommand and you can use it normally
Main.AddCommand(&gcmd.Command{
Name: "abc",
})
If you want to call a subcommand, add the implementation of the subcommand and you can use it normally
Main.AddCommand(&gcmd.Command{ Name: "abc", })
Not subcommand, just pass the args from command line into Func, need get the args with parser.GetArgAll().
Complete running codes:
package main
import (
"context"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gcmd"
)
var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "...",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
fmt.Println("main")
g.Dump(parser.GetArgAll())
println("-----")
g.Dump(parser.GetOptAll())
return nil
},
}
)
func main() {
var ctx = context.Background()
Main.Run(ctx)
}
@gqcn ,找不到子命令时,应将其视为参数,不应该报错
Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑🤝🧑👫🧑🏿🤝🧑🏻👩🏾🤝👨🏿👬🏿
@gqcn, when a subcommand cannot be found, it should be treated as a parameter and no error should be reported.