The gcmd component provides commonly used basic package methods that allow you to directly retrieve command line arguments and options according to default parsing rules.

Common Methods

For more component methods, please refer to the API documentation: https://pkg.go.dev/github.com/gogf/gf/v2/os/gcmd

  1. func Init(args ...string)
  2. func GetArg(index int, def ...string) *gvar.Var
  3. func GetArgAll() []string
  4. func GetOpt(name string, def ...string) *gvar.Var
  5. func GetOptAll() map[string]string

Init Custom Command Line

By default, the gcmd component automatically parses and retrieves parameters and data from os.Args. You can customize command line data using the Init method. Example usage:

  1. func ExampleInit() {
  2. gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
  3. fmt.Printf(`%#v`, gcmd.GetArgAll())
  4. // Output:
  5. // []string{"gf", "build", "main.go"}
  6. }

GetArg* Argument Retrieval

Argument retrieval can be done through the following two methods:

  1. The GetArg method is used to retrieve the command line arguments parsed by default. Arguments are retrieved by input index position starting from 0, though often the arguments we need to get start from 1 since the argument at index 0 is the program name.
  2. The GetArgAll method retrieves all command line arguments.

Example usage:

  1. func ExampleGetArg() {
  2. gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
  3. fmt.Printf(
  4. `Arg[0]: "%v", Arg[1]: "%v", Arg[2]: "%v", Arg[3]: "%v"`,
  5. gcmd.GetArg(0), gcmd.GetArg(1), gcmd.GetArg(2), gcmd.GetArg(3),
  6. )
  7. // Output:
  8. // Arg[0]: "gf", Arg[1]: "build", Arg[2]: "main.go", Arg[3]: ""
  9. }
  10. func ExampleGetArgAll() {
  11. gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
  12. fmt.Printf(`%#v`, gcmd.GetArgAll())
  13. // Output:
  14. // []string{"gf", "build", "main.go"}
  15. }

GetOpt* Option Retrieval

Option retrieval can be done through the following two methods:

  1. The GetOpt method is used to retrieve the command line options parsed by default. Options are retrieved by name, and their input is not ordered, meaning they can be input at any command line position. If the data for a given option name does not exist, it returns nil. Note that to check whether an option without data exists, you can use GetOpt(name) != nil.
  2. The GetOptAll method retrieves all options.

Example usage:

  1. func ExampleGetOpt() {
  2. gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
  3. fmt.Printf(
  4. `Opt["o"]: "%v", Opt["y"]: "%v", Opt["d"]: "%v"`,
  5. gcmd.GetOpt("o"), gcmd.GetOpt("y"), gcmd.GetOpt("d", "default value"),
  6. )
  7. // Output:
  8. // Opt["o"]: "gf.exe", Opt["y"]: "", Opt["d"]: "default value"
  9. }
  10. func ExampleGetOptAll() {
  11. gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
  12. fmt.Printf(`%#v`, gcmd.GetOptAll())
  13. // May Output:
  14. // map[string]string{"o":"gf.exe", "y":""}
  15. }

GetOptWithEnv Feature

  1. func GetOptWithEnv(key string, def ...interface{}) *gvar.Var

This method retrieves the specified option value from the command line and if the option does not exist, it reads from the environment variable instead. However, the naming rules differ. For example, gcmd.GetOptWithEnv("gf.debug") first reads the gf.debug option from the command line and if it does not exist, it reads the GF_DEBUG environment variable.

Note the parameter naming conversion rules:

  • Environment variables convert names to uppercase, and . characters in names become _.
  • On the command line, names are converted to lowercase and _ characters in names become ..

Example usage:

  1. func ExampleGetOptWithEnv() {
  2. fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))
  3. _ = genv.Set("GF_TEST", "YES")
  4. fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))
  5. // Output:
  6. // Opt[gf.test]:
  7. // Opt[gf.test]:YES
  8. }