summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-09-25 14:05:33 -0500
committerJeff Carr <[email protected]>2025-09-25 14:05:33 -0500
commit0f8eaec720a5a98d84242b431537dcc4c190c974 (patch)
treedf6f57ba32f7d0e137aa55aa339f3264b07a9409
parent69943556c09fe6b178afb33a8a6346adb3bd50e7 (diff)
first stab and go-arg Examples()
-rw-r--r--bash.orig.go1
-rw-r--r--complete.go95
-rw-r--r--debugger.go12
3 files changed, 30 insertions, 78 deletions
diff --git a/bash.orig.go b/bash.orig.go
index 1753498..52920eb 100644
--- a/bash.orig.go
+++ b/bash.orig.go
@@ -30,6 +30,7 @@ type AutoArgs struct {
hidden bool // don't update the toolkits when it's hidden
Auto func([]string) // the function for shell autocomplete
appName string // a good way to track the name of the binary ?
+ examples func() string // some examples
pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
autoFunc func(*Auto) // also a function for autocomplete
match map[string]string // maps for strings
diff --git a/complete.go b/complete.go
index 3f4ff60..e82510b 100644
--- a/complete.go
+++ b/complete.go
@@ -81,6 +81,19 @@ func (pb *Auto) doHandlePB() error {
func (pb *Auto) SubCommand(cmd ...string) {
partial := strings.Trim(pb.Partial, "'")
if pb.Debug {
+ if myAuto.examples == nil {
+ log.Fprintf(os.Stderr, "\n")
+ log.Fprintf(os.Stderr, "examples was nil\n")
+ // log.Fprintf(os.Stderr, "\n")
+ } else {
+ log.Fprintf(os.Stderr, "\n")
+ log.Fprintf(os.Stderr, "\n")
+ log.Fprintf(os.Stderr, "Examples:\n")
+ for _, line := range strings.Split(myAuto.examples(), "\n") {
+ log.Fprintf(os.Stderr, " %s\n", line)
+ }
+ // log.Fprintf(os.Stderr, "\n")
+ }
myAuto.pp.WriteHelpForAutocomplete(os.Stderr, os.Stdout, partial, cmd...)
// myAuto.pp.GetUsageForSubcommand(os.Stdout, os.Stderr, partial, cmd)
// myAuto.pp.GetUsageForSubcommand(os.Stdout, nil, partial, cmd)
@@ -249,9 +262,11 @@ func parseArgv(argname string) *Auto {
return pb
}
-// also try to parse/send cur (?)
-// func Bash3(appAutoFunc func(*Auto), dest any) *Auto {
func Bash3(dest any) *Auto {
+ return Bash(dest)
+}
+
+func Bash(dest any) *Auto {
myAuto = new(AutoArgs)
findAppInfo(dest) // parses back to main() for argv info
@@ -355,79 +370,3 @@ func doBash(argname string) {
}
os.Exit(0)
}
-
-/*
-// also try to parse/send cur (?)
-func Bash2(argname string, appAutoFunc func(*Auto)) *Auto {
- pb := parseArgv(argname)
- if pb.SetupAuto {
- // --bash was passed. try to configure bash-completion
- doBash2(argname)
- os.Exit(0)
- }
-
- if pb.Debug {
- // dump debug info
- pb.PrintDebug()
- }
-
- if pb.IsAuto {
- pb.doHandlePB()
- if pb.Debug {
- // TODO:
- // check here to see if there was any completion text sent
- // if not, send "reset bash newline\n" to cause bash to redraw PS1 for the user
- }
- arg.Register(&argBash)
- // flags := []string{pb.Arg3, pb.Arg0}
- // arg.InitFlags(flags)
-
- appAutoFunc(pb) // run the autocomplete function the user made for their application
- os.Exit(0)
- }
-
- arg.Register(&argBash)
- return pb
-}
-*/
-
-/*
-// prints help to STDERR // TODO: move everything below this to go-args
-func (args) doBashHelp() {
- if argv.BashAuto[1] != "''" {
- // if this is not blank, then the user has typed something
- return
- }
- if argv.BashAuto[0] != ARGNAME {
- // if this is not the name of the command, the user already started doing something
- return
- }
- if argv.BashAuto[0] == ARGNAME {
- me.pp.WriteHelp(os.Stderr)
- return
- }
- fmt.Fprintln(os.Stderr, "")
- fmt.Fprintln(os.Stderr, "hello world")
- fmt.Fprintln(os.Stderr, "")
-}
-*/
-
-/*
-func (pb *Auto) Autocomplete(notsure any, sendthis string) {
- parts := strings.Split(sendthis, " ")
- var all []string
- for _, part := range parts {
- var found bool
- for _, s := range os.Args {
- if s == part {
- found = true
- }
- }
- if found {
- continue
- }
- all = append(all, part)
- }
- fmt.Printf("%s", strings.Join(all, " "))
-}
-*/
diff --git a/debugger.go b/debugger.go
index 5b1e2a9..5fe28f9 100644
--- a/debugger.go
+++ b/debugger.go
@@ -55,6 +55,12 @@ type AutoFuncd interface {
DoAutoComplete(*Auto)
}
+type Examplesd interface {
+ // Version returns the version string that will be printed on a line by itself
+ // at the top of the help message.
+ Examples() string
+}
+
// Described is the interface that the destination struct should implement to
func findAppInfo(tmp interface{}) {
if tmp, ok := tmp.(Appnamed); ok {
@@ -63,6 +69,12 @@ func findAppInfo(tmp interface{}) {
panic("you need to make the function argv.Appname()")
}
+ if tmp, ok := tmp.(Examplesd); ok {
+ myAuto.examples = tmp.Examples
+ } else {
+ panic("you need to make the function argv.Appname()")
+ }
+
if tmp, ok := tmp.(AutoFuncd); ok {
myAuto.autoFunc = tmp.DoAutoComplete
} else {