From a1792c4e1afb1e8901f05e41e61add8f39038055 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 19 Oct 2025 06:25:16 -0500 Subject: new argv --- argv.custom.go | 50 ++++++++++++++++++++++++++++++++++++++ argv.go | 73 -------------------------------------------------------- argv.struct.go | 17 +++++++++++++ argv.template.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 13 +++------- structs.go | 6 ++++- 6 files changed, 146 insertions(+), 84 deletions(-) create mode 100644 argv.custom.go delete mode 100644 argv.go create mode 100644 argv.struct.go create mode 100644 argv.template.go diff --git a/argv.custom.go b/argv.custom.go new file mode 100644 index 0000000..fbbdc67 --- /dev/null +++ b/argv.custom.go @@ -0,0 +1,50 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +import ( + "os" + + "go.wit.com/lib/protobuf/argvpb" + "go.wit.com/log" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +// used for shell auto completion +var APPNAME string = "zood" + +func (a args) Description() string { + return ` + this daemon talks to zookeeper +` +} + +var NOW *log.LogFlag +var INFO *log.LogFlag +var PING *log.LogFlag +var WARN *log.LogFlag + +func init() { + full := "go.wit.com/apps/zood" + short := "zood" + + NOW = log.NewFlag("NOW", true, full, short, "useful while doing debugging") + INFO = log.NewFlag("INFO", false, full, short, "general zood") + PING = log.NewFlag("PING", false, full, short, "show pings to the zookeeper") + WARN = log.NewFlag("WARN", true, full, short, "bad things") +} + +// sends the strings to bash or zsh that will be your options +func (a args) SendCompletionStrings(pb *argvpb.Argv) { + if pb.Cmd == "" { + base := []string{"status", "test", "--version", "--daemon"} + pb.SendStrings(base) + } else { + pb.SubCommand(pb.Goargs...) + } + os.Exit(0) +} diff --git a/argv.go b/argv.go deleted file mode 100644 index 328da54..0000000 --- a/argv.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright 2017-2025 WIT.COM Inc. All rights reserved. -// Use of this source code is governed by the GPL 3.0 - -package main - -/* - this parses the command line arguements - this enables command line options from other packages like 'gui' and 'log' -*/ - -import ( - "os" - - "go.wit.com/lib/protobuf/argvpb" - "go.wit.com/log" -) - -var argv args - -type args struct { - Status *EmptyCmd `arg:"subcommand:status" help:"should display something"` - Test *EmptyCmd `arg:"subcommand:test" help:"test stuff"` - Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` - Port int `arg:"--port" default:"2521" help:"port to run on"` - URL string `arg:"--url" help:"url to use"` -} - -type EmptyCmd struct { -} - -func (a args) Description() string { - return ` - this daemon talks to zookeeper -` -} - -var NOW *log.LogFlag -var INFO *log.LogFlag -var PING *log.LogFlag -var WARN *log.LogFlag - -func init() { - full := "go.wit.com/apps/zood" - short := "zood" - - NOW = log.NewFlag("NOW", true, full, short, "useful while doing debugging") - INFO = log.NewFlag("INFO", false, full, short, "general zood") - PING = log.NewFlag("PING", false, full, short, "show pings to the zookeeper") - WARN = log.NewFlag("WARN", true, full, short, "bad things") -} - -func (args) Version() string { - return argvpb.StandardVersion(ARGNAME, VERSION, BUILDTIME) -} - -func (args) Buildtime() (string, string) { - return BUILDTIME, VERSION -} - -func (args) Appname() string { - return ARGNAME -} - -// sends the strings to bash or zsh that will be your options -func (a args) SendCompletionStrings(pb *argvpb.Argv) { - if pb.Cmd == "" { - base := []string{"status", "test", "--version", "--daemon"} - pb.SendStrings(base) - } else { - pb.SubCommand(pb.Goargs...) - } - os.Exit(0) -} diff --git a/argv.struct.go b/argv.struct.go new file mode 100644 index 0000000..7088811 --- /dev/null +++ b/argv.struct.go @@ -0,0 +1,17 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +var argv args + +type args struct { + Status *EmptyCmd `arg:"subcommand:status" help:"should display something"` + Test *EmptyCmd `arg:"subcommand:test" help:"test stuff"` + Daemon bool `arg:"--daemon" default:"false" help:"run in daemon mode"` + Port int `arg:"--port" default:"2521" help:"port to run on"` + URL string `arg:"--url" help:"url to use"` +} + +type EmptyCmd struct { +} diff --git a/argv.template.go b/argv.template.go new file mode 100644 index 0000000..cd4421d --- /dev/null +++ b/argv.template.go @@ -0,0 +1,71 @@ +package main + +// these are stubbed in functions needed +// just copy this file from another working app for now +// you shouldn't need to change anything here +// TODO: clean this up in argv + +import ( + "os" + + "go.wit.com/dev/alexflint/arg" + "go.wit.com/gui" + "go.wit.com/log" +) + +func (args) InitArgv() (string, string, string) { + return APPNAME, BUILDTIME, VERSION +} + +// this function will send the current argv PB to go-args for parsing +func (args) ParseFlags(flags []string) error { + var err error + if me.pp == nil { + // log.Info("Parse Flags GOT flags:", flags) + me.pp, err = arg.ParseFlags(flags, &argv) + // panic("got to the app's ParseFlags()") + } else { + panic("me.pp was not nil") + } + return err +} + +// add this funcgion: this will print the help +func (args) WriteHelpForSubcommand(cmd string) error { + me.pp.WriteHelpForSubcommand(os.Stderr, cmd) + return nil +} + +// this will print the help for the subcmd +func (args) WriteHelpForAutocomplete(part string, subcmd ...string) error { + return me.pp.WriteHelpForAutocomplete(os.Stderr, os.Stdout, part, subcmd...) +} + +func (args) WriteHelpForAutocompleteDebug(part string, subcmd ...string) error { + f, _ := os.OpenFile("/tmp/argv.junk", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + return me.pp.WriteHelpForAutocomplete(f, os.Stdout, part, subcmd...) +} + +// add this funcgion: this will print the help +func (args) WriteHelp() error { + me.pp.WriteHelp(os.Stderr) + return nil +} + +func (args) Exit() { + gui.UnloadToolkits() + if me.argv.Verbose() { + log.Info("argv.Exit() called", APPNAME+".Exit()") + } + // remove this from the template for your app (or make one for youself if you need it) + // forgeExit() // custom forge shutdown function +} + +func (args) Help() string { + return "got app help" +} + +func (args) MustParse() error { + me.pp = arg.MustParse(&argv) + return nil +} diff --git a/main.go b/main.go index 2ab076e..73f2b9a 100644 --- a/main.go +++ b/main.go @@ -16,19 +16,12 @@ import ( "go.wit.com/log" ) -// sent via -ldflags -var VERSION string -var BUILDTIME string - -// used for shell auto completion -var ARGNAME string = "zood" - //go:embed resources/* var resources embed.FS func main() { me = new(zoodStruct) - me.sh = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args + me.argv = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args if argv.Daemon { // turn off timestamps for STDOUT (systemd adds them) @@ -40,13 +33,13 @@ func main() { if argv.Status != nil { log.Info("todo: show status here") - me.sh.GoodExit("says hi") + me.argv.GoodExit("says hi") } if err := testZoo(); err != nil { log.Info("failed to connect. sleeping for 3 minutes") time.Sleep(3 * time.Minute) - me.sh.BadExit("failed to connect to zookeeper", err) + me.argv.BadExit("failed to connect to zookeeper", err) } me.pollDelay = 3 * time.Second me.failcountmax = 20 // die every minute if zookeeper can't be found diff --git a/structs.go b/structs.go index d3bd152..2a618a3 100644 --- a/structs.go +++ b/structs.go @@ -6,6 +6,7 @@ package main import ( "time" + "go.wit.com/dev/alexflint/arg" "go.wit.com/lib/protobuf/argvpb" "go.wit.com/lib/protobuf/zoopb" ) @@ -14,7 +15,10 @@ var me *zoodStruct // this app's variables type zoodStruct struct { - sh *argvpb.Argv // shell autocomplete + argv *argvpb.Argv // more experiments for bash handling + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! + // myGui *fhelp.GuiPrep // for initializing the GUI toolkits + urlbase string // the dns name for the zookeeper hostname string // my hostname pollDelay time.Duration // how often to report our status -- cgit v1.2.3