2021-01-17 21:46:25 +08:00
|
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-30 23:49:55 +08:00
|
|
|
|
//
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2019-09-05 11:38:36 +08:00
|
|
|
|
// Package gcmd provides console operations, like options/arguments reading and command running.
|
2017-12-30 23:49:55 +08:00
|
|
|
|
package gcmd
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2019-09-05 15:16:25 +08:00
|
|
|
|
import (
|
|
|
|
|
"os"
|
2021-11-15 20:26:31 +08:00
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
|
"github.com/gogf/gf/v2/container/gvar"
|
|
|
|
|
"github.com/gogf/gf/v2/internal/command"
|
2021-10-21 18:22:47 +08:00
|
|
|
|
"github.com/gogf/gf/v2/internal/utils"
|
2021-11-24 21:57:41 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
2021-12-09 23:49:39 +08:00
|
|
|
|
CtxKeyParser gctx.StrKey = `CtxKeyParser`
|
|
|
|
|
CtxKeyCommand gctx.StrKey = `CtxKeyCommand`
|
|
|
|
|
CtxKeyArguments gctx.StrKey = `CtxKeyArguments`
|
2019-09-05 15:16:25 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-05-26 22:08:13 +08:00
|
|
|
|
const (
|
|
|
|
|
helpOptionName = "help"
|
|
|
|
|
helpOptionNameShort = "h"
|
2021-11-21 13:09:45 +08:00
|
|
|
|
maxLineChars = 120
|
2019-09-05 15:16:25 +08:00
|
|
|
|
)
|
|
|
|
|
|
2021-05-26 22:08:13 +08:00
|
|
|
|
// Init does custom initialization.
|
2020-12-04 23:29:20 +08:00
|
|
|
|
func Init(args ...string) {
|
2021-02-04 00:10:13 +08:00
|
|
|
|
command.Init(args...)
|
2019-09-05 15:16:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// GetOpt returns the option value named `name` as gvar.Var.
|
|
|
|
|
func GetOpt(name string, def ...string) *gvar.Var {
|
|
|
|
|
if v := command.GetOpt(name, def...); v != "" {
|
|
|
|
|
return gvar.New(v)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2019-09-05 15:16:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetOptAll returns all parsed options.
|
|
|
|
|
func GetOptAll() map[string]string {
|
2021-02-04 00:10:13 +08:00
|
|
|
|
return command.GetOptAll()
|
2019-09-05 15:16:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// ContainsOpt checks whether option named `name` exist in the arguments.
|
2021-02-04 00:10:13 +08:00
|
|
|
|
func ContainsOpt(name string) bool {
|
|
|
|
|
return command.ContainsOpt(name)
|
2019-09-05 15:16:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// GetArg returns the argument at `index` as gvar.Var.
|
|
|
|
|
func GetArg(index int, def ...string) *gvar.Var {
|
|
|
|
|
if v := command.GetArg(index, def...); v != "" {
|
|
|
|
|
return gvar.New(v)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2019-09-05 15:16:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetArgAll returns all parsed arguments.
|
|
|
|
|
func GetArgAll() []string {
|
2021-02-04 00:10:13 +08:00
|
|
|
|
return command.GetArgAll()
|
2019-09-05 15:16:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// GetOptWithEnv returns the command line argument of the specified `key`.
|
|
|
|
|
// If the argument does not exist, then it returns the environment variable with specified `key`.
|
|
|
|
|
// It returns the default value `def` if none of them exists.
|
2020-12-04 23:44:54 +08:00
|
|
|
|
//
|
|
|
|
|
// Fetching Rules:
|
2021-10-21 18:22:47 +08:00
|
|
|
|
// 1. Command line arguments are in lowercase format, eg: gf.`package name`.<variable name>;
|
|
|
|
|
// 2. Environment arguments are in uppercase format, eg: GF_`package name`_<variable name>;
|
2021-02-02 15:16:09 +08:00
|
|
|
|
func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
|
2021-10-21 18:22:47 +08:00
|
|
|
|
cmdKey := utils.FormatCmdKey(key)
|
2021-02-04 00:10:13 +08:00
|
|
|
|
if ContainsOpt(cmdKey) {
|
|
|
|
|
return gvar.New(GetOpt(cmdKey))
|
2020-12-04 23:44:54 +08:00
|
|
|
|
} else {
|
2021-10-21 18:22:47 +08:00
|
|
|
|
envKey := utils.FormatEnvKey(key)
|
2021-02-04 00:10:13 +08:00
|
|
|
|
if r, ok := os.LookupEnv(envKey); ok {
|
|
|
|
|
return gvar.New(r)
|
|
|
|
|
} else {
|
|
|
|
|
if len(def) > 0 {
|
|
|
|
|
return gvar.New(def[0])
|
|
|
|
|
}
|
2020-12-04 23:44:54 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-21 18:22:47 +08:00
|
|
|
|
return nil
|
2020-12-04 23:44:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 21:01:04 +08:00
|
|
|
|
// BuildOptions builds the options as string.
|
|
|
|
|
func BuildOptions(m map[string]string, prefix ...string) string {
|
|
|
|
|
options := ""
|
2019-09-05 11:38:36 +08:00
|
|
|
|
leadStr := "-"
|
2019-07-25 21:01:04 +08:00
|
|
|
|
if len(prefix) > 0 {
|
2019-09-05 11:38:36 +08:00
|
|
|
|
leadStr = prefix[0]
|
2019-07-25 21:01:04 +08:00
|
|
|
|
}
|
|
|
|
|
for k, v := range m {
|
|
|
|
|
if len(options) > 0 {
|
|
|
|
|
options += " "
|
|
|
|
|
}
|
2019-09-05 11:38:36 +08:00
|
|
|
|
options += leadStr + k
|
2019-07-25 21:01:04 +08:00
|
|
|
|
if v != "" {
|
|
|
|
|
options += "=" + v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return options
|
|
|
|
|
}
|