mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
Improving gcmd Code Coverage
This commit is contained in:
@ -200,7 +200,7 @@ func (p *Parser) GetOptAll() map[string]string {
|
||||
|
||||
// GetArg returns the argument at `index` as gvar.Var.
|
||||
func (p *Parser) GetArg(index int, def ...string) *gvar.Var {
|
||||
if index < len(p.parsedArgs) {
|
||||
if index >= 0 && index < len(p.parsedArgs) {
|
||||
return gvar.New(p.parsedArgs[index])
|
||||
}
|
||||
if len(def) > 0 {
|
||||
|
||||
@ -55,6 +55,15 @@ func ExampleGetOpt() {
|
||||
// Opt["o"]: "gf.exe", Opt["y"]: "", Opt["d"]: "default value"
|
||||
}
|
||||
|
||||
func ExampleGetOpt_Def() {
|
||||
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
|
||||
|
||||
fmt.Println(gcmd.GetOpt("s", "Def").String())
|
||||
|
||||
// Output:
|
||||
// Def
|
||||
}
|
||||
|
||||
func ExampleGetOptAll() {
|
||||
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
|
||||
fmt.Printf(`%#v`, gcmd.GetOptAll())
|
||||
@ -87,6 +96,7 @@ func ExampleParse() {
|
||||
fmt.Println(p.GetOpt("y") != nil)
|
||||
fmt.Println(p.GetOpt("yes") != nil)
|
||||
fmt.Println(p.GetOpt("none") != nil)
|
||||
fmt.Println(p.GetOpt("none", "Def"))
|
||||
|
||||
// Output:
|
||||
// gf.exe
|
||||
@ -94,6 +104,7 @@ func ExampleParse() {
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// Def
|
||||
}
|
||||
|
||||
func ExampleCommandFromCtx() {
|
||||
@ -204,3 +215,57 @@ func ExampleCommand_Print() {
|
||||
//COMMAND
|
||||
// start
|
||||
}
|
||||
|
||||
func ExampleScan() {
|
||||
fmt.Println(gcmd.Scan("gf scan"))
|
||||
|
||||
// Output:
|
||||
// gf scan
|
||||
}
|
||||
|
||||
func ExampleScanf() {
|
||||
fmt.Println(gcmd.Scanf("gf %s", "scanf"))
|
||||
|
||||
// Output:
|
||||
// gf scanf
|
||||
}
|
||||
|
||||
func ExampleParserFromCtx() {
|
||||
parser, _ := gcmd.Parse(nil)
|
||||
|
||||
ctx := context.WithValue(gctx.New(), gcmd.CtxKeyParser, parser)
|
||||
nilCtx := context.WithValue(gctx.New(), "NilCtxKeyParser", parser)
|
||||
|
||||
fmt.Println(gcmd.ParserFromCtx(ctx).GetArgAll())
|
||||
fmt.Println(gcmd.ParserFromCtx(nilCtx) == nil)
|
||||
|
||||
// Output:
|
||||
// [gf build main.go]
|
||||
// true
|
||||
}
|
||||
|
||||
func ExampleParseArgs() {
|
||||
p, _ := gcmd.ParseArgs([]string{
|
||||
"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root",
|
||||
}, nil)
|
||||
|
||||
fmt.Println(p.GetArgAll())
|
||||
fmt.Println(p.GetOptAll())
|
||||
|
||||
// Output:
|
||||
// [gf path]
|
||||
// map[force:remove fq: n:root p:www]
|
||||
}
|
||||
|
||||
func ExampleParser_GetArg() {
|
||||
p, _ := gcmd.ParseArgs([]string{
|
||||
"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root",
|
||||
}, nil)
|
||||
|
||||
fmt.Println(p.GetArg(-1, "Def").String())
|
||||
fmt.Println(p.GetArg(-1) == nil)
|
||||
|
||||
// Output:
|
||||
// Def
|
||||
// true
|
||||
}
|
||||
|
||||
@ -48,6 +48,9 @@ func Test_Parse(t *testing.T) {
|
||||
t.Assert(p.GetOpt("q") != nil, true)
|
||||
t.Assert(p.GetOpt("quiet") != nil, true)
|
||||
t.Assert(p.GetOpt("none") != nil, false)
|
||||
|
||||
_, err = p.MarshalJSON()
|
||||
t.AssertNil(err)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user