format

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2025 License: Unlicense Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindStructFlags added in v0.1.0

func BindStructFlags[T any](cmd *cobra.Command, opts *T) error

BindStructFlags automatically binds struct fields to Cobra command-line flags.

The function uses reflection to iterate over exported fields of opts, creating a flag for each one. Fields must be exported (start with capital letter) to be considered for flag binding.

Flag naming follows these rules: - Use the struct tag `flagname:"custom-name"` to override the default name (WIP?) - Default flag name converts Go field name to kebab-case ("MyField" → "my-field")

Flag usage is determined by: - Use the struct tag `flag:"description"` to set help text - Falls back to generated usage based on field type and name if not provided

Supported field types: int, string (bool, float64, etc. can be added to switch)

Parameters: - cmd: The Cobra command to add flags to - opts: Pointer to struct whose fields become flags. Must be pointer to struct.

Returns error if: - opts is not a pointer to struct - Unsupported field type is encountered

Example:

```go
type Options struct {
		Debug       bool   `flag:"Enable debug logging"`
		MaxItems    int    `flagname:"max-items" flag:"Maximum items to process"`
		OutputFile  string
}
var opts Options
BindStructFlags(cmd, &opts)
```

func CamelCase added in v0.1.0

func CamelCase(s string) string

CamelCase converts a string to camelCase, handling various cases. It first checks if the string is entirely uppercase and, if so, converts it to lowercase. Then, it processes the string to create camelCase by capitalizing letters that follow a space, underscore, or hyphen, and lowercasing the very first letter.

func Format

func Format(f FormatType, o ...any)

Format formats the output according to the given format.

func HelpFmt

func HelpFmt[T any](a *T) string

func OutputFmtFlagUsage

func OutputFmtFlagUsage(allowedFormats []FormatType) string

allowedFormats := []f.FormatType{f.FormatJSON} format := apiCmd.Flag("format")

if format == nil {
	panic("FORMAT")
}

format.Usage = "output format. One of: "+f.HelpFmt(&allowedFormats)

func StructToURLValues added in v0.1.0

func StructToURLValues(data interface{}) (url.Values, error)

StructToURLValues converts a struct to a url.Values map based on its json tags. This allows you to easily serialize a struct into URL query parameters.

func UpdateUsageAndAssertContains

func UpdateUsageAndAssertContains(o *pflag.Flag, allowedFormats []FormatType) error

o outputFormat updates o.Usage and validates..

func ValidateFlags

func ValidateFlags[T comparable](rules map[string][]T) func(*cobra.Command, []string) error

ValidateFlags returns a PersistentPreRunE that validates flag values against pre-defined sets of allowed values. It uses generics so the allowed slice can be any comparable type (string, int, custom enums...).

func WriteJSON

func WriteJSON(v any, w io.Writer)

TODO: try to unwrap

func WriteLogFmt added in v0.1.0

func WriteLogFmt(v any, w io.Writer)

Types

type CobraPFlagParams

type CobraPFlagParams struct {
	// P is the pointer to the string variable that will hold the flag's value.
	P *string
	// Name is the long name of the flag.
	Name string
	// Shorthand is the single‑character abbreviation.
	Shorthand string
	// Value is the default value for the flag.
	Value string
	// Usage describes the purpose of the flag.
	Usage string
}

TODO: move this somewhere else misses flag set

type FormatType

type FormatType string

FormatType defines the allowed formats for the Format function. it implements pflag.VarP

const (
	FormatJSON   FormatType = "json"
	FormatLogFmt FormatType = "logfmt"
)

func (*FormatType) Set

func (f *FormatType) Set(val string) error

Set implements pflag.Value. It validates and sets the format.

func (*FormatType) String

func (f *FormatType) String() string

String implements pflag.Value.

func (*FormatType) Type

func (f *FormatType) Type() string

Type implements pflag.Value (required by cobra/pflag for help output).

Jump to

Keyboard shortcuts

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