pkg

package
v0.3.21 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package CnsPanel provides a structure and functions for handling console command flags.

Package CnsPanel provides a structure and functions for handling console command flags.

Package CnsPanel provides a structure and functions for handling console command flags.

Index

Constants

This section is empty.

Variables

View Source
var (
	ResultBranchSortFunc    uts.SortFunc[*resultBranch]
	FlagParseResultSortFunc uts.SortFunc[*FlagParseResult]
)

Functions

func NoArgumentParser

func NoArgumentParser(args []string) ([]any, error)

NoArgumentParser is a default argument parser function that returns the string as is.

Parameters:

  • string: The string to parse.

Returns:

  • any: The string as is.
  • error: nil

func NoCommandCallback

func NoCommandCallback(args map[string]map[string][]any) (any, error)

NoCommandCallback is a default callback function for a console command when no callback is provided.

Parameters:

  • args: A map of string keys to any values representing the arguments passed to the command.

Returns:

  • error: nil
  • any: nil

func NoFlagCallback

func NoFlagCallback(argMap map[string][]any) (map[string][]any, error)

NoFlagCallback is a default callback function for a console command flag when no callback is provided.

Parameters:

  • args: A slice of strings representing the arguments passed to the flag.

Returns:

  • map[string]any: A map of string keys to any values representing the parsed arguments.
  • error: nil

Types

type ArgInfo

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

ArgInfo represents an argument of a flag.

func NewArgument

func NewArgument(format string, fn ArgumentParserFunc) (*ArgInfo, error)

func (*ArgInfo) Equals

func (a *ArgInfo) Equals(other uc.Equaler) bool

Equals checks if the argument is equal to another argument.

Two arguments are equal iff their names are equal.

Parameters:

  • other: The other argument to compare.

Returns:

  • bool: true if the arguments are equal, false otherwise.

func (*ArgInfo) GetMinMax

func (a *ArgInfo) GetMinMax() (int, int)

func (*ArgInfo) GetName

func (a *ArgInfo) GetName() string

GetName returns the name of the argument.

Returns:

  • string: The name of the argument.

func (*ArgInfo) Parse

func (a *ArgInfo) Parse(args []string) ([]*resultArg, error)

Parse parses a string into the argument.

Parameters:

  • s: The string to parse.

Returns:

  • any: The parsed value.
  • error: An error if the parsing fails.

Errors:

  • The error returned is the error from the parser function.

func (*ArgInfo) String

func (a *ArgInfo) String() string

String returns the string: <name>

Returns:

  • string: The string representation of the argument.

type ArgumentParserFunc

type ArgumentParserFunc func(args []string) ([]any, error)

ArgumentParserFunc is a function type that represents a function that parses a string argument.

Parameters:

  • string: The string to parse.

Returns:

  • any: The parsed value.

type CommandCallbackFunc

type CommandCallbackFunc func(args map[string]map[string][]any) (any, error)

CommandCallbackFunc is a function type that represents a callback function for a console command.

Parameters:

  • args: A map of string keys to any values representing the arguments passed to the command.

Returns:

  • error: An error if the command fails.
  • any: The result of the command. (if any)

type CommandInfo

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

CommandInfo represents a console command.

func NewCommandInfo

func NewCommandInfo(name string, description []string, fn CommandCallbackFunc, flagInfos []*FlagInfo) (*CommandInfo, error)

NewCommandInfo creates a new CommandInfo with the provided command name and callback function.

Parameters:

  • name: The name of the command.
  • description: The description of the command.
  • callback: The function to call when the command is executed.

Returns:

  • *CommandInfo: A pointer to the new CommandInfo.

Behaviors:

  • If callback is nil, NoCommandCallback is used.

func (*CommandInfo) Equals

func (ci *CommandInfo) Equals(other uc.Equaler) bool

Equals checks if the command is equal to another command.

Two commands are equal iff their names are equal.

Parameters:

  • other: The other command to compare.

Returns:

  • bool: true if the commands are equal, false otherwise.

func (*CommandInfo) Evaluator

