Compare commits

..

3 Commits

Author SHA1 Message Date
Roberto Hidalgo
58b225f25e add (opt|arg).Repeats() 2024-06-20 19:37:21 -06:00
Roberto Hidalgo
4ffa7ba364 add SetOutput to logger for testing convenience 2024-06-19 17:27:47 -06:00
Roberto Hidalgo
51e6c01c5a remove statuscode.RenderHelp 2024-06-19 14:16:10 -06:00
6 changed files with 22 additions and 10 deletions

View File

@ -53,9 +53,9 @@ By default, and unless ﹅` + env.NoColor + `﹅ is set, ﹅@chinampa@ help﹅ w
if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] { if len(args) > 0 && c != nil && c.Name() != args[len(args)-1] {
c, topicArgs, err := c.Root().Find(args) c, topicArgs, err := c.Root().Find(args)
if err == nil && c != nil && len(topicArgs) == 0 { if err == nil && c != nil && len(topicArgs) == 0 {
// exact command help // user requestd command help with flag command
cobra.CheckErr(c.Help()) cobra.CheckErr(c.Help())
os.Exit(statuscode.RenderHelp) os.Exit(statuscode.Ok)
return return
} }
@ -74,9 +74,12 @@ By default, and unless ﹅` + env.NoColor + `﹅ is set, ﹅@chinampa@ help﹅ w
os.Exit(statuscode.NotFound) os.Exit(statuscode.NotFound)
} }
// top-level help with no arguments called
// write to stdout
c.SetOut(os.Stdout)
c.InitDefaultHelpFlag() // make possible 'help' flag to be shown c.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cobra.CheckErr(c.Help()) cobra.CheckErr(c.Help())
os.Exit(statuscode.RenderHelp) os.Exit(statuscode.Ok)
}, },
} }

View File

@ -182,6 +182,11 @@ func (arg *Argument) SetValue(value []string) {
arg.provided = &value arg.provided = &value
} }
// Repeats tells if an argument should be presented as a list.
func (arg *Argument) Repeats() bool {
return arg.Variadic
}
func (arg *Argument) IsKnown() bool { func (arg *Argument) IsKnown() bool {
return arg.provided != nil && len(*arg.provided) > 0 return arg.provided != nil && len(*arg.provided) > 0
} }

View File

@ -137,6 +137,11 @@ func (opt *Option) ToString() string {
return stringValue return stringValue
} }
// Repeats tells if an option should be presented as a list.
func (opt *Option) Repeats() bool {
return opt.Repeated
}
func (opt *Option) internalValidate(name, current string) error { func (opt *Option) internalValidate(name, current string) error {
if current == "" { if current == "" {
return nil return nil

View File

@ -25,12 +25,8 @@ func showHelp(cmd *cobra.Command) {
// HandleCobraExit is called when a command errors out or was not found. // HandleCobraExit is called when a command errors out or was not found.
func HandleCobraExit(cmd *cobra.Command, err error) error { func HandleCobraExit(cmd *cobra.Command, err error) error {
if err == nil { if err == nil {
ok, err := cmd.PersistentFlags().GetBool(_c.HelpCommandName)
if cmd.Name() == _c.HelpCommandName || err == nil && ok {
os.Exit(statuscode.RenderHelp)
}
os.Exit(statuscode.Ok) os.Exit(statuscode.Ok)
return nil
} }
switch err.(type) { switch err.(type) {

View File

@ -4,6 +4,7 @@ package logger
import ( import (
"context" "context"
"io"
"git.rob.mx/nidito/chinampa/pkg/runtime" "git.rob.mx/nidito/chinampa/pkg/runtime"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -47,6 +48,10 @@ func Configure(name string, level Level) {
} }
} }
func SetOutput(out io.Writer) {
logrus.SetOutput(out)
}
func Debug(args ...any) { func Debug(args ...any) {
Main.Debug(args...) Main.Debug(args...)
} }

View File

@ -12,8 +12,6 @@ package statuscode
const ( const (
// Ok means everything is fine. // Ok means everything is fine.
Ok = 0 Ok = 0
// RenderHelp provides answers to life, the universe and everything; also, renders help.
RenderHelp = 42
// Usage means bad arguments were provided by the user. // Usage means bad arguments were provided by the user.
Usage = 64 Usage = 64
// ProgrammerError means the developer made a mistake. // ProgrammerError means the developer made a mistake.