parse my args properly!

This commit is contained in:
2024-03-06 16:14:33 +05:30
parent d856a3bcd7
commit b044825d1a

View File

@@ -1,30 +1,38 @@
package argparser 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) { func Parse(args []string) (err error, commit_type string, commit_scope string, shouldCommit bool, shouldStage bool, commit_message []string) {
argparseloop: argparseloop:
for i := 0; i < len(args); i++ { for i := 0; i < len(args); i++ {
switch args[i] { if strings.HasPrefix(args[i],"-") {
case "-c", "--commit", "-commit": switch args[i] {
shouldCommit = true case "-c", "--commit", "-commit":
case "-a", "--add", "-add": shouldCommit = true
shouldStage = true case "-a", "--add", "-add":
case "--": shouldStage = true
commit_message = args[i+1:] case "--":
break argparseloop commit_message = args[i+1:]
default: break argparseloop
if commit_type == "" { default:
commit_type = args[i] err = errors.New("unknown argument")
continue argparseloop return
} }
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 return
} }