Skip to content

"help" subcommand does not work when MarkPersistentRequiredFlag is used. #1918

@makiuchi-d

Description

@makiuchi-d

This issue seems to be similar to #1093.

The auto-implemented subcommands (help and completion) don't work when MarkPersistentRequiredFlag() called on root command.
Of course it works if the required flag is specified.
IMHO, these auto-implemented subcommands should be able to work without flags defined by user.
Additionaly, these subcommends should not ignore auto-implement flags like -h, --help (this differs from #1093).

package main

import (
	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use: "mycmd",
}

var subCmd = &cobra.Command{
	Use: "subcmd",
	Run: func(cmd *cobra.Command, args []string) {},
}

func main() {
	rootCmd.PersistentFlags().Bool("required", true, "required")
	rootCmd.MarkPersistentFlagRequired("required")
	rootCmd.AddCommand(subCmd)
	rootCmd.Execute()
}
$ go run . help
Error: required flag(s) "required" not set
Usage:
  mycmd help [command] [flags]

Flags:
  -h, --help   help for help

Global Flags:
      --required   required (default true)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/cobra-commandCore `cobra.Command` implementationskind/bugA bug in cobra; unintended behavior

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions