cli

package
v0.0.0-...-2da20e2 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cli provides a lightweight CLI framework with support for positional arguments, named parameters, and flags.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Confirm

func Confirm(questionOpt ...string) (ok bool)

Confirm asks for y/n confirmation from the user via stdin. It returns true if the user enters 'y' or 'yes' (case-insensitive).

func Err

func Err(s string, args ...any)

Err prints an error message to stdout in red color. Used to highlight errors or critical problems for the user.

func Gray

func Gray(s string, args ...any) string

Gray returns a string formatted in gray color.

func Info

func Info(s string, args ...any)

Info prints an informational message to stdout in blue color. Intended for neutral, non-error messages (status, progress, hints).

func OK

func OK(s string, args ...any)

OK prints a success message prefixed with a checkmark. Used to indicate successful completion of an operation.

Types

type App

type App struct {
	Name string
	Env  string
	// contains filtered or unexported fields
}

App represents the main CLI application structure.

func NewApp

func NewApp(name, env string) *App

NewApp creates a new CLI application instance with the given name and environment.

func (*App) PromptOrRun

func (a *App) PromptOrRun()

PromptOrRun executes the CLI either from provided command-line arguments or starts interactive mode. If arguments are provided (beyond the binary name), it runs the specified command. Otherwise, it enters the interactive mode.

func (*App) Register

func (a *App) Register(cs ...Command) error

Register adds one or more commands to the application.

func (*App) Run

func (a *App) Run(name string, args ...string) error

Run executes a command by name with the given arguments.

func (*App) WaitForCommand

func (a *App) WaitForCommand()

WaitForCommand starts the interactive CLI loop waiting for user input.

type Command

type Command struct {
	Name  string
	Alias string
	Help  []string

	Handler Handler
	// contains filtered or unexported fields
}

Command represents a CLI command configuration.

func NewSampleCommandWithFeaturesCmd

func NewSampleCommandWithFeaturesCmd() Command

NewSampleCommandWithFeaturesCmd returns a sample command that demonstrates: - positional arguments - named options (-key=value) - boolean flags

Example (without optional arguments):

htf my-proj PROD -token=do-not-hack-me -vars='foo="bar",bar="beer"'

func NewSampleCommandWithFlagsCmd

func NewSampleCommandWithFlagsCmd() Command

NewSampleCommandWithFlagsCmd creates a new command instance demonstrating flag usage. Usage: how_to_flags [env] [-v] [-y].

func NewSampleCommandWithNamedParamsCmd

func NewSampleCommandWithNamedParamsCmd() Command

NewSampleCommandWithNamedParamsCmd creates a new command instance demonstrating named parameters.

func NewSampleCommandWithSignatureCmd

func NewSampleCommandWithSignatureCmd() Command

NewSampleCommandWithSignatureCmd creates a new command instance for demonstrating signature-based commands.

type Handler

type Handler interface {
	Handle(ctx context.Context) error
}

Handler defines the interface for command execution logic.

type SampleCommandWithFeaturesCmd

type SampleCommandWithFeaturesCmd struct {

	// Project is a required positional argument.
	Project string `arg:"project"`

	// Env is an optional positional argument (default: STAGING).
	Env *string `arg:"env" default:"STAGING"`

	// Named options: provided as {name}={value}.
	// Unlike positional arguments, they can appear in any order.
	AuthToken string   `arg:"-token"` //nolint:gosec
	TmpPath   *string  `arg:"-tmp-path" default:"/tmp/"`
	Variables []string `arg:"-vars"`
	Timeout   *int     `arg:"-t" default:"300"`

	// Flags: simple switches. If a flag is present, its value becomes true.
	// They can appear in any order.
	Verbose bool `arg:"-v"`
	Confirm bool `arg:"-y"`
}

SampleCommandWithFeaturesCmd demonstrates a complex command with various argument types.

func (*SampleCommandWithFeaturesCmd) Handle

func (cmd *SampleCommandWithFeaturesCmd) Handle(_ context.Context) (err error)

Handle executes the command logic.

type SampleCommandWithFlagsCmd

type SampleCommandWithFlagsCmd struct {
	Env     string `arg:"env" default:"STAGING"`
	Verbose bool   `arg:"-v"`
	Confirm bool   `arg:"-y"`
}

SampleCommandWithFlagsCmd demonstrates a command with boolean flags.

func (*SampleCommandWithFlagsCmd) Handle

func (c *SampleCommandWithFlagsCmd) Handle(_ context.Context) (err error)

Handle executes the command logic.

type SampleCommandWithNamedParamsCmd

type SampleCommandWithNamedParamsCmd struct {
	Name string  `arg:"name"`
	Mood *string `arg:"-m"`
	Age  *int    `arg:"-a"`
}

SampleCommandWithNamedParamsCmd demonstrates a command with named parameters.

func (*SampleCommandWithNamedParamsCmd) Handle

Handle executes the command logic.

type SampleCommandWithSignatureCmd

type SampleCommandWithSignatureCmd struct {
	Name string  `arg:"name"`
	Mood *string `arg:"mood" default:"Happy"`
	Age  *int    `arg:"age"`
}

SampleCommandWithSignatureCmd demonstrates a command with positional arguments.

func (*SampleCommandWithSignatureCmd) Handle

func (cmd *SampleCommandWithSignatureCmd) Handle(_ context.Context) (err error)

Handle executes the command logic.

Jump to

Keyboard shortcuts

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