diff --git a/pkg/karmadactl/cmdinit/cmdinit.go b/pkg/karmadactl/cmdinit/cmdinit.go index 5d55d170d..b4402d820 100644 --- a/pkg/karmadactl/cmdinit/cmdinit.go +++ b/pkg/karmadactl/cmdinit/cmdinit.go @@ -76,7 +76,7 @@ func NewCmdInit(parentCommand string) *cobra.Command { return nil }, } - flags := cmd.PersistentFlags() + flags := cmd.Flags() releaseVer, err := version.ParseGitVersion(version.Get().GitVersion) if err != nil { diff --git a/pkg/karmadactl/karmadactl.go b/pkg/karmadactl/karmadactl.go index 1a0fa8718..d32ebf603 100644 --- a/pkg/karmadactl/karmadactl.go +++ b/pkg/karmadactl/karmadactl.go @@ -14,6 +14,7 @@ import ( "k8s.io/kubectl/pkg/util/templates" "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit" + "github.com/karmada-io/karmada/pkg/karmadactl/options" "github.com/karmada-io/karmada/pkg/version/sharedcommand" ) @@ -95,6 +96,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command { filters := []string{"options"} rootCmd.AddCommand(sharedcommand.NewCmdVersion(parentCommand)) + rootCmd.AddCommand(options.NewCmdOptions(parentCommand, ioStreams.Out)) templates.ActsAsRootCommand(rootCmd, filters, groups...) diff --git a/pkg/karmadactl/options/options.go b/pkg/karmadactl/options/options.go new file mode 100644 index 000000000..e776b1f00 --- /dev/null +++ b/pkg/karmadactl/options/options.go @@ -0,0 +1,41 @@ +package options + +import ( + "fmt" + "io" + + "github.com/spf13/cobra" + "k8s.io/kubectl/pkg/util/templates" +) + +var ( + optionsExample = templates.Examples(` + # Print flags inherited by all commands + %[1]s options`) +) + +// NewCmdOptions implements the options command +func NewCmdOptions(parentCommand string, out io.Writer) *cobra.Command { + cmd := &cobra.Command{ + Use: "options", + Short: "Print the list of flags inherited by all commands", + Long: "Print the list of flags inherited by all commands", + Example: fmt.Sprintf(optionsExample, parentCommand), + RunE: func(cmd *cobra.Command, args []string) error { + if err := cmd.Usage(); err != nil { + return err + } + return nil + }, + } + + // The `options` command needs write its output to the `out` stream + // (typically stdout). Without calling SetOutput here, the Usage() + // function call will fall back to stderr. + // + // See https://github.com/kubernetes/kubernetes/pull/46394 for details. + cmd.SetOutput(out) + + templates.UseOptionsTemplates(cmd) + return cmd +}