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 ¶
- func AddCommandGroup(cmd *cobra.Command, groupID, title string)
- func AddCommandGroups(rootCmd *cobra.Command, groups ...CommandGroup)
- func AddStandardFlags(cmd *cobra.Command) *pflag.FlagSet
- func ConfirmAction(prompt string, skipConfirm bool) (bool, error)
- func NewVersionCommand(appName string) *cobra.Command
- func SetCommandGroup(cmd *cobra.Command, groupID string)
- func SetupRootCmd(cmd *cobra.Command, opts RootOptions)
- type CommandGroup
- type RootOptions
- type StandardFlags
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddCommandGroup ¶
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 ¶
AddStandardFlags adds the standard persistent flags (--verbose, --quiet, --no-color). Returns the flag set for further customization if needed.
func ConfirmAction ¶
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 ¶
NewVersionCommand creates a version command for the given app name.
func SetCommandGroup ¶
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 ¶
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 ¶
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. |