github.com/gogf/gf@v1.16.9/os/gcmd/gcmd.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  
     8  // Package gcmd provides console operations, like options/arguments reading and command running.
     9  package gcmd
    10  
    11  import (
    12  	"github.com/gogf/gf/container/gvar"
    13  	"github.com/gogf/gf/internal/command"
    14  	"os"
    15  	"strings"
    16  )
    17  
    18  var (
    19  	defaultCommandFuncMap = make(map[string]func())
    20  )
    21  
    22  // Custom initialization.
    23  func Init(args ...string) {
    24  	command.Init(args...)
    25  }
    26  
    27  // GetOpt returns the option value named <name>.
    28  func GetOpt(name string, def ...string) string {
    29  	Init()
    30  	return command.GetOpt(name, def...)
    31  }
    32  
    33  // GetOptVar returns the option value named <name> as gvar.Var.
    34  func GetOptVar(name string, def ...string) *gvar.Var {
    35  	Init()
    36  	return gvar.New(GetOpt(name, def...))
    37  }
    38  
    39  // GetOptAll returns all parsed options.
    40  func GetOptAll() map[string]string {
    41  	Init()
    42  	return command.GetOptAll()
    43  }
    44  
    45  // ContainsOpt checks whether option named <name> exist in the arguments.
    46  func ContainsOpt(name string) bool {
    47  	Init()
    48  	return command.ContainsOpt(name)
    49  }
    50  
    51  // GetArg returns the argument at <index>.
    52  func GetArg(index int, def ...string) string {
    53  	Init()
    54  	return command.GetArg(index, def...)
    55  }
    56  
    57  // GetArgVar returns the argument at <index> as gvar.Var.
    58  func GetArgVar(index int, def ...string) *gvar.Var {
    59  	Init()
    60  	return gvar.New(GetArg(index, def...))
    61  }
    62  
    63  // GetArgAll returns all parsed arguments.
    64  func GetArgAll() []string {
    65  	Init()
    66  	return command.GetArgAll()
    67  }
    68  
    69  // GetWithEnv is alias of GetOptWithEnv.
    70  // Deprecated, use GetOptWithEnv instead.
    71  func GetWithEnv(key string, def ...interface{}) *gvar.Var {
    72  	return GetOptWithEnv(key, def...)
    73  }
    74  
    75  // GetOptWithEnv returns the command line argument of the specified <key>.
    76  // If the argument does not exist, then it returns the environment variable with specified <key>.
    77  // It returns the default value <def> if none of them exists.
    78  //
    79  // Fetching Rules:
    80  // 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
    81  // 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
    82  func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
    83  	cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
    84  	if ContainsOpt(cmdKey) {
    85  		return gvar.New(GetOpt(cmdKey))
    86  	} else {
    87  		envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
    88  		if r, ok := os.LookupEnv(envKey); ok {
    89  			return gvar.New(r)
    90  		} else {
    91  			if len(def) > 0 {
    92  				return gvar.New(def[0])
    93  			}
    94  		}
    95  	}
    96  	return gvar.New(nil)
    97  }
    98  
    99  // BuildOptions builds the options as string.
   100  func BuildOptions(m map[string]string, prefix ...string) string {
   101  	options := ""
   102  	leadStr := "-"
   103  	if len(prefix) > 0 {
   104  		leadStr = prefix[0]
   105  	}
   106  	for k, v := range m {
   107  		if len(options) > 0 {
   108  			options += " "
   109  		}
   110  		options += leadStr + k
   111  		if v != "" {
   112  			options += "=" + v
   113  		}
   114  	}
   115  	return options
   116  }