diff --git a/cmd/base/flags.go b/cmd/base/flags.go index c989031..c780b10 100644 --- a/cmd/base/flags.go +++ b/cmd/base/flags.go @@ -9,6 +9,10 @@ func AddGlobalFlags(cmd *cobra.Command) { cmd.PersistentFlags().Int("http-timeout", 30, "HTTP timeout ( seconds )") cmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output") cmd.PersistentFlags().StringP("output", "o", "text", "output format (text/json/yaml)") + // define help flag without shorthand before cobra adds it by default to avoid conflict with no-header flag shorthand + cmd.PersistentFlags().Bool("help", false, "Print usage") + cmd.PersistentFlags().Lookup("help").Hidden = true + cmd.PersistentFlags().BoolP("no-header", "h", false, "print output without headers") } func AddFormatFlags(cmd *cobra.Command) { diff --git a/internal/output/formatter.go b/internal/output/formatter.go index b9d7ea2..1b7a2e4 100644 --- a/internal/output/formatter.go +++ b/internal/output/formatter.go @@ -19,6 +19,7 @@ type Formatter struct { pageView bool fieldsToShow []string fieldList bool + header bool } // NewFormatter creates new formatter with specified io.Writer @@ -28,6 +29,7 @@ func NewFormatter(cmd *cobra.Command, manager *config.Manager) *Formatter { template, _ := manager.GetResolvedStringValue(cmd, "template") fields, _ := manager.GetResolvedStringSliceValue(cmd, "field") fieldList, _ := manager.GetResolvedBoolValue(cmd, "field-list") + noHeader, _ := manager.GetResolvedBoolValue(cmd, "no-header") return &Formatter{ writer: cmd.OutOrStdout(), @@ -36,6 +38,7 @@ func NewFormatter(cmd *cobra.Command, manager *config.Manager) *Formatter { pageView: pageView, fieldsToShow: fields, fieldList: fieldList, + header: !noHeader, } } diff --git a/internal/output/utils.go b/internal/output/utils.go index dbd81bd..6fbf0d9 100644 --- a/internal/output/utils.go +++ b/internal/output/utils.go @@ -98,11 +98,13 @@ func (f *Formatter) formatText(v any) error { orderedFields := f.getOrderedFields(entity) - headers := make([]string, 0, len(orderedFields)) - for _, field := range orderedFields { - headers = append(headers, field.GetName()) + if f.header { + headers := make([]string, 0, len(orderedFields)) + for _, field := range orderedFields { + headers = append(headers, field.GetName()) + } + fmt.Fprintln(w, strings.Join(headers, "\t")) } - fmt.Fprintln(w, strings.Join(headers, "\t")) value := reflect.ValueOf(v) return processValue(value, func(item any) error {