CMLine

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseCommandLine

func ParseCommandLine(args []string, commands []*ConsoleCommandInfo) (string, any, error)

ParseCommandLine parses the command line arguments and executes the appropriate command. It takes a slice of arguments and a slice of ConsoleCommandInfo. The function first checks if the command exists in the provided commands. If the command is not found, an error is returned. If the command is found, the function checks if there are any other commands with the same name. If there are, an error is returned. The function then parses the flags for the command. If the flags cannot be parsed, an error is returned. Finally, the function executes the command's callback function and returns the command's name, the result of the callback, and any error that occurred.

Types

type ConsoleCommandInfo

type ConsoleCommandInfo struct {
	// Name is the name of the console command.
	Name string

	// Description provides a brief explanation of what the command does.
	Description string

	// Flags is a slice of ConsoleFlagInfo that represents the flags accepted by
	// the command.
	Flags []*ConsoleFlagInfo

	// Callback is a function that is invoked when the command is executed.
	// It takes a map of string keys to any type values as arguments, and returns
	// a result of any type and an error, if any.
	Callback func(args map[string]any) (any, error)
}

ConsoleCommandInfo represents a console command. It contains the name and description of the command, as well as a list of flags that the command accepts. The Callback function is invoked when the command is executed, with the provided arguments. It returns a result and an error, if any.

func (ConsoleCommandInfo) FString added in v0.2.3

func (cci ConsoleCommandInfo) FString(executableName string) string

FString formats the ConsoleCommandInfo into a string that can be displayed to the user. It includes the command name, description, usage, and flags. The executableName parameter is used to show the command usage. If the command has no flags, "None" is displayed. Each flag is formatted using its FString method. The method returns the formatted string.

type ConsoleFlagInfo

type ConsoleFlagInfo struct {
	// Name is the name of the flag.
	Name string

	// Args is a slice of strings that represents the arguments accepted by the flag.
	Args []string

	// Description provides a brief explanation of what the flag does.
	Description string

	// Required is a boolean that indicates whether the flag is required.
	Required bool

	// Callback is a function that is invoked when the flag is used.
	// It takes a variadic number of string arguments,
	// and returns a result of any type and an error, if any.
	Callback func(...string) (any, error)
}

ConsoleFlagInfo represents a flag for a console command. It contains the name, arguments, and description of the flag, as well as a boolean indicating whether the flag is required. The Callback function is invoked when the flag is used, with the provided arguments. It returns a result and an error, if any.

func (ConsoleFlagInfo) FString added in v0.2.3

func (cfi ConsoleFlagInfo) FString(indentLevel int) string

FString formats the ConsoleFlagInfo into a string that can be displayed to the user. It includes the flag name, description, and whether it's required. The indentLevel parameter is used to control the indentation of the output. If the flag has arguments, they are included in the output. The method returns the formatted string.

Jump to

Keyboard shortcuts

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