cling

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2025 License: MIT Imports: 13 Imported by: 0

README

cling

A Simple and Clear CLI library.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidCLIngConfig = errors.New("invalid CLIng configuration")
View Source
var ErrInvalidCommand = errors.New("invalid command")
View Source
var ErrStringLen = errors.New("string length is not in range")
View Source
var ErrValidatorFailed = errors.New("validation failed")

Functions

func Exit added in v0.3.5

func Exit(err error)

ExitWithErrorCode - exits the program with a non-zero exit code if the given error is non-nil. If the given error is an `ExitCoder`, the exit code will be taken from the error, otherwise it will be 1.

Uses `os.Exit` to exit the program. This function should be used only after when all cleanups are done.

func ExitWithMessage added in v0.3.5

func ExitWithMessage(err error)

ExitWithErrorMessage exits the program with a non-zero exit code if the given error is non-nil. If the given error is an `ExitCoder`, the exit code will be taken from the error, otherwise it will be 1. The error message will be printed to stderr as "Error: <message>\n".

Uses `os.Exit` to exit the program. This function should be used only after when all cleanups are done.

func Hydrate

func Hydrate[T any](ctx context.Context, argArguments []string, destination *T) error

Hydrate populates the destination struct based on command-line arguments and context.

func NoOpHook added in v0.3.0

func NoOpHook(ctx context.Context, args []string) error

func NoOpValidator added in v0.3.0

func NoOpValidator() validatorAny

NoOpValidator returns a no-op validator for any type

Types

type CLI

type CLI struct {
	// contains filtered or unexported fields
}

func NewCLI

func NewCLI(name string, version string) *CLI

NewCLI creates a new CLI

func (*CLI) Run

func (c *CLI) Run(ctx context.Context, args []string) error

Run executes the CLI with the given command line arguments.

func (*CLI) WithCommand added in v0.3.0

func (cli *CLI) WithCommand(command *Command) *CLI

WithCommand adds a command to the CLI

func (*CLI) WithDescription

func (cli *CLI) WithDescription(description string) *CLI

WithDescription sets the description of the CLI

func (*CLI) WithLongDescription

func (cli *CLI) WithLongDescription(longDescription string) *CLI

WithLongDescription sets the long description of the CLI

func (*CLI) WithPostRun added in v0.3.3

func (cli *CLI) WithPostRun(hook CommandHook) *CLI

WithPostRun sets the post-run hook for the CLI

func (*CLI) WithPreRun added in v0.3.3

func (cli *CLI) WithPreRun(hook CommandHook) *CLI

WithPreRun sets the pre-run hook for the CLI

type ClingContextKey added in v0.2.0

type ClingContextKey string
const (
	ContextKeyCommand ClingContextKey = "command"
)

type CmdArg added in v0.2.0

type CmdArg interface {
	CmdInput
	// WithLongDescription sets the long description of the command argument.
	WithLongDescription(string) CmdArg
	// contains filtered or unexported methods
}

type CmdFlag added in v0.2.0

type CmdFlag interface {
	CmdInput
	// FromEnv sets the environment sources of the command flag.
	// The environment sources are used to get the default value of the flag.
	// The default value is the first non-empty value found in the environment.
	// This will override the default value set by WithDefault.
	FromEnv([]string) CmdFlag
	// contains filtered or unexported methods
}

type CmdInput added in v0.2.0

type CmdInput interface {
	// Name returns the name of the command input.
	Name() string
	// Required marks the command input as required.
	Required() CmdInput
	// WithDescription sets the description of the command input.
	WithDescription(description string) CmdInput
	// Description returns the description of the command input.
	Description() string
	// AsFlag returns the command input as a flag.
	AsFlag() CmdFlag
	// AsArgument returns the command input as an argument.
	AsArgument() CmdArg
	// contains filtered or unexported methods
}

type CmdInputWithDefaultAndValidator added in v0.2.0

type CmdInputWithDefaultAndValidator[S any] interface {
	CmdInput
	ValidatorProvider
	// WithDefault sets the default value of the command input.
	WithDefault(value S) CmdInputWithDefaultAndValidator[S]
	// WithValidator sets the validator of the command input.
	WithValidator(validator Validator[S]) CmdInputWithDefaultAndValidator[S]
}

func NewBoolCmdInput added in v0.3.4

func NewBoolCmdInput(name string) CmdInputWithDefaultAndValidator[bool]

NewBoolCmdInput creates a new boolean command input with the given name.

func NewCmdSliceInput added in v0.3.0

func NewCmdSliceInput[T comparable](name string) CmdInputWithDefaultAndValidator[[]T]

func NewIntCmdInput added in v0.2.0

func NewIntCmdInput(name string) CmdInputWithDefaultAndValidator[int]

NewIntCmdInput creates a new integer command input with the given name.

func NewStringCmdInput added in v0.2.0

func NewStringCmdInput(name string) CmdInputWithDefaultAndValidator[string]

NewStringCmdInput creates a new string command input with the given name.

type Command

type Command struct {
	// contains filtered or unexported fields
}

func NewCommand

func NewCommand(name string, action CommandHandler) *Command

func (*Command) WithArgument added in v0.2.0

func (command *Command) WithArgument(arg CmdArg) *Command

func (*Command) WithChildCommand added in v0.2.0

func (command *Command) WithChildCommand(cmd *Command) *Command

func (*Command) WithDescription

func (c *Command) WithDescription(description string) *Command

func (*Command) WithFlag added in v0.2.0

func (command *Command) WithFlag(flag CmdFlag) *Command

func (*Command) WithLongDescription

func (c *Command) WithLongDescription(longDescription string) *Command

func (*Command) WithPersistentPostRun added in v0.3.0

func (c *Command) WithPersistentPostRun(hook CommandHook) *Command

func (*Command) WithPersistentPreRun added in v0.3.0

func (c *Command) WithPersistentPreRun(hook CommandHook) *Command

func (*Command) WithPostRun added in v0.3.0

func (c *Command) WithPostRun(hook CommandHook) *Command

func (*Command) WithPreRun added in v0.3.0

func (c *Command) WithPreRun(hook CommandHook) *Command

type CommandHandler

type CommandHandler func(ctx context.Context, args []string) error

type CommandHook added in v0.3.0

type CommandHook func(ctx context.Context, args []string) error

type Comparator added in v0.3.0

type Comparator[T any] func(a T) error

type ExitCoder added in v0.3.5

type ExitCoder interface {
	ExitCode() int
}

func NewExitCoder added in v0.3.5

func NewExitCoder(err error, code int) ExitCoder

type Validator added in v0.2.0

type Validator[T any] interface {
	Validate(value T) error
}

Validator interface with non-generic Validate method

func ComposeValidator added in v0.3.0

func ComposeValidator[T any](validators ...Validator[T]) Validator[T]

ComposeValidator composes multiple validators for a specific type

func NewComparatorValidator added in v0.3.0

func NewComparatorValidator[T any](comparator Comparator[T]) Validator[T]

NewComparatorValidator creates a new validator that checks if the value satisfies the comparator.

func NewEnumValidator added in v0.2.0

func NewEnumValidator[T comparable](allowedValues ...T) Validator[T]

NewEnumValidator creates a new validator that checks if the value is one of the allowed values.

func NewIntRangeValidator added in v0.2.0

func NewIntRangeValidator(min, max int) Validator[int]

func NewStringLengthValidator added in v0.2.0

func NewStringLengthValidator(min, max int) Validator[string]

type ValidatorProvider added in v0.3.0

type ValidatorProvider interface {
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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