github.com/gogf/gf/v2@v2.7.4/os/gcmd/gcmd_z_unit_parser_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcmd_test
    10  
    11  import (
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/v2/os/gcmd"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  )
    18  
    19  func Test_Parse(t *testing.T) {
    20  	gtest.C(t, func(t *gtest.T) {
    21  		os.Args = []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}
    22  		p, err := gcmd.Parse(map[string]bool{
    23  			"n, name":   true,
    24  			"p, prefix": true,
    25  			"f,force":   false,
    26  			"q,quiet":   false,
    27  		})
    28  		t.AssertNil(err)
    29  		t.Assert(len(p.GetArgAll()), 3)
    30  		t.Assert(p.GetArg(0), "gf")
    31  		t.Assert(p.GetArg(1), "remove")
    32  		t.Assert(p.GetArg(2), "path")
    33  		t.Assert(p.GetArg(2).String(), "path")
    34  
    35  		t.Assert(len(p.GetOptAll()), 8)
    36  		t.Assert(p.GetOpt("n"), "root")
    37  		t.Assert(p.GetOpt("name"), "root")
    38  		t.Assert(p.GetOpt("p"), "www")
    39  		t.Assert(p.GetOpt("prefix"), "www")
    40  		t.Assert(p.GetOpt("prefix").String(), "www")
    41  
    42  		t.Assert(p.GetOpt("n") != nil, true)
    43  		t.Assert(p.GetOpt("name") != nil, true)
    44  		t.Assert(p.GetOpt("p") != nil, true)
    45  		t.Assert(p.GetOpt("prefix") != nil, true)
    46  		t.Assert(p.GetOpt("f") != nil, true)
    47  		t.Assert(p.GetOpt("force") != nil, true)
    48  		t.Assert(p.GetOpt("q") != nil, true)
    49  		t.Assert(p.GetOpt("quiet") != nil, true)
    50  		t.Assert(p.GetOpt("none") != nil, false)
    51  
    52  		_, err = p.MarshalJSON()
    53  		t.AssertNil(err)
    54  	})
    55  }
    56  
    57  func Test_ParseArgs(t *testing.T) {
    58  	gtest.C(t, func(t *gtest.T) {
    59  		p, err := gcmd.ParseArgs(
    60  			[]string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"},
    61  			map[string]bool{
    62  				"n, name":   true,
    63  				"p, prefix": true,
    64  				"f,force":   false,
    65  				"q,quiet":   false,
    66  			})
    67  		t.AssertNil(err)
    68  		t.Assert(len(p.GetArgAll()), 3)
    69  		t.Assert(p.GetArg(0), "gf")
    70  		t.Assert(p.GetArg(1), "remove")
    71  		t.Assert(p.GetArg(2), "path")
    72  		t.Assert(p.GetArg(2).String(), "path")
    73  
    74  		t.Assert(len(p.GetOptAll()), 8)
    75  		t.Assert(p.GetOpt("n"), "root")
    76  		t.Assert(p.GetOpt("name"), "root")
    77  		t.Assert(p.GetOpt("p"), "www")
    78  		t.Assert(p.GetOpt("prefix"), "www")
    79  		t.Assert(p.GetOpt("prefix").String(), "www")
    80  
    81  		t.Assert(p.GetOpt("n") != nil, true)
    82  		t.Assert(p.GetOpt("name") != nil, true)
    83  		t.Assert(p.GetOpt("p") != nil, true)
    84  		t.Assert(p.GetOpt("prefix") != nil, true)
    85  		t.Assert(p.GetOpt("f") != nil, true)
    86  		t.Assert(p.GetOpt("force") != nil, true)
    87  		t.Assert(p.GetOpt("q") != nil, true)
    88  		t.Assert(p.GetOpt("quiet") != nil, true)
    89  		t.Assert(p.GetOpt("none") != nil, false)
    90  	})
    91  }