summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-21 15:48:24 -0500
committerJeff Carr <[email protected]>2025-10-21 15:48:24 -0500
commit2b6414a664fd7ccc519874f4d0b55a12a871307c (patch)
treeb72eba5a53e98a219f4a54249241e2fd9b66e6d8
parente9c3000abafd70702cb9db6e3738115012f498eb (diff)
housecleaning
-rw-r--r--argv.Match.go21
-rw-r--r--argv.Version.go14
-rw-r--r--argv.parseOsArgs.go115
-rw-r--r--exit.go (renamed from argv.Exit.go)0
-rw-r--r--init.go36
-rw-r--r--theMagicOfAutocomplete.go74
6 files changed, 141 insertions, 119 deletions
diff --git a/argv.Match.go b/argv.Match.go
new file mode 100644
index 0000000..e9670a7
--- /dev/null
+++ b/argv.Match.go
@@ -0,0 +1,21 @@
+package argvpb
+
+import "strings"
+
+func (pb *Argv) IsMatch(match string) bool {
+ parts := strings.Split(match, ".")
+ pb.Debugf("IsMatch() parts (%v)", parts)
+ for _, part := range parts {
+ var found bool
+ for _, v := range pb.Real {
+ if part == v {
+ found = true
+ }
+ }
+ if found {
+ continue
+ }
+ return false
+ }
+ return true
+}
diff --git a/argv.Version.go b/argv.Version.go
index c50c52a..4a133c2 100644
--- a/argv.Version.go
+++ b/argv.Version.go
@@ -1,7 +1,5 @@
package argvpb
-// initializes logging and command line options
-
import (
"fmt"
"os"
@@ -10,11 +8,6 @@ import (
"go.wit.com/log"
)
-// returns the name of the executable registered for shell autocomplete
-func GetAPPNAME() string {
- return me.pb.AppInfo.APPNAME
-}
-
func (pb *Argv) Version() string {
return pb.AppInfo.getVersion()
}
@@ -39,8 +32,7 @@ func (info *App) getVersion() string {
return s
}
-/*
-func StandardVersion(APPNAME, VERSION, BUILDTIME string) string {
- return fmt.Sprintf("%s %s Built on raw(%v) %s (from argv)", APPNAME, VERSION, BUILDTIME, cobol.Time(BUILDTIME))
+// deprecate ?
+func GetAPPNAME() string {
+ return me.pb.AppInfo.APPNAME
}
-*/
diff --git a/argv.parseOsArgs.go b/argv.parseOsArgs.go
index eb6f7d6..8623138 100644
--- a/argv.parseOsArgs.go
+++ b/argv.parseOsArgs.go
@@ -85,66 +85,71 @@ func (pb *Argv) parseArgv() {
return
}
- // 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
+ // the shell is trying to get autocomplete information
+
+ // initial PB setup
+ 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
+ // no subcommand. user has done "forge <TAB><TAB>"
+ pb.Partial = ""
+ pb.Cmd = ""
+ pb.Real = []string{""}
+ return
+ }
+
+ // figure out if there is a subcommand or a partial match
+ if pb.Partial == "''" {
+ pb.Partial = ""
+ }
+
+ // pb.Argv = os.Args[4:]
+ for _, s := range os.Args[4:] {
+ if s == "--autodebug" {
+ continue
}
- if pb.Partial == "''" {
- pb.Partial = ""
+ tmp := strings.Trim(pb.Partial, "'")
+ if tmp == s {
+ // don't put pb.Partial into Argv
+ continue
}
- // 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
+ 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
}
- if pb.Partial == "'"+pb.Cmd+"'" {
- // not really a command, it's just a partially inputed string from the user
- pb.Cmd = ""
+ 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)
}
- // 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)
+ pb.Last = s
+ if strings.HasPrefix(s, "-") {
+ // skip args like -test --verbose when sending subcommands to go-args for help text
+ continue
}
- // if pb.Cmd == "" {
- // pb.Cmd = strings.Join(pb.Argv, "BLAH")
- // }
+ pb.Goargs = append(pb.Goargs, s)
}
+ // if pb.Cmd == "" {
+ // pb.Cmd = strings.Join(pb.Argv, "BLAH")
+ // }
return
}
diff --git a/argv.Exit.go b/exit.go
index c2f33cb..c2f33cb 100644
--- a/argv.Exit.go
+++ b/exit.go
diff --git a/init.go b/init.go
new file mode 100644
index 0000000..9feed7f
--- /dev/null
+++ b/init.go
@@ -0,0 +1,36 @@
+package argvpb
+
+import (
+ "time"
+
+ "go.wit.com/lib/cobol"
+ "go.wit.com/log"
+)
+
+// gets APPNAME, BUILDTIME and VERSION from the application
+func initAppname() {
+ app := new(App)
+ APPNAME, anyString, VERSION := me.initArgvFunc()
+ app.APPNAME = APPNAME
+ app.VERSION = VERSION
+ //
+ // this logic isn't great, but it's what it is right now
+ //
+ // the reason this logic is messy is because cobol is supposed to "guess"
+ // that is the point of the cobol package and I've been using this code here
+ // to test the guesses because it's an easy place to test that code
+ //
+ if BUILDTIME, err := cobol.GetTime(anyString); BUILDTIME != nil {
+ // everyhting is working. BUILDTIME is *time.Time
+ app.BUILDTIME = cobol.Time(BUILDTIME)
+ } else if err == nil {
+ newtime := BUILDTIME.Add(-36 * time.Hour)
+ app.BUILDTIME = cobol.Time(BUILDTIME)
+ log.Printf("TIME initAppname() ERR=(%v) anyString=(%v) GetTime.BUILTIME=(%v) app.BUILDTIME=(%v)\n", err, anyString, newtime, app.BUILDTIME)
+ } else {
+ app.BUILDTIME = anyString
+ log.Printf("TIME initAppname() ERR=(%v) anyString=(%v) GetTime.BUILTIME=(%v) app.BUILDTIME=(%v)\n", err, anyString, BUILDTIME, app.BUILDTIME)
+ }
+ me.pb.AppInfo = app
+ me.pb.Argname = APPNAME // deprecate this
+}
diff --git a/theMagicOfAutocomplete.go b/theMagicOfAutocomplete.go
index e8e4594..9155517 100644
--- a/theMagicOfAutocomplete.go
+++ b/theMagicOfAutocomplete.go
@@ -17,38 +17,14 @@ import (
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
-// gets APPNAME, BUILDTIME and VERSION from the application
-func initAppname() {
- app := new(App)
- APPNAME, anyString, VERSION := me.initArgvFunc()
- app.APPNAME = APPNAME
- app.VERSION = VERSION
- //
- // this logic isn't great, but it's what it is right now
- //
- // the reason this logic is messy is because cobol is supposed to "guess"
- // that is the point of the cobol package and I've been using this code here
- // to test the guesses because it's an easy place to test that code
- //
- if BUILDTIME, err := cobol.GetTime(anyString); BUILDTIME != nil {
- // everyhting is working. BUILDTIME is *time.Time
- app.BUILDTIME = cobol.Time(BUILDTIME)
- } else if err == nil {
- newtime := BUILDTIME.Add(-36 * time.Hour)
- app.BUILDTIME = cobol.Time(BUILDTIME)
- log.Printf("TIME initAppname() ERR=(%v) anyString=(%v) GetTime.BUILTIME=(%v) app.BUILDTIME=(%v)\n", err, anyString, newtime, app.BUILDTIME)
- } else {
- app.BUILDTIME = anyString
- log.Printf("TIME initAppname() ERR=(%v) anyString=(%v) GetTime.BUILTIME=(%v) app.BUILDTIME=(%v)\n", err, anyString, BUILDTIME, app.BUILDTIME)
- }
- me.pb.AppInfo = app
- me.pb.Argname = APPNAME // deprecate this
-}
-
func Autocomplete(dest any) *Argv {
me = new(AutoArgs) // todo: redo this
me.pb = new(Argv)
+ // set the start time of the binary
+ now := time.Now()
+ me.pb.Ctime = timestamppb.New(now)
+
// makes sure the application has the
// needed functions defined, otherwise panics
verifyApplication(dest)
@@ -56,25 +32,14 @@ func Autocomplete(dest any) *Argv {
// gets APPNAME, BUILDTIME and VERSION from the application
initAppname()
- // parses os.Args into a protobuf
+ // parses os.Args into the protobuf
me.pb.parseArgv()
- // todo: figure this out
- if me.guiFunc != nil {
- // register gui args
- me.guiFunc()
- // log.Info("gui init")
- } else {
- // log.Info("no gui init")
- }
-
- // the argv history
- all := NewArgvs()
-
- // initializes the application config file
+ // initializes the lib/ENV library
ENV.Init(me.pb.AppInfo.APPNAME, me.pb.AppInfo.VERSION, cobol.Time(me.pb.AppInfo.BUILDTIME), me.pb.Real, GoodExit, BadExit)
- // loads the autocomplete history file
+ // loads the argv autocomplete history file
+ all := NewArgvs()
err := config.LoadCache(all, "argv", me.pb.AppInfo.APPNAME) //
if err != nil {
// there is no history.
@@ -82,12 +47,16 @@ func Autocomplete(dest any) *Argv {
// todo: check if this is automatically done already
}
- // set the start time of the binary
- now := time.Now()
- me.pb.Ctime = timestamppb.New(now)
-
// try to register bash args for go-args
// arg.Register(&ArgvBash)
+ // todo: figure this out
+ if me.guiFunc != nil {
+ // register gui args
+ me.guiFunc()
+ // log.Info("gui init")
+ } else {
+ // log.Info("no gui init")
+ }
// user is trying to setup bash or zsh autocomplete
// --bash or --zsh is the first os.Args
@@ -163,6 +132,11 @@ func Autocomplete(dest any) *Argv {
flags = append(flags, s)
}
+ if strings.HasPrefix(me.pb.Last, "--argv") {
+ me.pb.SendString("--argvdebug --argvhelp")
+ os.Exit(0)
+ }
+
// use go-args to parse the structs so we can use them here
// me.pp, err = arg.ParseFlags(flags, dest)
if me.parseFlagsFunc == nil {
@@ -183,12 +157,6 @@ func Autocomplete(dest any) *Argv {
}
}
- // if me.pp == nil {
- // me.pb.Debugf("DEBUG: me.pp == nil after ParseFlags()")
- // } else {
- // me.pb.Debugf("DEBUG: me.pp is ok after ParseFlags()")
- // }
-
// save now. this is near the end probably
all.Clone(me.pb)
errors.Join(err, all.Save())