diff options
| author | Jeff Carr <[email protected]> | 2025-10-13 07:39:20 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-13 07:39:20 -0500 |
| commit | 915ea4f648f8639cb1fc4111fa49ff298ea79441 (patch) | |
| tree | 2b1e84efaa03f9813492fee7bad248c9981ca0ae | |
| parent | d4d06bfb10b4d73d96c80770309124a6d3901e0c (diff) | |
cleanup and annotate parse os.Args()
| -rw-r--r-- | parseArgv.go | 45 |
1 files changed, 36 insertions, 9 deletions
diff --git a/parseArgv.go b/parseArgv.go index a1cd20e..d16c745 100644 --- a/parseArgv.go +++ b/parseArgv.go @@ -13,22 +13,43 @@ import ( func parseArgv(argname string) *Auto { pb := new(Auto) pb.Argname = argname + + // this shouldn't really happen. OS/POSIX error? if len(os.Args) == 0 { + // fmt.Fprintf(os.Stderr, "what OS is this?\n") return pb } + // there is nothing on the command line. user just ran "forge" and hit enter + if len(os.Args) == 1 { + pb.Arg0 = os.Args[0] + return pb + } + + // try to setup bash autocomplete and exit if len(os.Args) > 1 && os.Args[1] == "--bash" { pb.SetupAuto = true return pb } - // HACK: set debug flag if --autodebug is passed + // try to setup zsh autocomplete and exit + if len(os.Args) > 1 && os.Args[1] == "--zsh" { + pb.SetupAuto = true + return pb + } + + // set debug flag if --argvdebug is passed for _, s := range os.Args { + if s == "--argvdebug" { + pb.Debug = true + } + // deprecate if s == "--autodebug" { pb.Debug = true } } + // wtf is this. I've forgotten. todo: figure this out if len(os.Args) > 1 && os.Args[1] == pb.Argname { pb.IsAuto = true parts := strings.Split(os.Getenv("COMP_LINE"), " ") @@ -43,6 +64,7 @@ func parseArgv(argname string) *Auto { os.Exit(0) } + // print the version and exit if len(os.Args) > 1 && os.Args[1] == "--version" { if myAuto.buildtime != nil { // if binary defined buildtime() then process standard version output here @@ -50,16 +72,21 @@ func parseArgv(argname string) *Auto { os.Exit(0) } } - if len(os.Args) == 0 { - return pb - } - if len(os.Args) == 1 { - pb.Arg0 = os.Args[0] - return pb - } if os.Args[1] != "--auto-complete" { - pb.Cmd = os.Args[1] + // if the first arg is not --auto-complete, then don't go any farther + for _, s := range os.Args[1:] { + if strings.HasPrefix(s, "-") { + // option is something like --verbose + // skip these. they are not subcommands + continue + } + // found the subcommand + pb.Cmd = s + break + } + // exit here. not autocomplete + // todo: actually finish parsing the pb. is it safe to continue from here? return pb } |