func (inf *CommandInfo) Evaluator() evalSlc.LeafEvaluater[string, *resultBranch, int, []*FlagParseResult]

Evaluator implements the Evaluable interface.

func (*CommandInfo) FString

func (cci *CommandInfo) FString(trav *ffs.Traversor, opts ...ffs.Option) error

FString generates a formatted string representation of a CommandInfo.

Parameters:

  • indentLevel: The level of indentation to use for the CommandInfo.

Returns:

  • []string: A slice of strings representing the CommandInfo.

Format:

Description:
	// <description>

Flags:
	- <flag 1>:
   	// <description>
	- <flag 2>:
   	// <description>
	// ...

func (*CommandInfo) GetCallback

func (inf *CommandInfo) GetCallback() CommandCallbackFunc

GetCallback returns the callback function of a CommandInfo.

Returns:

  • CommandCallbackFunc: The callback function of the CommandInfo.

Behaviors:

  • Never returns nil.

func (*CommandInfo) GetDescription

func (inf *CommandInfo) GetDescription() []string

GetDescription returns the description of a CommandInfo.

Returns:

  • *fsd.Document: The description of the CommandInfo.

func (*CommandInfo) GetFlag

func (inf *CommandInfo) GetFlag(name string) *FlagInfo

GetFlag returns the FlagInfo with the provided name.

Parameters:

  • name: The name of the flag to get.

Returns:

  • *FlagInfo: The FlagInfo with the provided name. Nil if not found.

func (*CommandInfo) GetFlags

func (inf *CommandInfo) GetFlags() []*FlagInfo

GetFlags returns the flags of a CommandInfo.

Returns:

  • []*FlagInfo: The flags of the CommandInfo.

Behaviors:

  • Modifying the returned slice will affect the CommandInfo.

func (*CommandInfo) GetOpcode

func (inf *CommandInfo) GetOpcode() string

GetOpcode returns the name of a CommandInfo.

Returns:

  • string: The name of the CommandInfo.

func (*CommandInfo) HasFlag

func (inf *CommandInfo) HasFlag(name string) bool

HasFlag checks if a CommandInfo has a flag with the provided name.

Parameters:

  • name: The name of the flag to check.

Returns:

  • bool: True if the flag exists, false otherwise.

func (*CommandInfo) Parse

func (inf *CommandInfo) Parse(branches []*resultBranch, args []string) ([]*uc.Pair[*ParsedCommand, error], error)

ParseArguments parses the provided command line arguments and returns a ParsedCommand ready to be executed.

Errors:

  • *ue.ErrInvalidParameter: No arguments provided.
  • *ErrCommandNotFound: Command not found.
  • *ErrParsingFlags: Error parsing flags.

Parameters:

  • args: The command line arguments to parse. Without the executable name.

Returns:

  • *ParsedCommand: A pointer to the parsed command.
  • error: An error, if any.

type DescriptionPrinter

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

func NewDescriptionPrinter

func NewDescriptionPrinter(lines []string) *DescriptionPrinter

func (*DescriptionPrinter) FString

func (dp *DescriptionPrinter) FString(trav *ffs.Traversor, opts ...ffs.Option) error

type ErrFormatError

type ErrFormatError struct {
	// Reason is the reason for the error.
	Reason error
}

ErrFormatError is an error type that is returned when a format error occurs.

func NewErrFormatError

func NewErrFormatError(reason error) *ErrFormatError

NewErrFormatError creates a new ErrFormatError error.

Parameters:

  • reason: The reason for the error.

Returns:

  • *ErrFormatError: A pointer to the new error.

func (*ErrFormatError) Error

func (e *ErrFormatError) Error() string

Error returns the error message: "format error: <reason>".

Returns:

  • string: The error message.

Behaviors:

  • If Reason is nil, the error message is: "format is invalid" instead.

type FlagCallbackFunc

type FlagCallbackFunc func(argMap map[string][]any) (map[string][]any, error)

FlagCallbackFunc is a function type that represents a callback function for a console command flag.

