Skip to content

Commit

Permalink
[v1.0.0] Env, EnvAll 추가 및 테스크 코드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotaneum committed Oct 3, 2022
1 parent 75f595f commit 51c5169
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/Sotaneum/go-args-parser

go 1.17
go 1.19
29 changes: 27 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
4 changes: 4 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})))
}

0 comments on commit 51c5169

Please sign in to comment.