Skip to content

Commit

Permalink
added MapArgs function
Browse files Browse the repository at this point in the history
  • Loading branch information
AspieSoft committed Nov 2, 2022
1 parent 99fa604 commit 22e84da
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
103 changes: 103 additions & 0 deletions goutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,109 @@ func FormatMemoryUsage(b uint64) float64 {
return math.Round(float64(b) / 1024 / 1024 * 100) / 100
}


// MapArgs will convert a bash argument array ([]string) into a map (map[string]string)
//
// When @args is left blank with no values, it will default to os.Args[1:]
//
// -- Arg Convertions:
//
// "--Key=value" will convert to "key:value"
//
// "--boolKey" will convert to "boolKey:true"
//
// "-flags" will convert to "f:true, l:true, a:true, g:true, s:true" (only if its alphanumeric [A-Za-z0-9])
// if -flags is not alphanumeric (example: "-test.paniconexit0" "-test.timeout=10m0s") it will be treated as a --flag (--key=value --boolKey)
//
// keys that match a number ("--1" or "-1") will start with a "-" ("--1=value" -> "-1:value", "-1" -> -1:true)
// this prevents a number key from conflicting with an index key
//
// everything else is given a number value index starting with 0
//
// this method will not allow --args to have their values modified after they have already been set
func MapArgs(args ...[]string) map[string]string {
if len(args) == 0 {
args = append(args, os.Args[1:])
}

argMap := map[string]string{}
i := 0

for _, argList := range args {
for _, arg := range argList {
if strings.HasPrefix(arg, "--") {
arg = arg[2:]
if strings.ContainsRune(arg, '=') {
data := strings.SplitN(arg, "=", 2)
if _, err := strconv.Atoi(data[0]); err == nil {
if argMap["-"+data[0]] == "" {
argMap["-"+data[0]] = data[1]
}
}else{
if argMap[data[0]] == "" {
argMap[data[0]] = data[1]
}
}
}else{
if _, err := strconv.Atoi(arg); err == nil {
if argMap["-"+arg] == "" {
argMap["-"+arg] = "true"
}
}else{
if argMap[arg] == "" {
argMap[arg] = "true"
}
}
}
}else if strings.HasPrefix(arg, "-") {
arg = arg[1:]
if regex.Match([]byte(arg), regex.Compile(`^[A-Za-z0-9]+$`)) {
flags := strings.Split(arg, "")
for _, flag := range flags {
if _, err := strconv.Atoi(flag); err == nil {
if argMap["-"+flag] == "" {
argMap["-"+flag] = "true"
}
}else{
if argMap[flag] == "" {
argMap[flag] = "true"
}
}
}
}else{
if strings.ContainsRune(arg, '=') {
data := strings.SplitN(arg, "=", 2)
if _, err := strconv.Atoi(data[0]); err == nil {
if argMap["-"+data[0]] == "" {
argMap["-"+data[0]] = data[1]
}
}else{
if argMap[data[0]] == "" {
argMap[data[0]] = data[1]
}
}
}else{
if _, err := strconv.Atoi(arg); err == nil {
if argMap["-"+arg] == "" {
argMap["-"+arg] = "true"
}
}else{
if argMap[arg] == "" {
argMap[arg] = "true"
}
}
}
}
}else{
argMap[strconv.Itoa(i)] = arg
i++
}
}
}

return argMap
}

var regEscHTML *regex.Regexp = regex.Compile(`[<>&]`)
var regEscFixAmp *regex.Regexp = regex.Compile(`&amp;(amp;)*`)

Expand Down
4 changes: 4 additions & 0 deletions goutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func Test(t *testing.T){
if val, err := JoinPath("test", "../out/of/root"); err == nil {
t.Error("[", val, "]\n", errors.New("JoinPath Method Leaked Outsite The Root"))
}

if args := MapArgs([]string{"arg1", "--key=value", "--bool", "-flags"}); args["0"] != "arg1" || args["bool"] != "true" || args["key"] != "value" || args["f"] != "true" || args["l"] != "true" || args["s"] != "true" {
t.Error(args, "\n", errors.New("MapArgs Produced The Wrong Output"))
}
}

func TestEncrypt(t *testing.T){
Expand Down

0 comments on commit 22e84da

Please sign in to comment.