Skip to content

Commit 75f595f

Browse files
committed
[beta] agrs 추가
1 parent d5c8749 commit 75f595f

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/Sotaneum/go-args-parser
2+
3+
go 1.17

parser.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package parser
2+
3+
import (
4+
"os"
5+
"strings"
6+
)
7+
8+
func toKeyValue(data, pattern string) (string, string) {
9+
items := strings.Split(data, pattern)
10+
if len(items) != 2 {
11+
if pattern == ":" {
12+
return data, "true"
13+
}
14+
return toKeyValue(data, ":")
15+
}
16+
return items[0], items[1]
17+
}
18+
19+
func Args(options map[string]string) map[string]string {
20+
argsWithoutProg := os.Args[1:]
21+
22+
for _, arg := range argsWithoutProg {
23+
println(arg)
24+
key, value := toKeyValue(arg, "=")
25+
if key == "" || value == "" {
26+
continue
27+
}
28+
options[key] = value
29+
}
30+
return options
31+
}

parser_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package parser_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
parser "github.com/Sotaneum/go-args-parser"
8+
)
9+
10+
func TestRunner(t *testing.T) {
11+
fmt.Println(parser.Args(map[string]string{}))
12+
}

0 commit comments

Comments
 (0)