xcli

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GlobalTypeRegistry = map[xcliconstt.FlagType]xcliintfc.ValueParser{

	xcliconstt.TypeString: TypedDescriptor[string]{
		Parser:         xclifn.ParseString,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeBool: TypedDescriptor[bool]{
		Parser: xclifn.ParseBool,
	},
	xcliconstt.TypeInt: TypedDescriptor[int]{
		Parser:         xclifn.ParseInt,
		LimitValidator: xclifn.ValidateNumberLimits[int],
	},
	xcliconstt.TypeFloat: TypedDescriptor[float64]{
		Parser:         xclifn.ParseFloat,
		LimitValidator: xclifn.ValidateNumberLimits[float64],
	},

	xcliconstt.TypeJSON: TypedDescriptor[string]{
		Parser:         xclifn.ParseJSON,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeDuration: TypedDescriptor[time.Duration]{
		Parser:         xclifn.ParseDuration,
		LimitValidator: xclifn.ValidateNumberLimits[time.Duration],
	},
	xcliconstt.TypeDate: TypedDescriptor[time.Time]{
		Parser:         xclifn.ParseDate,
		LimitValidator: xclifn.ValidateNumberLimits[time.Time],
	},
	xcliconstt.TypeTime: TypedDescriptor[time.Time]{
		Parser:         xclifn.ParseTime,
		LimitValidator: xclifn.ValidateNumberLimits[time.Time],
	},
	xcliconstt.TypeDateTime: TypedDescriptor[time.Time]{
		Parser:         xclifn.ParseDateTime,
		LimitValidator: xclifn.ValidateNumberLimits[time.Time],
	},
	xcliconstt.TypeEmail: TypedDescriptor[string]{
		Parser:         xclifn.ParseEmail,
		LimitValidator: xclifn.ValidateStringLimits,
	},

	xcliconstt.TypePath: TypedDescriptor[string]{
		Parser:         xclifn.ParsePath,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeFilename: TypedDescriptor[string]{
		Parser:         xclifn.ParseFilename,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeDirname: TypedDescriptor[string]{
		Parser:         xclifn.ParseDirname,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeURL: TypedDescriptor[string]{
		Parser:         xclifn.ParseURLStandard,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeFullURL: TypedDescriptor[string]{
		Parser:         xclifn.ParseFullURL,
		LimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeRelativeURL: TypedDescriptor[string]{
		Parser:         xclifn.ParseRelativeURL,
		LimitValidator: xclifn.ValidateStringLimits,
	},

	xcliconstt.TypeFilepath: TypedDescriptor[string]{
		Parser:        xclifn.ParseFilepath,
		DiskValidator: xclifn.ValidateDiskResources,
	},
	xcliconstt.TypeDirpath: TypedDescriptor[string]{
		Parser:        xclifn.ParseDirpath,
		DiskValidator: xclifn.ValidateDiskResources,
	},

	xcliconstt.TypeStringArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseString,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeIntArray: ArrayDescriptor[int]{
		ElementParser:      xclifn.ParseInt,
		ItemLimitValidator: xclifn.ValidateNumberLimits[int],
	},
	xcliconstt.TypeFloatArray: ArrayDescriptor[float64]{
		ElementParser:      xclifn.ParseFloat,
		ItemLimitValidator: xclifn.ValidateNumberLimits[float64],
	},
	xcliconstt.TypeBoolArray: ArrayDescriptor[bool]{
		ElementParser: xclifn.ParseBool,
	},
	xcliconstt.TypeDurationArray: ArrayDescriptor[time.Duration]{
		ElementParser:      xclifn.ParseDuration,
		ItemLimitValidator: xclifn.ValidateNumberLimits[time.Duration],
	},
	xcliconstt.TypeDateArray: ArrayDescriptor[time.Time]{
		ElementParser:      xclifn.ParseDate,
		ItemLimitValidator: xclifn.ValidateNumberLimits[time.Time],
	},
	xcliconstt.TypeTimeArray: ArrayDescriptor[time.Time]{
		ElementParser:      xclifn.ParseTime,
		ItemLimitValidator: xclifn.ValidateNumberLimits[time.Time],
	},
	xcliconstt.TypeDateTimeArray: ArrayDescriptor[time.Time]{
		ElementParser:      xclifn.ParseDateTime,
		ItemLimitValidator: xclifn.ValidateNumberLimits[time.Time],
	},
	xcliconstt.TypeEmailArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseEmail,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypePathArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParsePath,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeFilenameArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseFilename,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeFilepathArray: ArrayDescriptor[string]{
		ElementParser:     xclifn.ParseFilepath,
		ItemDiskValidator: xclifn.ValidateDiskResources,
	},
	xcliconstt.TypeDirnameArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseDirname,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeDirpathArray: ArrayDescriptor[string]{
		ElementParser:     xclifn.ParseDirpath,
		ItemDiskValidator: xclifn.ValidateDiskResources,
	},
	xcliconstt.TypeURLArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseURLStandard,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeFullURLArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseFullURL,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
	xcliconstt.TypeRelativeURLArray: ArrayDescriptor[string]{
		ElementParser:      xclifn.ParseRelativeURL,
		ItemLimitValidator: xclifn.ValidateStringLimits,
	},
}

GlobalTypeRegistry acts as the immutable central directory mapping every supported FlagType constant to its respective type-safe conversion and validation engine instance.

Functions

This section is empty.

Types

type ArrayDescriptor

type ArrayDescriptor[T any] struct {
	// ElementParser references the single item parser function pointer.
	ElementParser func(val string) (T, error)

	// Optional item-level limit validator hook applied to every slice element.
	ItemLimitValidator func(flag string, val T, spec xclistruc.Flag) error

	// Optional item-level physical disk validator hook applied to every slice element.
	ItemDiskValidator func(flag string, val T, spec xclistruc.Flag) error
}

ArrayDescriptor implements the xcliintfc.ValueParser interface, orchestrating tokenizer splitting, iterative primitive parsing, and capacity validation for slices.

func (ArrayDescriptor[T]) ParseAndValidate

func (ad ArrayDescriptor[T]) ParseAndValidate(flag string, raw string, spec xclistruc.Flag) (any, error)

ParseAndValidate orchestrates JSON matrix deserialization over array flag inputs.

It decodes structured terminal string blocks using native encoding capabilities, validates total collection capacities metrics, and iteratively executes multi-layered domain limits and physical validation restrictions hooks over every deserialized item instance.

Arguments:

  • flag: The command line flag identifier label causing the failure (e.g., "--ids").
  • raw: The raw unparsed text payload extracted from the command arguments collection.
  • spec: The complete core flag layout containing the validation metadata guidelines.

Returns:

  • any: The fully converted, type-safe native Go slice matching the target element layout ([]T).
  • error: Returns an xerrors.IErrorCLI capturing visual telemetry diagnostics if any rule fails.

Error & Panic Natures:

  • Complex Errors: Fails early if the raw input structure violates strict JSON formatting guidelines. Evaluates high-level collection capacity boundary lengths via custom helpers. Iterates over deserialized items, throwing formatted CLI errors if an item-level constraint or a physical disk permission assertion fails evaluation targets.

func (ArrayDescriptor[T]) ValidateCapacity

func (ad ArrayDescriptor[T]) ValidateCapacity(flag string, total int, spec xclistruc.Flag) error

validateCapacity enforces slice size limits against spec guidelines without reflection.

It delegates collection item metrics checking directly to the specialized core validation package layers to isolate error visual formatting structures.

Arguments:

  • flag: The command line flag identifier label causing the failure (e.g., "--ids").
  • total: The total number of items parsed and identified inside the active slice collection.
  • spec: The complete core flag layout containing the validation metadata guidelines.

Returns:

  • error: Returns an xerrors.IErrorCLI if the count violates configured boundaries.

Error & Panic Natures:

  • Complex Errors: Routes length counters down to the package validation infrastructure, triggering a multi-line visual terminal error layout if minItems or maxItems are violated.

type Command

type Command struct {
	// Name is the string that triggers this command in the terminal.
	Name string

	// ShortDescription is a brief one-line summary used in general help listings.
	ShortDescription string

	// LongDescription is a detailed explanation shown when help is requested
	// specifically for this command. If left empty, ShortDescription will be used.
	LongDescription string

	// Flags is the list of exclusive options accepted strictly by this command.
	Flags []xclistruc.Flag

	// Subcommands holds the next layer of commands, indexed by their execution Name.
	// Due to context isolation, children do not inherit flags from their parents.
	Subcommands map[string]*Command

	// Run is the execution hook containing the command's business logic.
	// It receives the Context containing all parsed, typed, and validated flags.
	Run func(ctx *xclistruc.FlagValues) error
}

Command represents a node in the CLI command tree. Each command forms an isolated scope and executes its own business logic.

func (*Command) TriggerHelp

func (c *Command) TriggerHelp() error

TriggerHelp intercepts the flow and renders the automatic command documentation.

It evaluates operational descriptions metadata fields and directly serializes aligned visual tables mapping usage definitions, child subcommands, and flags guidelines to standard stdout tracks.

Returns:

  • error: Returns a structured error tracking instance if writing stdout triggers telemetry blocks.

func (*Command) ValidateAndHydrateFlags

func (c *Command) ValidateAndHydrateFlags(
	rawFlags map[string]string,
) (
	*xclistruc.FlagValues,
	error,
)

ValidateAndHydrateFlags loops through registered constraints performing types translation and bounds enforcement.

It checks flag mandatory presence, injects fallback defaults allocation blocks, and leverages the package-level central registry directory to resolve runtime type translations dynamically.

Arguments:

  • rawFlags: The extracted command line layout map pairing flag strings to text values.

Returns:

  • *xclistruc.FlagValues: A populated type-safe context directory containing translated Go instances.
  • error: Returns an xerrors.IErrorCLI capturing visual telemetry diagnostics if any validation path fails.

Error & Panic Natures:

  • Complex Errors: Fails immediately if a required token is missing from raw inputs. Queries the global type registry throwing errors if a type metadata token is unmapped. Performs zombie variable tracking blocks, failing if unregistered flag tokens are found.

type Router

type Router struct {
	// Root is the entry point of the CLI application.
	Root *Command
}

Router manages the CLI lifecycle by registering the root command, navigating the command tree, and triggering argument parsing and validation.

func NewRouter

func NewRouter(root *Command) *Router

NewRouter initializes a new CLI router with a defined root command.

func (*Router) Run

func (r *Router) Run(rawArgs []string) error

Run reads the raw terminal arguments, resolves the command path, validates the flags, checks for help triggers, and executes the final command hook.

Arguments:

  • rawArgs: The slice of strings representing terminal inputs (usually os.Args[1:]).

Returns:

  • error: Returns an xerrors.IErrorCLI if a command is not found or flag parsing/validation fails.

type TypedDescriptor

type TypedDescriptor[T any] struct {
	// Parser conversions primitive engine pointer.
	Parser func(val string) (T, error)

	// Optional specific limit validator hook.
	LimitValidator func(flag string, val T, spec xclistruc.Flag) error

	// Optional structural disk capability validator hook.
	DiskValidator func(flag string, val T, spec xclistruc.Flag) error
}

TypedDescriptor implements the xcliintfc.ValueParser interface, orchestrating type conversion and multi-layered validation tracks for a single primitive type.

func (TypedDescriptor[T]) ParseAndValidate

func (td TypedDescriptor[T]) ParseAndValidate(
	flag string,
	raw string,
	spec xclistruc.Flag,
) (any, error)

ParseAndValidate orchestrates the complete strongly-typed pipeline for single terminal values.

It handles primitive mapping, standard constraint limits enforcement, and specialized physical file system privilege validation tracks using compile-time type dispatch.

Arguments:

  • flag: The command line flag identifier label causing the failure (e.g., "--output").
  • raw: The raw unparsed text payload extracted from the command arguments collection.
  • spec: The complete core flag layout containing the validation metadata guidelines.

Returns:

  • any: The fully converted, type-safe native Go primitive instance.
  • error: Returns an xerrors.IErrorCLI capturing visual telemetry diagnostics if any rule fails.

Error & Panic Natures:

  • Complex Errors: Evaluates sequential pipeline layers. Fails early if the core type parser throws an execution blocker. Sequentially triggers LimitValidator and DiskValidator bounds hooks only if they are actively configured inside the global registration matrix.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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