refactor arg parsing into own package; don't use flags
This commit is contained in:
31
internal/argparser/argparser.go
Normal file
31
internal/argparser/argparser.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package argparser
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
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++ {
|
||||||
|
switch args[i] {
|
||||||
|
case "-c", "--commit", "-commit":
|
||||||
|
shouldCommit = true
|
||||||
|
case "-a", "--add", "-add":
|
||||||
|
shouldStage = true
|
||||||
|
case "--":
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
56
main.go
56
main.go
@@ -2,65 +2,21 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"flag"
|
|
||||||
"os"
|
"os"
|
||||||
|
"qc/internal/argparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
var flags *flag.FlagSet
|
func main() {
|
||||||
var stage_scope bool
|
err, commit_type, commit_scope, shouldCommit, shouldStage, commit_message := argparser.Parse(os.Args[1:])
|
||||||
var commit_directly bool
|
if err != nil {
|
||||||
var commit_type string
|
fmt.Fprintf(os.Stderr, "%s", err)
|
||||||
var commit_scope string
|
|
||||||
var commit_message []string
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
flags = flag.NewFlagSet("flags", flag.ExitOnError)
|
|
||||||
flags.BoolVar(&commit_directly, "c", false, "Commit directly without opening editor")
|
|
||||||
flags.BoolVar(&commit_directly, "commit", false, "Commit directly without opening editor")
|
|
||||||
flags.BoolVar(&stage_scope, "a", false, "Stage (git add) the directory of scope")
|
|
||||||
flags.BoolVar(&stage_scope, "add", false, "Stage (git add) the directory of scope")
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseArgs() {
|
|
||||||
if len(os.Args) < 2 {
|
|
||||||
fmt.Fprintf(os.Stderr, "ERROR: need at least `type`\n")
|
|
||||||
os.Exit(5)
|
os.Exit(5)
|
||||||
}
|
}
|
||||||
|
|
||||||
argparseloop:
|
|
||||||
for i := 1; i < len(os.Args); i++ {
|
|
||||||
fmt.Println(os.Args[i],"@", i)
|
|
||||||
switch os.Args[i] {
|
|
||||||
case "-c", "--commit", "-commit":
|
|
||||||
flags.Parse([]string{os.Args[i]})
|
|
||||||
case "-a", "--add", "-add":
|
|
||||||
flags.Parse([]string{os.Args[i]})
|
|
||||||
case "--":
|
|
||||||
commit_message = os.Args[i+1:]
|
|
||||||
break argparseloop
|
|
||||||
default:
|
|
||||||
if commit_type == "" {
|
|
||||||
commit_type = os.Args[i]
|
|
||||||
continue argparseloop
|
|
||||||
}
|
|
||||||
if commit_scope == "" {
|
|
||||||
commit_scope = os.Args[i]
|
|
||||||
continue argparseloop
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "unsure what do with additional args %q\n",os.Args[i])
|
|
||||||
os.Exit(5)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Println("hello world");
|
|
||||||
parseArgs()
|
|
||||||
fmt.Printf(`these are your args:
|
fmt.Printf(`these are your args:
|
||||||
commit: %t
|
commit: %t
|
||||||
stage: %t
|
stage: %t
|
||||||
type: %s
|
type: %s
|
||||||
scope: %s
|
scope: %s
|
||||||
message: %s
|
message: %s
|
||||||
`, commit_directly, stage_scope, commit_type, commit_scope, commit_message)
|
`, shouldCommit, shouldStage, commit_type, commit_scope, commit_message)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user