cli

package
v0.0.0-...-7f9a76f Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: BSD-3-Clause Imports: 9 Imported by: 2

Documentation

Overview

Package cli provides Cobra CLI helpers for Valksor tools.

This package provides common patterns for building CLI tools with Cobra, including standard flags, version information, command grouping, and user interaction helpers.

Basic usage:

var rootCmd = &cobra.Command{
    Use:   "mytool",
    Short: "My tool description",
}

cli.SetupRootCmd(rootCmd, cli.RootOptions{
    ToolName: "mytool",
    VersionInfo: func() string { return version.Info("mytool") },
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddCommandGroup

func AddCommandGroup(cmd *cobra.Command, groupID, title string)

AddCommandGroup adds a command group to the given command.

func AddCommandGroups

func AddCommandGroups(rootCmd *cobra.Command, groups ...CommandGroup)

AddCommandGroups adds multiple command groups to the root command. Each group includes the group definition and assigns commands to it.

func AddStandardFlags

func AddStandardFlags(cmd *cobra.Command) *pflag.FlagSet

AddStandardFlags adds the standard persistent flags (--verbose, --quiet, --no-color). Returns the flag set for further customization if needed.

func ConfirmAction

func ConfirmAction(prompt string, skipConfirm bool) (bool, error)

ConfirmAction prompts the user for confirmation with a yes/no prompt. If skipConfirm is true, it returns true immediately without prompting. Returns true if the user confirms (responds with "y" or "yes"), false otherwise.

Example usage:

if confirmed, err := cli.ConfirmAction("This will delete all files", yesFlag); err != nil {
    return err
}
if !confirmed {
    fmt.Println("Operation cancelled")
    return nil
}

func NewVersionCommand

func NewVersionCommand(appName string) *cobra.Command

NewVersionCommand creates a version command for the given app name.

func SetCommandGroup

func SetCommandGroup(cmd *cobra.Command, groupID string)

SetCommandGroup assigns a command to a group. This is a convenience wrapper around cobra.Command's GroupID field.

func SetupRootCmd

func SetupRootCmd(cmd *cobra.Command, opts RootOptions)

SetupRootCmd configures a root command with standard settings.

Types

type CommandGroup

type CommandGroup struct {
	ID       string
	Title    string
	Commands []*cobra.Command
}

CommandGroup represents a group of related commands.

type RootOptions

type RootOptions struct {
	// ToolName is the name of the CLI tool.
	ToolName string
	// VersionInfo returns formatted version information.
	VersionInfo func() string
	// PersistentPreRun is called before each command.
	// If nil, a default implementation is used that configures logging and colors.
	PersistentPreRun func(cmd *cobra.Command, args []string) error
	// PreRunHook is called before the default PersistentPreRun logic.
	// Use this for custom initialization that needs to happen before logging/colors setup.
	PreRunHook func() error
	// SilenceErrors silences error messages (errors are handled by the caller).
	SilenceErrors bool
	// SilenceUsage silences usage printing on error.
	SilenceUsage bool
}

RootOptions configures the root command setup.

type StandardFlags

type StandardFlags struct {
	Verbose bool
	Quiet   bool
	NoColor bool
}

StandardFlags holds the standard persistent flag values.

func GetStandardFlags

func GetStandardFlags() *StandardFlags

GetStandardFlags returns the current values of standard flags.

Directories

Path Synopsis
Package disambiguate provides Symfony-style command prefix matching and colon notation for Cobra CLI applications.
Package disambiguate provides Symfony-style command prefix matching and colon notation for Cobra CLI applications.

Jump to

Keyboard shortcuts

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