cmdutil

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: Apache-2.0 Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cmdutil provides reusable CLI helper functions for Cobra commands. Both the open-source CLI and private overlays import this package to avoid duplicating flag validation, time parsing, and UX helpers.

Index

Constants

This section is empty.

Variables

View Source
var CommonFlagAliases = map[string]string{
	"json":            "format json",
	"output":          "format",
	"out":             "format",
	"o":               "format",
	"silent":          "quiet",
	"dry":             "dry-run",
	"force":           "yes",
	"f":               "yes",
	"timeout-seconds": "timeout",
	"device-flow":     "device",
	"deviceflow":      "device",
}

CommonFlagAliases maps commonly misused flag names to their correct equivalents.

View Source
var FlexTimeLayouts = []string{
	time.RFC3339,
	"2006-01-02T15:04:05Z",
	"2006-01-02T15:04:05-07:00",
	"2006-01-02T15:04:05",
	"2006-01-02 15:04:05",
	"2006-01-02T15:04",
	"2006-01-02 15:04",
	"2006-01-02",
	"2006/01/02 15:04:05",
	"2006/01/02",
	"20060102",
}

FlexTimeLayouts is the ordered list of time formats tried by ParseISOTimeToMillis.

Functions

func ConfirmDelete

func ConfirmDelete(cmd *cobra.Command, resourceType, resourceName string) bool

ConfirmDelete asks for interactive confirmation before destructive operations. Returns true if --yes/-y flag is set or the user types "yes"/"y".

func DetectNumericTypeError

func DetectNumericTypeError(err error) (flagName, badValue string, ok bool)

DetectNumericTypeError checks if err is a Cobra/pflag numeric type validation error. Returns the flag name and the bad value if detected.

func FlagOrFallback

func FlagOrFallback(cmd *cobra.Command, primary string, aliases ...string) string

FlagOrFallback reads the primary flag; if empty, falls back through alias flags in order, returning the first non-empty value.

func GroupRunE

func GroupRunE(cmd *cobra.Command, args []string) error

GroupRunE is a reusable RunE for parent (group) commands that have no business logic of their own. With args it returns an error listing available subcommands; without args it shows help.

func HintSubCmd

func HintSubCmd(use, hint string) *cobra.Command

HintSubCmd creates a hidden subcommand that only prints a disambiguation hint.

func LevenshteinDist

func LevenshteinDist(a, b string) int

LevenshteinDist returns the edit distance between two strings.

func LevenshteinThreshold

func LevenshteinThreshold(nameLen int) int

LevenshteinThreshold returns the max edit distance allowed based on string length.

func MustFlagOrFallback

func MustFlagOrFallback(cmd *cobra.Command, primary string, aliases ...string) (string, error)

MustFlagOrFallback works like FlagOrFallback but returns an error when all flags are empty.

func MustFlagWithHint

func MustFlagWithHint(cmd *cobra.Command, name, example string) (string, error)

MustFlagWithHint returns an error with an explicit usage example when the flag is empty.

func MustGetFlag

func MustGetFlag(cmd *cobra.Command, name string) string

MustGetFlag retrieves a string flag value, checking both local and inherited flags.

func ParseISOTimeToMillis

func ParseISOTimeToMillis(flagName, value string) (int64, error)

ParseISOTimeToMillis parses a time string into a millisecond Unix timestamp. Supports RFC3339, UTC Z, timezone-less, space-separated, date-only, and more. When the input lacks an explicit timezone, Asia/Shanghai is assumed.

func ValidateRequiredFlagWithAliases

func ValidateRequiredFlagWithAliases(cmd *cobra.Command, primary string, aliases ...string) error

ValidateRequiredFlagWithAliases checks that at least one of the primary flag or its aliases is non-empty.

func ValidateRequiredFlags

func ValidateRequiredFlags(cmd *cobra.Command, names ...string) error

ValidateRequiredFlags checks that all named string flags are non-empty. Returns a formatted error listing all missing flags, or nil.

func ValidateTimeRange

func ValidateTimeRange(startMs, endMs int64) error

ValidateTimeRange checks that endMs is strictly after startMs.

Types

type FlagFixResult

type FlagFixResult struct {
	Suggestion   string
	AutoFixFlag  string
	AutoFixValue string
}

FlagFixResult holds the result of SuggestFlagFix analysis.

func SuggestFlagFix

func SuggestFlagFix(cmd *cobra.Command, flagErr error) FlagFixResult

SuggestFlagFix detects flag-value concatenation errors, common flag aliases, and Levenshtein-close typos.

Jump to

Keyboard shortcuts

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