From 8125b8e2d070c33c94717db0ba0761a7d6299112 Mon Sep 17 00:00:00 2001 From: kevinnls Date: Tue, 16 Jan 2024 19:41:27 +0530 Subject: [PATCH] basic arg parsing --- main.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 main.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..c44f37e --- /dev/null +++ b/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "flag" + "os" +) + +var flags *flag.FlagSet +var stage_scope bool +var commit_directly bool +var commit_type string +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) + } + + 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: + commit: %t + stage: %t + type: %s + scope: %s + message: %s +`, commit_directly, stage_scope, commit_type, commit_scope, commit_message) +}