diff options
Diffstat (limited to 'parseOsArgs.go')
| -rw-r--r-- | parseOsArgs.go | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/parseOsArgs.go b/parseOsArgs.go new file mode 100644 index 0000000..c91c1e5 --- /dev/null +++ b/parseOsArgs.go @@ -0,0 +1,155 @@ +package argvpb + +// initializes logging and command line options + +import ( + "fmt" + "os" + "strings" + + "go.wit.com/log" +) + +func parseArgv(argname string) *Argv { + pb := new(Argv) + 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 + } + + // 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"), " ") + pb.Debug = true + log.Fprintf(os.Stderr, "\n") + pb.Debugf("MATCH Partial os.Args=%v COMP_LINE=%v", os.Args, os.Getenv("COMP_LINE")) + // log.Fprintf(os.Stdout, "jcarr") + if len(parts) > 0 { + pb.Arg0 = parts[0] + } + pb.Arg1 = os.Args[1] + os.Exit(0) + } + + // print the version and exit + if len(os.Args) > 1 && os.Args[1] == "--version" { + if me.buildtime != nil { + // if binary defined buildtime() then process standard version output here + doVersion(pb) + os.Exit(0) + } + } + + if os.Args[1] != "--auto-complete" { + // 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 + } + + // should we do auto complete here? + if len(os.Args) > 1 && os.Args[1] == "--auto-complete" { + pb.IsAuto = true + pb.Arg0 = os.Args[0] + pb.Arg1 = os.Args[1] + pb.Partial = os.Args[2] + pb.Arg3 = os.Args[3] + if len(os.Args) < 5 { + // the user is doing autocomplete on the command itself + pb.Partial = "" + pb.Cmd = "" + pb.Real = []string{""} + return pb + } + if pb.Partial == "''" { + pb.Partial = "" + } + // pb.Argv = os.Args[4:] + for _, s := range os.Args[4:] { + if s == "--autodebug" { + continue + } + tmp := strings.Trim(pb.Partial, "'") + if tmp == s { + // don't put pb.Partial into Argv + continue + } + pb.Real = append(pb.Real, s) + } + // set pb.Cmd to the first thing that doesn't have a '-' arg + for _, s := range pb.Real { + if strings.HasPrefix(s, "-") { + continue + } + pb.Cmd = s + break + } + if pb.Partial == "'"+pb.Cmd+"'" { + // not really a command, it's just a partially inputed string from the user + pb.Cmd = "" + } + // try to figure out what the last argv is + for _, s := range pb.Real { + p := fmt.Sprintf("'%s'", s) + if pb.Partial == p { + pb.Debugf("DEBUG: Last argv MATCHES Partial %s %s", s, p) + continue + } else { + pb.Debugf("DEBUG: Last argv DOES NOT MATCH Partial %s %s", s, p) + } + pb.Last = s + if strings.HasPrefix(s, "-") { + // skip args like -test --verbose when sending subcommands to go-args for help text + continue + } + pb.Goargs = append(pb.Goargs, s) + } + // if pb.Cmd == "" { + // pb.Cmd = strings.Join(pb.Argv, "BLAH") + // } + } + return pb +} |
