From f06db58e8cade98c42171646da9eaf47b6b8255d Mon Sep 17 00:00:00 2001 From: Feruzjon Muyassarov Date: Wed, 29 Oct 2025 14:31:55 +0200 Subject: [PATCH] client: add examples, short flags and better error handling - Add usage examples to export and compat commands - Add `-p` short flag for `--path` option - Silence usage output on runtime errors for cleaner error messages Signed-off-by: Feruzjon Muyassarov --- cmd/nfd/subcmd/compat/validate-node.go | 9 +++++++-- cmd/nfd/subcmd/export/features.go | 16 +++++++++++++--- cmd/nfd/subcmd/export/labels.go | 16 +++++++++++++--- cmd/nfd/subcmd/root.go | 1 + 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cmd/nfd/subcmd/compat/validate-node.go b/cmd/nfd/subcmd/compat/validate-node.go index a1a3abc30f..35e7a0243e 100644 --- a/cmd/nfd/subcmd/compat/validate-node.go +++ b/cmd/nfd/subcmd/compat/validate-node.go @@ -49,11 +49,16 @@ var ( username string password string accessToken string + + validateExample = ` +# Validate image compatibility +nfd compat validate-node --image ` ) var validateNodeCmd = &cobra.Command{ - Use: "validate-node", - Short: "Perform node validation based on its associated image compatibility artifact", + Use: "validate-node", + Short: "Perform node validation based on its associated image compatibility artifact", + Example: validateExample, PreRunE: func(cmd *cobra.Command, args []string) error { var err error diff --git a/cmd/nfd/subcmd/export/features.go b/cmd/nfd/subcmd/export/features.go index 8085012ed7..8f838eedfc 100644 --- a/cmd/nfd/subcmd/export/features.go +++ b/cmd/nfd/subcmd/export/features.go @@ -25,10 +25,20 @@ import ( "sigs.k8s.io/node-feature-discovery/source" ) +var ( + exportFeaturesExample = ` +# Export node features to stdout (prints to terminal) +nfd export features + +# Export node features to a file instead +nfd export features --path /tmp/features.json` +) + func NewExportCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "features", - Short: "Export features for given node", + Use: "features", + Short: "Export features for given node", + Example: exportFeaturesExample, RunE: func(cmd *cobra.Command, args []string) error { sources := map[string]source.FeatureSource{} for k, v := range source.GetAllFeatureSources() { @@ -59,7 +69,7 @@ func NewExportCmd() *cobra.Command { return err }, } - cmd.Flags().StringVar(&outputPath, "path", "", "export to this JSON path") + cmd.Flags().StringVarP(&outputPath, "path", "p", "", "export to this JSON path") return cmd } diff --git a/cmd/nfd/subcmd/export/labels.go b/cmd/nfd/subcmd/export/labels.go index 16d36d1611..bcaadedcce 100644 --- a/cmd/nfd/subcmd/export/labels.go +++ b/cmd/nfd/subcmd/export/labels.go @@ -31,10 +31,20 @@ import ( "sigs.k8s.io/node-feature-discovery/source" ) +var ( + exportLabelsExample = ` +# Export node labels to stdout (prints to terminal) +nfd export features + +# Export node labels to a file instead +nfd export features --path /tmp/labels.json` +) + func NewLabelsCmd() *cobra.Command { cmd := &cobra.Command{ - Use: "labels", - Short: "Export feature labels for given node", + Use: "labels", + Short: "Export feature labels for given node", + Example: exportLabelsExample, RunE: func(cmd *cobra.Command, args []string) error { // Determine enabled feature sources @@ -93,7 +103,7 @@ func NewLabelsCmd() *cobra.Command { return err }, } - cmd.Flags().StringVar(&outputPath, "path", "", "export to this JSON path") + cmd.Flags().StringVarP(&outputPath, "path", "p", "", "export to this JSON path") return cmd } diff --git a/cmd/nfd/subcmd/root.go b/cmd/nfd/subcmd/root.go index e87c0f05bd..4c563f38ec 100644 --- a/cmd/nfd/subcmd/root.go +++ b/cmd/nfd/subcmd/root.go @@ -35,6 +35,7 @@ var RootCmd = &cobra.Command{ func init() { RootCmd.AddCommand(compat.CompatCmd) RootCmd.AddCommand(export.ExportCmd) + RootCmd.SilenceUsage = true } // Execute adds all child commands to the root command and sets flags appropriately.