diff options
| author | Jeff Carr <[email protected]> | 2025-10-25 17:42:35 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-25 17:42:35 -0500 |
| commit | 29c03d2c0c88486c2299492b53e4d6cf9999efa4 (patch) | |
| tree | fb4a375025b9e3b47166fd528605fa54cc4fbbd5 | |
| parent | ab82f99c94d1c0b0d386ca691902183b409e3d38 (diff) | |
debugging argv
| -rw-r--r-- | argv.Print.go | 15 | ||||
| -rw-r--r-- | argv.parseOsArgs.go | 10 | ||||
| -rw-r--r-- | argv.proto | 2 | ||||
| -rw-r--r-- | fakeStdout.go | 79 | ||||
| -rw-r--r-- | theMagicOfAutocomplete.go | 15 |
5 files changed, 107 insertions, 14 deletions
diff --git a/argv.Print.go b/argv.Print.go index ed6f75e..0d029b4 100644 --- a/argv.Print.go +++ b/argv.Print.go @@ -19,7 +19,8 @@ func (pb *Argv) Debugf(fmts string, parts ...any) { func Debugf(fmts string, parts ...any) { fmts = strings.TrimSpace(fmts) fmts += "\n" - me.pb.Stderr += fmt.Sprintf(fmts, parts...) + // me.pb.Stderr += fmt.Sprintf(fmts, parts...) + fmt.Fprintf(Stderr, fmts, parts...) } // print out auto complete debugging info @@ -59,14 +60,18 @@ func (all *Argvs) PrintHistory(last string) { // hist := fmt.Sprintf("HIST(%d)", counter) hist := fmt.Sprintf("HIST") pb.PrintDebugNew(hist, last) + if counter > 100 { + // sometimes I'm dumb + break + } } } -func (pb *Argv) PrintStderr() { +func PrintStderr() { if !me.debug { return } - lines := strings.Split(strings.TrimSpace(pb.Stderr), "\n") + lines := strings.Split(strings.TrimSpace(PB.Stderr), "\n") if len(lines) == 0 { return } @@ -80,6 +85,6 @@ func (pb *Argv) PrintStderr() { } } -func (pb *Argv) PrintStdout() { - fmt.Fprintf(os.Stdout, "%s\n", pb.Stdout) +func PrintStdout() { + fmt.Fprintf(os.Stdout, "%s\n", PB.Stdout) } diff --git a/argv.parseOsArgs.go b/argv.parseOsArgs.go index b7d4774..5df4a8d 100644 --- a/argv.parseOsArgs.go +++ b/argv.parseOsArgs.go @@ -12,10 +12,18 @@ func GetCmd() string { return PB.GetCmd() } +func mypanic(blah string) { + me.debug = true + doStdoutStderr() + panic(blah) +} + // todo: this is wrong func (pb *Argv) GetCmd() string { var curcmd string - // for _, s := range os.Args[1:] { + if pb == nil { + return "pb=<nil>" + } for _, s := range pb.Real { if strings.HasPrefix(s, "-") { // option is something like --verbose @@ -43,7 +43,7 @@ message Argv { // `autogenpb:marshal message Argvs { // `autogenpb:marshal` `autogenpb:sort` `autogenpb:nomutex` string uuid = 1; // `autogenpb:uuid:1e6e765c-0c77-4c81-a622-0d819bfcce9a` - string version = 2; // `autogenpb:version:v0.0.4` + string version = 2; // `autogenpb:version:v0.0.5` repeated Argv argvs = 3; string filename = 4; // `autogenpb:save` -- this enables autogenerated pb.Load() and pb.Save() } diff --git a/fakeStdout.go b/fakeStdout.go new file mode 100644 index 0000000..b263f8a --- /dev/null +++ b/fakeStdout.go @@ -0,0 +1,79 @@ +package argvpb + +// StringWriter is a custom type that implements the io.Writer interface +// for a string variable. It holds a pointer to the string it will write to. +type StringWriter struct { + s *string +} + +// NewStringWriter creates a new StringWriter that writes to the provided string pointer. +func NewStringWriter(s *string) *StringWriter { + return &StringWriter{s: s} +} + +// Write implements the io.Writer interface. It appends the byte slice `p` +// to the string pointed to by the StringWriter. +func (sw *StringWriter) Write(p []byte) (n int, err error) { + // Append the contents of the byte slice p to the string *sw.s + *sw.s += string(p) + return len(p), nil +} + +var Stdout *StringWriter +var Stderr *StringWriter + +func fakeStdout() { + Stdout = NewStringWriter(&PB.Stdout) + Stderr = NewStringWriter(&PB.Stderr) +} + +/* +func main() { + // 1. Declare the string variable that will be our output destination. + var myout string + + // 2. Create a new writer that points to our string variable. + // Our StringWriter now satisfies the io.Writer interface. + writer := NewStringWriter(&myout) + + // 3. Use our writer with functions that accept an io.Writer. + fmt.Fprintln(writer, "Hello, world!") + io.WriteString(writer, "This is the second line.\n") + fmt.Fprintf(writer, "A number: %d\n", 123) + + // 4. Print the final contents of the string to verify. + fmt.Println("--- Final content of myout: ---") + fmt.Print(myout) + fmt.Println("-----------------------------") +} +*/ +/* +package main + +import ( + "fmt" + "io" + "strings" +) + +func main() { + // 1. Declare the final string variable. + var myout string + + // 2. Create a strings.Builder. It already implements io.Writer. + var builder strings.Builder + + // 3. Use the builder as the io.Writer. + fmt.Fprintln(&builder, "Hello from the builder!") + io.WriteString(&builder, "This is the second line.\n") + fmt.Fprintf(&builder, "Another number: %d\n", 456) + + // 4. Get the final string from the builder and assign it. + myout = builder.String() + + // 5. Print the final contents of the string to verify. + fmt.Println("--- Final content of myout: ---") + fmt.Print(myout) + fmt.Println("-----------------------------") +} +*/ diff --git a/theMagicOfAutocomplete.go b/theMagicOfAutocomplete.go index d72b46f..0e8a7f3 100644 --- a/theMagicOfAutocomplete.go +++ b/theMagicOfAutocomplete.go @@ -26,6 +26,7 @@ func Autocomplete(dest any) *Argv { me = new(AutoArgs) // todo: redo this me.pb = new(Argv) PB = me.pb + fakeStdout() me.pb.Uuid = uuid.New().String() // set the start time of the binary @@ -192,16 +193,16 @@ func doStdoutStderr() { savePB() if me.debug { me.all.PrintHistory(me.last.GetCmd()) - me.pb.Stderr += fmt.Sprintf("pb.Stdout (%v)\n", PB.Stdout) - me.pb.PrintStderr() + fmt.Fprintf(Stderr, "debug=true pb.Stdout (%v)\n", PB.Stdout) } else { if me.pb.Fast { if me.pb.HelpCounter < 3 { - me.pb.PrintStderr() + fmt.Fprintf(Stderr, "help counter < 3\n") } } } - me.pb.PrintStdout() + PrintStderr() + PrintStdout() } func saveAndExit() { @@ -224,14 +225,14 @@ func examineArgvHistory() { // so this panic() is safe and can never be triggered by normal program execution. // me.debug = true - me.pb.PrintStderr() me.pb.Stderr += fmt.Sprintf("config.CreateCacheDirPB() err(%v)\n", me.Err) + doStdoutStderr() panic("argvpb.Load() history file failed") } if me.debug { // use this if you are having trouble debugging this code // me.all.PrintHistory("EARLY") - // me.pb.PrintStderr() + // doStdoutStderr() } // roll the autocomplete file maxsize := 17 @@ -276,8 +277,8 @@ func examineArgvHistory() { // only have nil values in the .pb file. just die for now if me.all.Len() == 0 { me.debug = true - me.pb.PrintStderr() me.pb.Stderr += fmt.Sprintf("examineArgvHistory() couldn't find a valid last entry") + doStdoutStderr() // todo: make a blank entry here panic("examineArgvHistory() couldn't find a valid last entry") } |
