Compare commits
2 Commits
69d2eb70a1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
b044825d1a
|
|||
|
d856a3bcd7
|
@@ -1,10 +1,14 @@
|
||||
package argparser
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Parse(args []string) (err error, commit_type string, commit_scope string, shouldCommit bool, shouldStage bool, commit_message []string) {
|
||||
argparseloop:
|
||||
for i := 0; i < len(args); i++ {
|
||||
if strings.HasPrefix(args[i],"-") {
|
||||
switch args[i] {
|
||||
case "-c", "--commit", "-commit":
|
||||
shouldCommit = true
|
||||
@@ -14,18 +18,22 @@ func Parse(args []string) (err error, commit_type string, commit_scope string, s
|
||||
commit_message = args[i+1:]
|
||||
break argparseloop
|
||||
default:
|
||||
if commit_type == "" {
|
||||
commit_type = args[i]
|
||||
continue argparseloop
|
||||
}
|
||||
if commit_scope == "" {
|
||||
commit_scope = args[i]
|
||||
continue argparseloop
|
||||
}
|
||||
err = errors.New("unknown argument")
|
||||
return
|
||||
}
|
||||
}
|
||||
if commit_type == "" {
|
||||
commit_type = args[i]
|
||||
} else if commit_scope == "" {
|
||||
commit_scope = args[i]
|
||||
} else {
|
||||
err = errors.New("unknown argument")
|
||||
return
|
||||
}
|
||||
}
|
||||
if commit_type == "" {
|
||||
err = errors.New("need at least type")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -26,5 +26,13 @@ func TestParse(t *testing.T) {
|
||||
assert.Equal(t, shouldCommit, false)
|
||||
assert.Equal(t, shouldStage, false)
|
||||
})
|
||||
t.Run("fake arg", func(t *testing.T) {
|
||||
error,_,_,_,_,_ := Parse([]string{"-f"})
|
||||
assert.NotEqual(t, error, nil)
|
||||
})
|
||||
t.Run("excess arg", func(t *testing.T) {
|
||||
error,_,_,_,_,_ := Parse([]string{"type","scope","fool me"})
|
||||
assert.NotEqual(t, error, nil)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user