Skip to main content
Version: 2.8.x(Latest)

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

func Init(args ...string)

func GetArg(index int, def ...string) *gvar.Var
func GetArgAll() []string

func GetOpt(name string, def ...string) *gvar.Var
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:

func ExampleInit() {
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
fmt.Printf(`%#v`, gcmd.GetArgAll())

// Output:
// []string{"gf", "build", "main.go"}
}

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:

func ExampleGetArg() {
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
fmt.Printf(
`Arg[0]: "%v", Arg[1]: "%v", Arg[2]: "%v", Arg[3]: "%v"`,
gcmd.GetArg(0), gcmd.GetArg(1), gcmd.GetArg(2), gcmd.GetArg(3),
)

// Output:
// Arg[0]: "gf", Arg[1]: "build", Arg[2]: "main.go", Arg[3]: ""
}

func ExampleGetArgAll() {
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
fmt.Printf(`%#v`, gcmd.GetArgAll())

// Output:
// []string{"gf", "build", "main.go"}
}

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:

func ExampleGetOpt() {
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
fmt.Printf(
`Opt["o"]: "%v", Opt["y"]: "%v", Opt["d"]: "%v"`,
gcmd.GetOpt("o"), gcmd.GetOpt("y"), gcmd.GetOpt("d", "default value"),
)

// Output:
// Opt["o"]: "gf.exe", Opt["y"]: "", Opt["d"]: "default value"
}

func ExampleGetOptAll() {
gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y")
fmt.Printf(`%#v`, gcmd.GetOptAll())

// May Output:
// map[string]string{"o":"gf.exe", "y":""}
}

GetOptWithEnv Feature

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:

func ExampleGetOptWithEnv() {
fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))
_ = genv.Set("GF_TEST", "YES")
fmt.Printf("Opt[gf.test]:%s\n", gcmd.GetOptWithEnv("gf.test"))

// Output:
// Opt[gf.test]:
// Opt[gf.test]:YES
}