ConsolePanel

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 8 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.

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

Index

Constants

View Source
const (
	// DefaultWidth is the default width of the console.
	DefaultWidth int = 80

	// DefaultHeight is the default height of the console.
	DefaultHeight int = 24
)

Variables

This section is empty.

Functions

func NoArgumentParser added in v0.2.42

func NoArgumentParser(s 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 added in v0.2.42

func NoCommandCallback(args 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 added in v0.2.42

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 Argument added in v0.2.42

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

Argument represents an argument of a flag.

func NewArgument added in v0.2.42

func NewArgument(name string, argumentParserFunc ArgumentParserFunc) *Argument

NewArgument creates a new argument.

Parameters:

  • name: The name of the argument.
  • argumentParserFunc: The function that parses the argument.

Returns:

  • *Argument: A pointer to the newly created argument.

Behaviors:

  • If argumentParserFunc is nil, the default NoArgumentParser is used.

func (*Argument) Parse added in v0.2.42

func (a *Argument) Parse(s string) (any, 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 (*Argument) String added in v0.2.42

func (a *Argument) String() string

String returns the string: <name>

Returns:

  • string: The string representation of the argument.

type ArgumentParserFunc added in v0.2.42

type ArgumentParserFunc func(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 added in v0.2.42

type CommandCallbackFunc func(args 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 added in v0.2.42

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

CommandInfo represents a console command.

func NewCommandInfo added in v0.2.42

func NewCommandInfo(description *cdd.Document, callback CommandCallbackFunc) *CommandInfo

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

Parameters:

  • 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) AddFlag added in v0.2.42

func (ci *CommandInfo) AddFlag(flag string, info *FlagInfo) *CommandInfo

AddFlag adds a new flag to a CommandInfo.

Parameters:

  • flag: The flag to add.
  • info: The FlagInfo for the flag.

Returns:

  • *CommandInfo: A pointer to the CommandInfo. This allows for chaining.

Behaviors:

  • If info is nil, the flag is not added.
  • If the flag already exists, the existing flag is replaced with the new one.

func (*CommandInfo) FString added in v0.2.42

func (cci *CommandInfo) FString(trav *fs.Traversor)

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 added in v0.2.42

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 added in v0.2.42

func (inf *CommandInfo) GetDescription() *cdd.Document

GetDescription returns the description of a CommandInfo.

Returns:

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

func (*CommandInfo) GetFlags added in v0.2.42

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) Parse added in v0.2.42

func (inf *CommandInfo) Parse(args []string) (*ParsedCommand, error)

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

Errors:

  • *ers.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 ConsolePanel

type ConsolePanel struct {
	// ExecutableName is the name of the executable.
	ExecutableName string
	// contains filtered or unexported fields
}

ConsolePanel represents a command line console.

func NewConsolePanel

func NewConsolePanel(execName string, description *cdd.Document) *ConsolePanel

NewConsolePanel creates a new ConsolePanel with the provided executable name.

Parameters:

  • execName: The name of the executable.

Returns:

  • *ConsolePanel: A pointer to the created ConsolePanel.

func (*ConsolePanel) AddCommand

func (cp *ConsolePanel) AddCommand(opcode string, info *CommandInfo) *ConsolePanel

AddCommand adds the provided command to the ConsolePanel.

Parameters:

  • opcode: The command opcode.
  • info: The CommandInfo for the command.

Returns:

  • *ConsolePanel: A pointer to the ConsolePanel. This allows for chaining.

Behaviors:

  • If opcode is either an empty string or "help", the command is not added.
  • If info is nil, the command is not added.
  • If the opcode already exists, the existing command is replaced with the new one.

func (*ConsolePanel) FString

func (cns *ConsolePanel) FString(trav *fs.Traversor)

FString generates a formatted string representation of a ConsolePanel.

Parameters:

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

Returns:

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

Format:

Usage: <executable name> <commands> [flags]

Description:
	// <description>

Commands:
	- <command 1>:
   	// <description>
	- <command 2>:
   	// <description>
	// ...

func (*ConsolePanel) GetCommand

func (cns *ConsolePanel) GetCommand(opcode string) (*CommandInfo, bool)

GetCommand returns the CommandInfo for the provided opcode.

Parameters:

  • opcode: The opcode of the command.

Returns:

  • *CommandInfo: The CommandInfo for the opcode.
  • bool: A boolean indicating if the command was found.

func (*ConsolePanel) ParseArguments

func (cns *ConsolePanel) ParseArguments(args []string) (*ParsedCommand, error)

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

Errors:

  • *ers.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.

func (*ConsolePanel) SetDimensions added in v0.2.43

func (cns *ConsolePanel) SetDimensions(width, height int) error

SetDimensions sets the dimensions of the console.

Parameters:

  • width: The width of the console.
  • height: The height of the console.

Returns:

  • error: An error of type *ers.ErrInvalidParameter if width or height is less than or equal to 0.

type ErrCommandNotFound added in v0.2.42

type ErrCommandNotFound struct {
	// The command that was not found.
	Command string
}

ErrCommandNotFound represents an error where a command is not found.

func NewErrCommandNotFound added in v0.2.42

func NewErrCommandNotFound(command string) *ErrCommandNotFound

NewErrCommandNotFound creates a new ErrCommandNotFound with the provided command.

Parameters:

  • command: The command that was not found.

Returns:

  • *ErrCommandNotFound: A pointer to the new ErrCommandNotFound.

func (*ErrCommandNotFound) Error added in v0.2.42

func (e *ErrCommandNotFound) Error() string

Error returns the error message for an ErrCommandNotFound.

Returns:

  • string: The error message.

type ErrFewArguments added in v0.2.42

type ErrFewArguments struct{}

ErrFewArguments represents an error where not enough arguments are provided.

func NewErrFewArguments added in v0.2.42

func NewErrFewArguments() *ErrFewArguments

NewErrFewArguments creates a new ErrFewArguments.

Returns:

  • *ErrFewArguments: A pointer to the new ErrFewArguments.

func (*ErrFewArguments) Error added in v0.2.42

func (e *ErrFewArguments) Error() string

Error returns the error message for an ErrFewArguments.

Returns:

  • string: The error message.

type ErrParsingFlags added in v0.2.42

type ErrParsingFlags struct {
	// Name is the flag that could not be parsed.
	Name string

	// Reason is the reason the flag could not be parsed.
	Reason error
}

ErrParsingFlags represents an error where flags could not be parsed.

func NewErrParsingFlags added in v0.2.42

func NewErrParsingFlags(name string, reason error) *ErrParsingFlags

NewErrParsingFlags creates a new ErrParsingFlags with the provided name and reason.

Parameters:

  • name: The flag that could not be parsed.
  • reason: The reason the flag could not be parsed.

Returns:

  • *ErrParsingFlags: A pointer to the new ErrParsingFlags.

func (*ErrParsingFlags) Error added in v0.2.42

func (e *ErrParsingFlags) Error() string

Error returns the error message for an ErrParsingFlags.

Returns:

  • string: The error message.

func (*ErrParsingFlags) Unwrap added in v0.2.42

func (e *ErrParsingFlags) Unwrap() error

Unwrap returns the reason the flags could not be parsed.

Returns:

  • error: The reason the flags could not be parsed.

type ErrUnknownFlag added in v0.2.42

type ErrUnknownFlag struct {
	// The flag that is unknown.
	Flag string
}

ErrUnknownFlag represents an error where an unknown flag is provided.

func NewErrUnknownFlag added in v0.2.42

func NewErrUnknownFlag(flag string) *ErrUnknownFlag

NewErrUnknownFlag creates a new ErrUnknownFlag.

Returns:

  • *ErrUnknownFlag: A pointer to the new ErrUnknownFlag.

func (*ErrUnknownFlag) Error added in v0.2.42

func (e *ErrUnknownFlag) Error() string

Error returns the error message: "<flag> is not a known flag".

Returns:

  • string: The error message.

type FlagCallbackFunc added in v0.2.42

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 added in v0.2.42

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

FlagInfo represents a flag for a console command.

func NewFlagInfo added in v0.2.42

func NewFlagInfo(isRequired bool, callback FlagCallbackFunc, args ...*Argument) *FlagInfo

NewFlagInfo creates a new FlagInfo with the given name and arguments.

Parameters:

  • isRequired: A boolean indicating whether the flag is required.
  • callback: The function that parses the flag arguments.
  • args: A slice of strings representing the arguments accepted by the flag.

Returns:

  • *FlagInfo: A pointer to the new FlagInfo.

Behaviors:

  • Any nil arguments are filtered out.
  • If 'callback' is nil, a default callback is used that returns nil without error.

func (*FlagInfo) FString added in v0.2.42

func (cfi *FlagInfo) FString(trav *fs.Traversor)

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 added in v0.2.42

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

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) IsRequired added in v0.2.42

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 added in v0.2.42

func (flag *FlagInfo) Parse(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.
  • int: The index of the last unsuccessful parse argument.
  • bool: A boolean indicating whether the error is ignorable.
  • error: An error, if any.

func (*FlagInfo) SetDescription added in v0.2.42

func (cfi *FlagInfo) SetDescription(description *cdd.Document) *FlagInfo

SetDescription sets the description of a FlagInfo.

Parameters:

  • description: The description of the FlagInfo.

Returns:

  • *FlagInfo: A pointer to the FlagInfo. This allows for chaining.

type FlagParseResult added in v0.2.42

type FlagParseResult struct {
	// Parsed arguments.
	Args map[string]any

	// Index of the last unsuccessful parse argument.
	Index int
}

FlagParseResult represents the result of parsing a flag.

func NewFlagParseResult added in v0.2.42

func NewFlagParseResult(args map[string]any, index int) *FlagParseResult

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

Parameters:

  • args: The arguments to parse.
  • index: The index of the last unsuccessful parse argument.

Returns:

  • *FlagParseResult: A pointer to the new FlagParseResult.

type ParsedCommand added in v0.2.42

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

ParsedCommand represents a parsed console command.

func NewParsedCommand added in v0.2.42

func NewParsedCommand(args map[string]any, callbackFunc CommandCallbackFunc) (*ParsedCommand, error)

NewParsedCommand creates a new ParsedCommand with the provided name, arguments, and callback function.

Parameters:

  • args: A map of string keys to any values representing the arguments passed to the command.
  • callbackFunc: The function to call when the command is used.

Returns:

  • *ParsedCommand: A pointer to the new ParsedCommand.
  • error: An error of type *ers.ErrInvalidParameter if the callbackFunc is nil.

Behaviors:

  • If callbackFunc is nil, NoCommandCallback is used.

func (*ParsedCommand) Execute added in v0.2.42

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.

Jump to

Keyboard shortcuts

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