Created
March 12, 2024 13:10
-
-
Save lsongdev/3a2d48f52050ec1332ee7329aed632c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cli | |
import ( | |
"os" | |
"regexp" | |
"strings" | |
) | |
func ParseArgs() ([]string, map[string]any) { | |
args := []string{} | |
flags := map[string]any{} | |
re := regexp.MustCompile(`^--(\w+)(=(.+))?$`) | |
for _, arg := range os.Args[1:] { | |
if re.Match([]byte(arg)) { | |
p := re.FindStringSubmatch(arg) | |
k := p[1] | |
var v any | |
v = p[3] | |
if v == "" { | |
v = true | |
} | |
flags[k] = v | |
} else if strings.HasPrefix(arg, "-") { | |
for _, v := range arg[1:] { | |
flags[string(v)] = true | |
} | |
} else { | |
args = append(args, arg) | |
} | |
} | |
return args, flags | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment