github.com/gogf/gf/v2@v2.7.4/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  	"os"
    13  
    14  	"github.com/gogf/gf/v2/container/gvar"
    15  	"github.com/gogf/gf/v2/internal/command"
    16  	"github.com/gogf/gf/v2/internal/utils"
    17  	"github.com/gogf/gf/v2/os/gctx"
    18  )
    19  
    20  const (
    21  	CtxKeyParser         gctx.StrKey = `CtxKeyParser`
    22  	CtxKeyCommand        gctx.StrKey = `CtxKeyCommand`
    23  	CtxKeyArgumentsIndex gctx.StrKey = `CtxKeyArgumentsIndex`
    24  )
    25  
    26  const (
    27  	helpOptionName        = "help"
    28  	helpOptionNameShort   = "h"
    29  	maxLineChars          = 120
    30  	tracingInstrumentName = "github.com/gogf/gf/v2/os/gcmd.Command"
    31  	tagNameName           = "name"
    32  	tagNameShort          = "short"
    33  )
    34  
    35  // Init does custom initialization.
    36  func Init(args ...string) {
    37  	command.Init(args...)
    38  }
    39  
    40  // GetOpt returns the option value named `name` as gvar.Var.
    41  func GetOpt(name string, def ...string) *gvar.Var {
    42  	if v := command.GetOpt(name, def...); v != "" {
    43  		return gvar.New(v)
    44  	}
    45  	if command.ContainsOpt(name) {
    46  		return gvar.New("")
    47  	}
    48  	return nil
    49  }
    50  
    51  // GetOptAll returns all parsed options.
    52  func GetOptAll() map[string]string {
    53  	return command.GetOptAll()
    54  }
    55  
    56  // GetArg returns the argument at `index` as gvar.Var.
    57  func GetArg(index int, def ...string) *gvar.Var {
    58  	if v := command.GetArg(index, def...); v != "" {
    59  		return gvar.New(v)
    60  	}
    61  	return nil
    62  }
    63  
    64  // GetArgAll returns all parsed arguments.
    65  func GetArgAll() []string {
    66  	return command.GetArgAll()
    67  }
    68  
    69  // GetOptWithEnv returns the command line argument of the specified `key`.
    70  // If the argument does not exist, then it returns the environment variable with specified `key`.
    71  // It returns the default value `def` if none of them exists.
    72  //
    73  // Fetching Rules:
    74  // 1. Command line arguments are in lowercase format, eg: gf.`package name`.<variable name>;
    75  // 2. Environment arguments are in uppercase format, eg: GF_`package name`_<variable name>;
    76  func GetOptWithEnv(key string, def ...interface{}) *gvar.Var {
    77  	cmdKey := utils.FormatCmdKey(key)
    78  	if command.ContainsOpt(cmdKey) {
    79  		return gvar.New(GetOpt(cmdKey))
    80  	} else {
    81  		envKey := utils.FormatEnvKey(key)
    82  		if r, ok := os.LookupEnv(envKey); ok {
    83  			return gvar.New(r)
    84  		} else {
    85  			if len(def) > 0 {
    86  				return gvar.New(def[0])
    87  			}
    88  		}
    89  	}
    90  	return nil
    91  }
    92  
    93  // BuildOptions builds the options as string.
    94  func BuildOptions(m map[string]string, prefix ...string) string {
    95  	options := ""
    96  	leadStr := "-"
    97  	if len(prefix) > 0 {
    98  		leadStr = prefix[0]
    99  	}
   100  	for k, v := range m {
   101  		if len(options) > 0 {
   102  			options += " "
   103  		}
   104  		options += leadStr + k
   105  		if v != "" {
   106  			options += "=" + v
   107  		}
   108  	}
   109  	return options
   110  }