github.com/gogf/gf/v2@v2.7.4/os/gcmd/gcmd_z_example_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  package gcmd_test
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"os"
    13  
    14  	"github.com/gogf/gf/v2/frame/g"
    15  	"github.com/gogf/gf/v2/os/gcmd"
    16  	"github.com/gogf/gf/v2/os/gctx"
    17  	"github.com/gogf/gf/v2/os/genv"
    18  )
    19  
    20  func ExampleInit() {
    21  	gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    22  	fmt.Printf(`%#v`, gcmd.GetArgAll())
    23  
    24  	// Output:
    25  	// []string{"gf", "build", "main.go"}
    26  }
    27  
    28  func ExampleGetArg() {
    29  	gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    30  	fmt.Printf(
    31  		`Arg[0]: "%v", Arg[1]: "%v", Arg[2]: "%v", Arg[3]: "%v"`,
    32  		gcmd.GetArg(0), gcmd.GetArg(1), gcmd.GetArg(2), gcmd.GetArg(3),
    33  	)
    34  
    35  	// Output:
    36  	// Arg[0]: "gf", Arg[1]: "build", Arg[2]: "main.go", Arg[3]: ""
    37  }
    38  
    39  func ExampleGetArgAll() {
    40  	gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    41  	fmt.Printf(`%#v`, gcmd.GetArgAll())
    42  
    43  	// Output:
    44  	// []string{"gf", "build", "main.go"}
    45  }
    46  
    47  func ExampleGetOpt() {
    48  	gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    49  	fmt.Printf(
    50  		`Opt["o"]: "%v", Opt["y"]: "%v", Opt["d"]: "%v"`,
    51  		gcmd.GetOpt("o"), gcmd.GetOpt("y"), gcmd.GetOpt("d", "default value"),
    52  	)
    53  
    54  	// Output:
    55  	// Opt["o"]: "gf.exe", Opt["y"]: "", Opt["d"]: "default value"
    56  }
    57  
    58  func ExampleGetOpt_Def() {
    59  	gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    60  
    61  	fmt.Println(gcmd.GetOpt("s", "Def").String())
    62  
    63  	// Output:
    64  	// Def
    65  }
    66  
    67  func ExampleGetOptAll() {
    68  	gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
    69  	fmt.Printf(`%#v`, gcmd.GetOptAll())
    70  
    71  	// May Output:
    72  	// map[string]string{"o":"gf.exe", "y":""}
    73  }
    74  
    75  func ExampleGetOptWithEnv() {
    76  	fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))
    77  	_ = genv.Set("GF_TEST", "YES")
    78  	fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))
    79  
    80  	// Output:
    81  	// Opt[gf.test]:
    82  	// Opt[gf.test]:YES
    83  }
    84  
    85  func ExampleParse() {
    86  	os.Args = []string{"gf", "build", "main.go", "-o=gf.exe", "-y"}
    87  	p, err := gcmd.Parse(g.MapStrBool{
    88  		"o,output": true,
    89  		"y,yes":    false,
    90  	})
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  	fmt.Println(p.GetOpt("o"))
    95  	fmt.Println(p.GetOpt("output"))
    96  	fmt.Println(p.GetOpt("y") != nil)
    97  	fmt.Println(p.GetOpt("yes") != nil)
    98  	fmt.Println(p.GetOpt("none") != nil)
    99  	fmt.Println(p.GetOpt("none", "Def"))
   100  
   101  	// Output:
   102  	// gf.exe
   103  	// gf.exe
   104  	// true
   105  	// true
   106  	// false
   107  	// Def
   108  }
   109  
   110  func ExampleCommandFromCtx() {
   111  	var (
   112  		command = gcmd.Command{
   113  			Name: "start",
   114  		}
   115  	)
   116  
   117  	ctx := context.WithValue(gctx.New(), gcmd.CtxKeyCommand, &command)
   118  	unAddCtx := context.WithValue(gctx.New(), gcmd.CtxKeyCommand, &gcmd.Command{})
   119  	nonKeyCtx := context.WithValue(gctx.New(), "Testkey", &gcmd.Command{})
   120  
   121  	fmt.Println(gcmd.CommandFromCtx(ctx).Name)
   122  	fmt.Println(gcmd.CommandFromCtx(unAddCtx).Name)
   123  	fmt.Println(gcmd.CommandFromCtx(nonKeyCtx) == nil)
   124  
   125  	// Output:
   126  	// start
   127  	//
   128  	// true
   129  }
   130  
   131  func ExampleCommand_AddCommand() {
   132  	commandRoot := &gcmd.Command{
   133  		Name: "gf",
   134  	}
   135  	commandRoot.AddCommand(&gcmd.Command{
   136  		Name: "start",
   137  	}, &gcmd.Command{})
   138  
   139  	commandRoot.Print()
   140  
   141  	// Output:
   142  	// USAGE
   143  	//     gf COMMAND [OPTION]
   144  	//
   145  	// COMMAND
   146  	//     start
   147  }
   148  
   149  func ExampleCommand_AddCommand_Repeat() {
   150  	commandRoot := &gcmd.Command{
   151  		Name: "gf",
   152  	}
   153  	err := commandRoot.AddCommand(&gcmd.Command{
   154  		Name: "start",
   155  	}, &gcmd.Command{
   156  		Name: "stop",
   157  	}, &gcmd.Command{
   158  		Name: "start",
   159  	})
   160  
   161  	fmt.Println(err)
   162  
   163  	// Output:
   164  	// command "start" is already added to command "gf"
   165  }
   166  
   167  func ExampleCommand_AddObject() {
   168  	var (
   169  		command = gcmd.Command{
   170  			Name: "start",
   171  		}
   172  	)
   173  
   174  	command.AddObject(&TestCmdObject{})
   175  
   176  	command.Print()
   177  
   178  	// Output:
   179  	// USAGE
   180  	//     start COMMAND [OPTION]
   181  	//
   182  	// COMMAND
   183  	//     root    root env command
   184  }
   185  
   186  func ExampleCommand_AddObject_Error() {
   187  	var (
   188  		command = gcmd.Command{
   189  			Name: "start",
   190  		}
   191  	)
   192  
   193  	err := command.AddObject(&[]string{"Test"})
   194  
   195  	fmt.Println(err)
   196  
   197  	// Output:
   198  	// input object should be type of struct, but got "*[]string"
   199  }
   200  
   201  func ExampleCommand_Print() {
   202  	commandRoot := &gcmd.Command{
   203  		Name: "gf",
   204  	}
   205  	commandRoot.AddCommand(&gcmd.Command{
   206  		Name: "start",
   207  	}, &gcmd.Command{})
   208  
   209  	commandRoot.Print()
   210  
   211  	// Output:
   212  	// USAGE
   213  	//     gf COMMAND [OPTION]
   214  	//
   215  	// COMMAND
   216  	//     start
   217  }
   218  
   219  func ExampleScan() {
   220  	fmt.Println(gcmd.Scan("gf scan"))
   221  
   222  	// Output:
   223  	// gf scan
   224  }
   225  
   226  func ExampleScanf() {
   227  	fmt.Println(gcmd.Scanf("gf %s", "scanf"))
   228  
   229  	// Output:
   230  	// gf scanf
   231  }
   232  
   233  func ExampleParserFromCtx() {
   234  	parser, _ := gcmd.Parse(nil)
   235  
   236  	ctx := context.WithValue(gctx.New(), gcmd.CtxKeyParser, parser)
   237  	nilCtx := context.WithValue(gctx.New(), "NilCtxKeyParser", parser)
   238  
   239  	fmt.Println(gcmd.ParserFromCtx(ctx).GetArgAll())
   240  	fmt.Println(gcmd.ParserFromCtx(nilCtx) == nil)
   241  
   242  	// Output:
   243  	// [gf build main.go]
   244  	// true
   245  }
   246  
   247  func ExampleParseArgs() {
   248  	p, _ := gcmd.ParseArgs([]string{
   249  		"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root",
   250  	}, nil)
   251  
   252  	fmt.Println(p.GetArgAll())
   253  	fmt.Println(p.GetOptAll())
   254  
   255  	// Output:
   256  	// [gf path]
   257  	// map[force:remove fq: n:root p:www]
   258  }
   259  
   260  func ExampleParser_GetArg() {
   261  	p, _ := gcmd.ParseArgs([]string{
   262  		"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root",
   263  	}, nil)
   264  
   265  	fmt.Println(p.GetArg(-1, "Def").String())
   266  	fmt.Println(p.GetArg(-1) == nil)
   267  
   268  	// Output:
   269  	// Def
   270  	// true
   271  }