Merge pull request #2283 from lonelyCZ/pr-options-cmd
karmadactl: Introduce options subcmd to list global command-line options
This commit is contained in:
commit
e153614477
|
@ -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 {
|
||||
|
|
|
@ -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...)
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue