fix help, short names for options

This commit is contained in:
Roberto Hidalgo 2022-12-22 23:29:58 -06:00
parent 20a2befa34
commit 26e00210aa
3 changed files with 14 additions and 4 deletions

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Chinampa
Like [`milpa`](https://milpa.dev) but for go programs only.

View File

@ -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

View File

@ -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
}