diff --git a/LICENSE b/LICENSE index 943b5f7..edc8984 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 LEE DONG GUN +Copyright (c) 2021 - 2022 LEE DONG GUN Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 9aa9e44..05556c4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # go-args-parser -go args 파서 입니다. + +## Getting started + +### download + +```bash +go get -v github.com/Sotaneum/go-args-parser +``` + +### API + +| name | params | returns | example | - | +| ----------- | ------------------- | ------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------- | +| Args | `map[string]string` | `map[string]string` | parser.Args(map[string]string{}) | Argument로 넘어오는 값을 `map[string]string` 포맷으로 반환합니다. | +| EnvAll | `map[string]string` | `map[string]string` | parser.EnvAll(map[string]string{}) | 모든 환경 변수 값을 `map[string]string` 포맷으로 반환합니다. | +| Env | `map[string]string` | `map[string]string` | parser.Env(map[string]string{}) | 환경 변수 값 중 `params`에 해당하는 key만 추출하여 `map[string]string` 포맷으로 반환합니다. | +| ArgsJoinEnv | `map[string]string` | `map[string]string` | parser.ArgsJoinEnv(map[string]string{}) | `params`에 해당 하는 key만 추출하여 env로 부터 값을 가져오고 그 위에 Args를 덮어쓰기합니다. | + +## LICENSE + +[MIT](./LICENSE) diff --git a/go.mod b/go.mod index e80659b..795176f 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/Sotaneum/go-args-parser -go 1.17 +go 1.19 diff --git a/parser.go b/parser.go index 5a92539..d9b37c7 100644 --- a/parser.go +++ b/parser.go @@ -18,9 +18,7 @@ func toKeyValue(data, pattern string) (string, string) { func Args(options map[string]string) map[string]string { argsWithoutProg := os.Args[1:] - for _, arg := range argsWithoutProg { - println(arg) key, value := toKeyValue(arg, "=") if key == "" || value == "" { continue @@ -29,3 +27,30 @@ func Args(options map[string]string) map[string]string { } return options } + +func Env(options map[string]string) map[string]string{ + for key, _ := range options { + value := os.Getenv(key) + if value == ""{ + continue + } + options[key] = value + } + return options +} + +func EnvAll(options map[string]string) map[string]string{ + for _, item := range os.Environ() { + pair := strings.Split(item, "=") + options[pair[0]] = pair[1] + } + return options +} + +func ArgsJoinEnv(options map[string]string) map[string]string{ + return Args(Env(options)) +} + +func ArgsJoinEnvAll(options map[string]string) map[string]string{ + return Args(EnvAll(options)) +} \ No newline at end of file diff --git a/parser_test.go b/parser_test.go index 7f084f7..2ff30f4 100644 --- a/parser_test.go +++ b/parser_test.go @@ -9,4 +9,8 @@ import ( func TestRunner(t *testing.T) { fmt.Println(parser.Args(map[string]string{})) + fmt.Println(parser.EnvAll(map[string]string{})) + fmt.Println(parser.Env((map[string]string{"TMPDIR":"test"}))) + fmt.Println(parser.ArgsJoinEnv((map[string]string{"TMPDIR":"test"}))) } +