Skip to content

Commit 51c5169

Browse files
committed
[v1.0.0] Env, EnvAll 추가 및 테스크 코드 제거
1 parent 75f595f commit 51c5169

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 LEE DONG GUN
3+
Copyright (c) 2021 - 2022 LEE DONG GUN
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,22 @@
11
# go-args-parser
2-
go args 파서 입니다.
2+
3+
## Getting started
4+
5+
### download
6+
7+
```bash
8+
go get -v github.com/Sotaneum/go-args-parser
9+
```
10+
11+
### API
12+
13+
| name | params | returns | example | - |
14+
| ----------- | ------------------- | ------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------- |
15+
| Args | `map[string]string` | `map[string]string` | parser.Args(map[string]string{}) | Argument로 넘어오는 값을 `map[string]string` 포맷으로 반환합니다. |
16+
| EnvAll | `map[string]string` | `map[string]string` | parser.EnvAll(map[string]string{}) | 모든 환경 변수 값을 `map[string]string` 포맷으로 반환합니다. |
17+
| Env | `map[string]string` | `map[string]string` | parser.Env(map[string]string{}) | 환경 변수 값 중 `params`에 해당하는 key만 추출하여 `map[string]string` 포맷으로 반환합니다. |
18+
| ArgsJoinEnv | `map[string]string` | `map[string]string` | parser.ArgsJoinEnv(map[string]string{}) | `params`에 해당 하는 key만 추출하여 env로 부터 값을 가져오고 그 위에 Args를 덮어쓰기합니다. |
19+
20+
## LICENSE
21+
22+
[MIT](./LICENSE)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/Sotaneum/go-args-parser
22

3-
go 1.17
3+
go 1.19

parser.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ func toKeyValue(data, pattern string) (string, string) {
1818

1919
func Args(options map[string]string) map[string]string {
2020
argsWithoutProg := os.Args[1:]
21-
2221
for _, arg := range argsWithoutProg {
23-
println(arg)
2422
key, value := toKeyValue(arg, "=")
2523
if key == "" || value == "" {
2624
continue
@@ -29,3 +27,30 @@ func Args(options map[string]string) map[string]string {
2927
}
3028
return options
3129
}
30+
31+
func Env(options map[string]string) map[string]string{
32+
for key, _ := range options {
33+
value := os.Getenv(key)
34+
if value == ""{
35+
continue
36+
}
37+
options[key] = value
38+
}
39+
return options
40+
}
41+
42+
func EnvAll(options map[string]string) map[string]string{
43+
for _, item := range os.Environ() {
44+
pair := strings.Split(item, "=")
45+
options[pair[0]] = pair[1]
46+
}
47+
return options
48+
}
49+
50+
func ArgsJoinEnv(options map[string]string) map[string]string{
51+
return Args(Env(options))
52+
}
53+
54+
func ArgsJoinEnvAll(options map[string]string) map[string]string{
55+
return Args(EnvAll(options))
56+
}

parser_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ import (
99

1010
func TestRunner(t *testing.T) {
1111
fmt.Println(parser.Args(map[string]string{}))
12+
fmt.Println(parser.EnvAll(map[string]string{}))
13+
fmt.Println(parser.Env((map[string]string{"TMPDIR":"test"})))
14+
fmt.Println(parser.ArgsJoinEnv((map[string]string{"TMPDIR":"test"})))
1215
}
16+

0 commit comments

Comments
 (0)