Parameters:

  • argMap: A map of string keys to any values representing the arguments passed to the flag.

Returns:

  • map[string]any: A map of string keys to any values representing the parsed arguments.
  • error: An error if the flag fails.

type FlagInfo

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

FlagInfo represents a flag for a console command.

func NewFlagInfo

func NewFlagInfo(name string, doc []string, isRequired bool, fn FlagCallbackFunc, argInfs []*ArgInfo) (*FlagInfo, error)

func (*FlagInfo) Equals

func (inf *FlagInfo) Equals(other uc.Equaler) bool

Equals checks if the flag is equal to another flag.

Two flags are equal iff their names are equal.

Parameters:

  • other: The other flag to compare.

Returns:

  • bool: true if the flags are equal, false otherwise.

func (*FlagInfo) Evaluator

func (inf *FlagInfo) Evaluator() evalSlc.LeafEvaluater[string, *FlagParseResult, *ArgInfo, []*resultArg]

Evaluator implements the Evaluable interface.

func (*FlagInfo) FString

func (cfi *FlagInfo) FString(trav *ffs.Traversor, opts ...ffs.Option) error

FString generates a formatted string representation of a FlagInfo.

Parameters:

  • indentLevel: The level of indentation to use for the FlagInfo.

Returns:

  • []string: A slice of strings representing the FlagInfo.

Format:

Arguments: <arg1> <arg2> ...

Description:
	// <description>

Required: <Yes/No>

func (*FlagInfo) GetArguments

func (inf *FlagInfo) GetArguments() []*ArgInfo

GetArguments returns the arguments of a FlagInfo.

Returns:

  • []*Argument: A slice of pointers to the arguments.

Behaviors:

  • No nil values are returned.
  • Modifying the returned slice will affect the FlagInfo.

func (*FlagInfo) GetName

func (inf *FlagInfo) GetName() string

GetName returns the name of a FlagInfo.

Returns:

  • string: The name of the FlagInfo.

func (*FlagInfo) IsRequired

func (inf *FlagInfo) IsRequired() bool

IsRequired returns whether a FlagInfo is required.

Returns:

  • bool: A boolean indicating whether the FlagInfo is required.

func (*FlagInfo) Parse

func (flag *FlagInfo) Parse(branches []*FlagParseResult, args []string) ([]*FlagParseResult, error)

Parse parses the provided arguments into a map of parsed arguments.

Parameters:

  • args: The arguments to parse.

Returns:

  • map[string]any: A map of the parsed arguments.
  • error: An error if the arguments are invalid.

type FlagParseResult

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

FlagParseResult represents the result of parsing a flag.

func NewFlagParseResult

func NewFlagParseResult() *FlagParseResult

NewFlagParseResult creates a new FlagParseResult with the given arguments, index, and ignorable boolean.

Parameters:

  • argMap: A map of argument names to their values.
  • argsDone: A list of arguments that have been parsed so far.

Returns:

  • *FlagParseResult: A pointer to the new FlagParseResult.

func (*FlagParseResult) Copy

func (fpr *FlagParseResult) Copy() uc.Copier

Copier implements the Copier interface.

func (*FlagParseResult) GetArgumentsDone

func (fpr *FlagParseResult) GetArgumentsDone() []string

GetArgumentsDone returns the arguments that have been parsed so far.

Returns:

  • []string: The arguments that have been parsed so far.

func (*FlagParseResult) GetResult

func (fpr *FlagParseResult) GetResult() map[string][]any

GetResult returns the parsed arguments.

Returns:

  • map[string][]any: The parsed arguments.

type ParsedCommand

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

ParsedCommand represents a parsed console command.

func (*ParsedCommand) Execute

func (pc *ParsedCommand) Execute() (any, error)

Execute executes the callback function for the parsed command.

Returns:

  • any: The result of the callback function.
  • error: An error if the callback function fails.

func (*ParsedCommand) GetName

func (pc *ParsedCommand) GetName() string

GetName returns the name of the command.

Returns:

  • string: The name of the command.

Jump to

Keyboard shortcuts

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