cleaner argv autocomplete

This commit is contained in:
Jeff Carr 2025-09-09 02:30:38 -05:00
parent f1c1ca950c
commit 4827be1d2a
2 changed files with 6 additions and 41 deletions

32
argv.go
View File

@ -25,8 +25,6 @@ type args struct {
Daemon bool `arg:"--daemon" help:"run as a daemon"` Daemon bool `arg:"--daemon" help:"run as a daemon"`
Force bool `arg:"--force" help:"try to strong arm things"` Force bool `arg:"--force" help:"try to strong arm things"`
Verbose bool `arg:"--verbose" help:"show more output"` Verbose bool `arg:"--verbose" help:"show more output"`
Bash bool `arg:"--bash" help:"generate bash completion"`
BashAuto []string `arg:"--auto-complete" help:"todo: move this to go-arg"`
} }
type EmptyCmd struct { type EmptyCmd struct {
@ -40,37 +38,13 @@ func init() {
arg.MustParse(&argv) arg.MustParse(&argv)
} }
// prints help to STDERR // TODO: move everything below this to go-args func (a args) DoAutoComplete(argv []string) {
func (args) doBashHelp() {
if len(argv.BashAuto) < 2 {
fmt.Fprintf(os.Stderr, "something went wrong with the GO args autocomplete in %s\n", ARGNAME)
return
}
if argv.BashAuto[1] != "''" {
// if this is not blank, then the user has typed something
return
}
if argv.BashAuto[0] != ARGNAME {
fmt.Fprintln(os.Stderr, argv.BashAuto[0])
// 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, "something went wrong with the GO args package")
fmt.Fprintln(os.Stderr, "")
}
func (args) doBashAuto() {
// argv.doBashHelp() // argv.doBashHelp()
switch argv.BashAuto[0] { switch argv[0] {
case "merge": case "merge":
fmt.Println("--force") fmt.Println("--force")
default: default:
if argv.BashAuto[0] == ARGNAME { if argv[0] == ARGNAME {
// list the subcommands here // list the subcommands here
fmt.Println("list merge") fmt.Println("list merge")
} }

15
main.go
View File

@ -8,8 +8,7 @@ import (
"time" "time"
"go.wit.com/dev/alexflint/arg" "go.wit.com/dev/alexflint/arg"
"go.wit.com/gui" "go.wit.com/lib/gui/prep"
"go.wit.com/lib/fhelp"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -28,18 +27,10 @@ var LIBDIR string = "/var/lib/forged/" // need to deprecate this
func main() { func main() {
me = new(mainType) me = new(mainType)
gui.InitArg() prep.Bash(ARGNAME, argv.DoAutoComplete) // todo: this line should be: prep.Bash(argv)
me.myGui = prep.Gui() // prepares the GUI package for go-args
me.pp = arg.MustParse(&argv) me.pp = arg.MustParse(&argv)
if argv.Bash {
fhelp.DoBash(ARGNAME)
os.Exit(0)
}
if len(argv.BashAuto) != 0 {
argv.doBashAuto()
os.Exit(0)
}
if argv.Hostname != "" { if argv.Hostname != "" {
HOSTNAME = argv.Hostname HOSTNAME = argv.Hostname
} }