cliformat

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 26, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package cliformat provides a shared --format flag and output writer for Go CLI tools. Supported formats: name, yaml, json.

Usage:

cliformat.AddFlag(cmd, "name")         // register --format on a cobra command
format := cliformat.GetFormat(cmd)     // read the resolved value
cliformat.WriteList(w, format, items, func(d *MyType) string { return d.Name })

Index

Constants

This section is empty.

Variables

View Source
var ValidFormats = []string{"name", "yaml", "json"}

ValidFormats is the set of formats supported by WriteList.

Functions

func AddFlag

func AddFlag(cmd *cobra.Command, defaultFormat string)

AddFlag registers a --format flag on cmd with the given default value. The flag description lists all ValidFormats.

func GetFormat

func GetFormat(cmd *cobra.Command) string

GetFormat returns the resolved --format flag value from cmd (lower-cased). Returns an empty string if the flag was not registered.

func WriteList

func WriteList[T any](w io.Writer, format string, items []T, nameOf func(T) string) error

WriteList renders items to w in the requested format.

  • "name" (or ""): one name per line, using nameOf to extract each name.
  • "yaml": YAML document stream, one --- block per item.
  • "json": JSON array.

T may be any type — struct, map, pointer, etc.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL