gf/os/gcmd/gcmd.go

162 lines
3.9 KiB
Go
Raw Normal View History

2019-09-16 20:57:43 +08:00
// Copyright 2019 gf Author(https://github.com/gogf/gf). 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,
// You can obtain one at https://github.com/gogf/gf.
//
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.
package gcmd
2017-11-23 10:21:28 +08:00
2019-09-05 15:16:25 +08:00
import (
"os"
"strings"
2019-09-05 15:16:25 +08:00
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/text/gregex"
)
var (
2019-09-16 20:57:43 +08:00
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
defaultCommandFuncMap = make(map[string]func())
2019-09-05 15:16:25 +08:00
)
// Custom initialization.
2020-12-04 23:29:20 +08:00
func Init(args ...string) {
if len(args) == 0 {
if len(defaultParsedArgs) == 0 && len(defaultParsedOptions) == 0 {
args = os.Args
} else {
return
}
} else {
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
2019-09-05 15:16:25 +08:00
}
// Parsing os.Args with default algorithm.
2020-12-04 23:29:20 +08:00
for i := 0; i < len(args); {
array, _ := gregex.MatchString(`^\-{1,2}([\w\?\.\-]+)(=){0,1}(.*)$`, args[i])
if len(array) > 2 {
if array[2] == "=" {
defaultParsedOptions[array[1]] = array[3]
} else if i < len(args)-1 {
2020-12-14 23:00:22 +08:00
if len(args[i+1]) > 0 && args[i+1][0] == '-' {
// Eg: gf gen -d -n 1
2020-12-04 23:29:20 +08:00
defaultParsedOptions[array[1]] = array[3]
} else {
2020-12-14 23:00:22 +08:00
// Eg: gf gen -n 2
2020-12-04 23:29:20 +08:00
defaultParsedOptions[array[1]] = args[i+1]
i += 2
continue
}
} else {
2020-12-14 23:00:22 +08:00
// Eg: gf gen -h
defaultParsedOptions[array[1]] = array[3]
2020-12-04 23:29:20 +08:00
}
2019-09-05 15:16:25 +08:00
} else {
2020-12-04 23:29:20 +08:00
defaultParsedArgs = append(defaultParsedArgs, args[i])
2019-09-05 15:16:25 +08:00
}
2020-12-04 23:29:20 +08:00
i++
2019-09-05 15:16:25 +08:00
}
}
// GetOpt returns the option value named <name>.
func GetOpt(name string, def ...string) string {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
if v, ok := defaultParsedOptions[name]; ok {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
}
// GetOptVar returns the option value named <name> as gvar.Var.
func GetOptVar(name string, def ...string) *gvar.Var {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
return gvar.New(GetOpt(name, def...))
}
// GetOptAll returns all parsed options.
func GetOptAll() map[string]string {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
return defaultParsedOptions
}
// ContainsOpt checks whether option named <name> exist in the arguments.
func ContainsOpt(name string, def ...string) bool {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
_, ok := defaultParsedOptions[name]
return ok
}
// GetArg returns the argument at <index>.
func GetArg(index int, def ...string) string {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
if index < len(defaultParsedArgs) {
return defaultParsedArgs[index]
}
if len(def) > 0 {
return def[0]
}
return ""
}
// GetArgVar returns the argument at <index> as gvar.Var.
func GetArgVar(index int, def ...string) *gvar.Var {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
return gvar.New(GetArg(index, def...))
}
// GetArgAll returns all parsed arguments.
func GetArgAll() []string {
2020-12-04 23:29:20 +08:00
Init()
2019-09-05 15:16:25 +08:00
return defaultParsedArgs
}
// GetWithEnv 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.
//
// Fetching Rules:
// 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>
func GetWithEnv(key string, def ...interface{}) *gvar.Var {
value := interface{}(nil)
if len(def) > 0 {
value = def[0]
}
cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
if v := GetOpt(cmdKey); v != "" {
value = v
} else {
envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
if v := os.Getenv(envKey); v != "" {
value = v
}
}
return gvar.New(value)
}
// 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 := "-"
if len(prefix) > 0 {
2019-09-05 11:38:36 +08:00
leadStr = prefix[0]
}
for k, v := range m {
if len(options) > 0 {
options += " "
}
2019-09-05 11:38:36 +08:00
options += leadStr + k
if v != "" {
options += "=" + v
}
}
return options
}