From 8385540f09ca42ef9443fddfd1a89e76ad5f1329 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 26 Oct 2025 08:55:11 -0500 Subject: new argvpb changes --- argv.custom.go | 48 ------------------------------------- argv.go | 32 +++++++++++++++++++++++++ argv.struct.go | 32 ------------------------- argv.template.go | 72 ------------------------------------------------------- complete.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ doProto.go | 7 +++--- generateHeader.go | 5 ++-- main.go | 8 +++---- structs.go | 6 ++--- 9 files changed, 107 insertions(+), 165 deletions(-) delete mode 100644 argv.custom.go create mode 100644 argv.go delete mode 100644 argv.struct.go delete mode 100644 argv.template.go create mode 100644 complete.go diff --git a/argv.custom.go b/argv.custom.go deleted file mode 100644 index 3401eea..0000000 --- a/argv.custom.go +++ /dev/null @@ -1,48 +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 - -import ( - "os" - - "go.wit.com/lib/protobuf/argvpb" -) - -// sent via -ldflags -var VERSION string -var BUILDTIME string - -var APPNAME string = "autogenpb" - -func (a args) Description() string { - return "go.wit.com/apps/autogenpb " + VERSION + " Built on " + BUILDTIME + ` - -From a .proto file, autogenpb will: - - * auto generate Sort(), Marshal() and GUI() functions in .pb.go files - * auto format the .proto file (like goimport does for .go files) - * encourages (enforces?) plural struct names (like rails) - -See the sources for an example .proto files - -Example usage: - - autogenpb --proto foo.proto --package main -` -} - -func (args) MatchClean() string { - return "foo.proto bar.proto" -} - -// sends the strings to bash or zsh that will be your options -func (a args) SendCompletionStrings(pb *argvpb.Argv) { - if pb.GetCmd() == "" { - base := []string{"--bash", "--proto", "--regret", "--debug", "--renumber", "--reformat", "--delete", "--dry-run"} - pb.SendStrings(base) - } else { - pb.SubCommand(pb.Goargs...) - } - os.Exit(0) -} diff --git a/argv.go b/argv.go new file mode 100644 index 0000000..0649e16 --- /dev/null +++ b/argv.go @@ -0,0 +1,32 @@ +// 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' +*/ + +var argv args + +type args struct { + Package string `arg:"--package" help:"the package name"` + Proto string `arg:"--proto" help:"the .proto filename"` + Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"` + MutexName string `arg:"--mutex-name" help:"use a var name for the mutex"` + Regret bool `arg:"--regret" help:"ignore needed UUID. You will eventually regret this."` + Delete bool `arg:"--delete" help:"use delete with copy experiment"` + DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` + ReFormat bool `arg:"--reformat" help:"reformat the .proto file and exit"` + Debug bool `arg:"--debug" help:"enable debugging information"` + Comments bool `arg:"--format-comments" help:"enforce parseable comments in a .proto file"` + NoFormat bool `arg:"--no-format" help:"do not auto-reformat the .proto file"` + Renumber bool `arg:"--renumber" help:"renumber everything. obviously breaks backwards compatiblity"` + Clean bool `arg:"--clean" help:"clean out any *pb.go files; bypassing mtime sanity checks"` + Mtime bool `arg:"--mtime" help:"do os.Stat() modtime sanity checks"` + GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` + GoPath string `arg:"--gopath" help:"the gopath of this repo"` + Identify string `arg:"--identify" help:"identify file"` +} diff --git a/argv.struct.go b/argv.struct.go deleted file mode 100644 index 0649e16..0000000 --- a/argv.struct.go +++ /dev/null @@ -1,32 +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' -*/ - -var argv args - -type args struct { - Package string `arg:"--package" help:"the package name"` - Proto string `arg:"--proto" help:"the .proto filename"` - Mutex bool `arg:"--mutex" default:"true" help:"insert a mutex into protoc .pb.go file"` - MutexName string `arg:"--mutex-name" help:"use a var name for the mutex"` - Regret bool `arg:"--regret" help:"ignore needed UUID. You will eventually regret this."` - Delete bool `arg:"--delete" help:"use delete with copy experiment"` - DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` - ReFormat bool `arg:"--reformat" help:"reformat the .proto file and exit"` - Debug bool `arg:"--debug" help:"enable debugging information"` - Comments bool `arg:"--format-comments" help:"enforce parseable comments in a .proto file"` - NoFormat bool `arg:"--no-format" help:"do not auto-reformat the .proto file"` - Renumber bool `arg:"--renumber" help:"renumber everything. obviously breaks backwards compatiblity"` - Clean bool `arg:"--clean" help:"clean out any *pb.go files; bypassing mtime sanity checks"` - Mtime bool `arg:"--mtime" help:"do os.Stat() modtime sanity checks"` - GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` - GoPath string `arg:"--gopath" help:"the gopath of this repo"` - Identify string `arg:"--identify" help:"identify file"` -} diff --git a/argv.template.go b/argv.template.go deleted file mode 100644 index 4e6f4f7..0000000 --- a/argv.template.go +++ /dev/null @@ -1,72 +0,0 @@ -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/lib/env" - "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 env.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/complete.go b/complete.go new file mode 100644 index 0000000..ae1dd12 --- /dev/null +++ b/complete.go @@ -0,0 +1,62 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +import ( + "fmt" + "strings" + + "go.wit.com/dev/alexflint/arg" + "go.wit.com/lib/protobuf/argvpb" +) + +// sent via -ldflags +var VERSION string +var BUILDTIME string + +var APPNAME string = "autogenpb" + +func (a args) Description() string { + return "go.wit.com/apps/autogenpb " + VERSION + " Built on " + BUILDTIME + ` + +From a .proto file, autogenpb will: + + * auto generate Sort(), Marshal() and GUI() functions in .pb.go files + * auto format the .proto file (like goimport does for .go files) + * encourages (enforces?) plural struct names (like rails) + +See the sources for an example .proto files + +Example usage: + + autogenpb --proto foo.proto --package main +` +} + +func (args) MustParse() error { + me.pp = arg.MustParse(&argv) + return nil +} + +func (args) MatchClean() string { + return "foo.proto bar.proto" +} + +// sends the strings to bash or zsh that will be your options +func (a args) SendCompletionStrings() error { + if argvpb.PB.GetCmd() == "" { + base := []string{"--bash", "--proto", "--regret", "--debug", "--renumber", "--reformat", "--delete", "--dry-run"} + fmt.Fprintf(argvpb.Stdout, " %s", strings.Join(base, " ")) + return nil + } + var err error + if me.pp == nil { + me.pp, err = arg.ParseFlagsArgv(&argv) + if err != nil { + return err + } + } + err = me.pp.WriteHelpForAutocomplete(argvpb.PB.Partial, argvpb.PB.Real...) + return err +} diff --git a/doProto.go b/doProto.go index 5335ab9..6060f59 100644 --- a/doProto.go +++ b/doProto.go @@ -23,6 +23,7 @@ import ( "github.com/go-cmd/cmd" "go.wit.com/lib/fhelp" "go.wit.com/lib/gui/shell" + "go.wit.com/lib/protobuf/argvpb" "go.wit.com/log" ) @@ -36,12 +37,12 @@ func doProto(argvProto string) (string, error) { if argv.Clean { doClean(pf.Filebase) - me.argv.GoodExit("doClean() ran") + argvpb.GoodExit("doClean() ran") } if argv.Mtime { doMtime(pf.Filebase) - me.argv.GoodExit("doClean() ran") + argvpb.GoodExit("doClean() ran") } if argv.ReFormat { @@ -63,7 +64,7 @@ func doProto(argvProto string) (string, error) { } if doMtime(pf.Filebase) { - me.argv.GoodExit(pf.Filename + " did not change") + argvpb.GoodExit(pf.Filename + " did not change") } else { log.Info("ctime check: need to re-run autogenpb") } diff --git a/generateHeader.go b/generateHeader.go index eafc25b..c94b045 100644 --- a/generateHeader.go +++ b/generateHeader.go @@ -8,6 +8,7 @@ import ( "io" "os" + "go.wit.com/lib/protobuf/argvpb" "go.wit.com/log" ) @@ -19,7 +20,7 @@ func pbHeaderComment(w io.Writer) { fmt.Fprintln(w, "// user defined Mutex locks were auto added") fmt.Fprintln(w, "//") fmt.Fprintln(w, "// This file was autogenerated with autogenpb:") - fmt.Fprintln(w, "// ", me.argv.Version()) + fmt.Fprintln(w, "// ", argvpb.Version()) fmt.Fprintln(w, "// Theese sort.pb.go and marshal.pb.go files are autogenerated") fmt.Fprintln(w, "// The autogenpb sources have example .proto files with instructions") fmt.Fprintln(w, "") @@ -31,7 +32,7 @@ func headerComment(w io.Writer) { fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest") fmt.Fprintln(w, "//") fmt.Fprintln(w, "// This file was autogenerated with autogenpb:") - fmt.Fprintln(w, "// ", me.argv.Version()) + fmt.Fprintln(w, "// ", argvpb.Version()) fmt.Fprintln(w, "// Theese sort.pb.go and marshal.pb.go files are autogenerated") fmt.Fprintln(w, "// The autogenpb sources have example .proto files with instructions") fmt.Fprintln(w, "//") diff --git a/main.go b/main.go index ea3de98..6645fb7 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ import ( func main() { me = new(mainType) - me.argv = argvpb.Autocomplete(&argv) // adds shell auto complete to go-args + argvpb.Init(&argv, APPNAME, BUILDTIME, VERSION) // adds shell auto-complete me.pb = new(Files) var s string var err error @@ -45,9 +45,9 @@ func main() { // safe exits back to your shell (with timing and toolkit close) if err != nil { - me.argv.BadExit(s, err) + argvpb.BadExit(s, err) } - me.argv.GoodExit(s) + argvpb.GoodExit(s) } func okExit(s string) { @@ -67,7 +67,7 @@ func doPrecheck() (string, error) { if argv.Proto == "" { // todo: run on every .proto file log.Info("todo: run on all .proto files") - argv.WriteHelp() + me.pp.WriteHelp(os.Stdout) return "you must provide --proto .proto", errors.New("need .proto") } diff --git a/structs.go b/structs.go index f74b90f..75e9891 100644 --- a/structs.go +++ b/structs.go @@ -5,14 +5,12 @@ package main import ( "go.wit.com/dev/alexflint/arg" - "go.wit.com/lib/protobuf/argvpb" ) var me *mainType // this app's variables type mainType struct { - argv *argvpb.Argv // more experiments for bash handling - pp *arg.Parser // for parsing the command line args. Yay to alexf lint! - pb *Files // all the proto files as they get parsed (into another protobuf file) + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! + pb *Files // all the proto files as they get parsed (into another protobuf file) } -- cgit v1.2.3