From 26e00210aa8f641b3b0e3fff4af2634dd2a2780a Mon Sep 17 00:00:00 2001 From: Roberto Hidalgo Date: Thu, 22 Dec 2022 23:29:58 -0600 Subject: [PATCH] fix help, short names for options --- README.md | 3 +++ internal/registry/registry.go | 8 ++++++-- pkg/command/command.go | 7 +++++-- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..3948c89 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Chinampa + +Like [`milpa`](https://milpa.dev) but for go programs only. diff --git a/internal/registry/registry.go b/internal/registry/registry.go index a025d44..83ea8f9 100644 --- a/internal/registry/registry.go +++ b/internal/registry/registry.go @@ -218,9 +218,13 @@ func Execute(version string) error { } cmdRoot.SetCobra(ccRoot) - err := ccRoot.Execute() + current, _, err := ccRoot.Find(os.Args[1:]) if err != nil { - errors.HandleCobraExit(ccRoot, err) + current = ccRoot + } + err = current.Execute() + if err != nil { + errors.HandleCobraExit(current, err) } return err diff --git a/pkg/command/command.go b/pkg/command/command.go index 7caf276..1108238 100644 --- a/pkg/command/command.go +++ b/pkg/command/command.go @@ -81,14 +81,15 @@ func (cmd *Command) FlagSet() *pflag.FlagSet { if opt.Default != nil { def = opt.Default.(bool) } - fs.Bool(name, def, opt.Description) + + fs.BoolP(name, opt.ShortName, def, opt.Description) case ValueTypeDefault, ValueTypeString: opt.Type = ValueTypeString def := "" if opt.Default != nil { def = fmt.Sprintf("%s", opt.Default) } - fs.String(name, def, opt.Description) + fs.StringP(name, opt.ShortName, def, opt.Description) default: // ignore flag logrus.Warnf("Ignoring unknown option type <%s> for option <%s>", opt.Type, name) @@ -113,6 +114,7 @@ func (cmd *Command) ParseInput(cc *cobra.Command, args []string) error { logrus.Debug("Validating flags") if err := cmd.Options.AreValid(); err != nil { + logrus.Debug("Invalid flags for %s: %w", cmd.FullName(), err) return err } } @@ -124,6 +126,7 @@ func (cmd *Command) Run(cc *cobra.Command, args []string) error { logrus.Debugf("running command %s", cmd.FullName()) if err := cmd.ParseInput(cc, args); err != nil { + logrus.Debugf("Parsing input to command %s failed: %s", cmd.FullName(), err) return err }