cobraize once, prevent parsing errors from panics

This commit is contained in:
Roberto Hidalgo 2023-09-24 23:04:13 -06:00
parent de1c898f0d
commit d3e5afbdfc
2 changed files with 10 additions and 2 deletions

View File

@ -57,7 +57,11 @@ func newCobraRoot(root *command.Command) *cobra.Command {
func ToCobra(cmd *command.Command, globalOptions command.Options, parent *cobra.Command) *cobra.Command {
localName := cmd.Name()
useSpec := []string{localName, "[options]"}
for _, arg := range cmd.Arguments {
for idx, arg := range cmd.Arguments {
if arg == nil {
useSpec = append(useSpec, fmt.Sprintf("could not parse spec for argument %d of command %s", idx, cmd.FullName()))
continue
}
useSpec = append(useSpec, arg.ToDesc())
}
@ -93,6 +97,10 @@ func ToCobra(cmd *command.Command, globalOptions command.Options, parent *cobra.
cc.Flags().AddFlagSet(cmd.FlagSet())
for name, opt := range cmd.Options {
if opt == nil {
useSpec = append(useSpec, fmt.Sprintf("could not parse spec for option %s of command %s", name, cmd.FullName()))
continue
}
if err := cc.RegisterFlagCompletionFunc(name, opt.CompletionFunction); err != nil {
log.Errorf("Failed setting up autocompletion for option <%s> of command <%s>", name, cmd.FullName())
}

View File

@ -105,7 +105,7 @@ func Execute(version string) error {
container := ccRoot
for idx, cp := range cmd.Path {
if idx == len(cmd.Path)-1 {
if cmd.Action != nil {
if cmd.Action != nil && cmd.Cobra == nil {
// nil actions come when the current command consists only
// of metadata for a "group parent" command
// and we don't wanna cobraize it like a regular, actionable one