CmdLineParser

package
v0.3.22 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.

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
)
View Source
const (
	// HelpOpcode is the opcode for the help command.
	HelpOpcode string = "help"
)

Variables

This section is empty.

Functions

func BoolFString

func BoolFString(val bool) (string, error)

BoolFS returns "Yes" if val is true, "No" otherwise.

Parameters:

  • val: The boolean value.

Returns:

  • string: "Yes" if val is true, "No" otherwise.
  • error: nil

Types

type ArgBuilder

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

func NewArgBuilder

func NewArgBuilder() *ArgBuilder

func (*ArgBuilder) Build

func (b *ArgBuilder) Build() ([]*pkg.ArgInfo, error)

func (*ArgBuilder) Reset

func (b *ArgBuilder) Reset()

func (*ArgBuilder) SetArg

func (b *ArgBuilder) SetArg(name string, parserFunc pkg.ArgumentParserFunc) *ArgBuilder

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.

type CmdBuilder

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

func NewCmdBuilder

func NewCmdBuilder() *CmdBuilder

func (*CmdBuilder) Build

func (b *CmdBuilder) Build() ([]*pkg.CommandInfo, error)

func (*CmdBuilder) Reset

func (b *CmdBuilder) Reset()

func (*CmdBuilder) SetCmd

func (b *CmdBuilder) SetCmd(name string, description []string, callback pkg.CommandCallbackFunc, flagBuilder *FlagBuilder) *CmdBuilder

type CmdLineParser

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

CmdLineParser represents a command line console.

func NewCmdLineParser

func NewCmdLineParser(execName string, description []string, commandBuilder *CmdBuilder) (*CmdLineParser, error)

NewCmdLineParser creates a new CmdLineParser with the provided executable name.

Parameters:

  • execName: The name of the executable.

Returns:

  • *CmdLineParser: A pointer to the created CmdLineParser.

addCommand adds the provided command to the CmdLineParser.

Parameters:

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

Returns:

  • *CmdLineParser: A pointer to the CmdLineParser. 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 (*CmdLineParser) FString

func (cns *CmdLineParser) FString(trav *ffs.Traversor, opts ...ffs.Option) error

FString generates a formatted string representation of a CmdLineParser.

Parameters:

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

Returns:

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

Format:

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

Description:
	// <description>

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

func (*CmdLineParser) GetCommand

func (cns *CmdLineParser) GetCommand(opcode string) (*pkg.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 (*CmdLineParser) GetCommandByOpcode

func (cns *CmdLineParser) GetCommandByOpcode(opcode string) *pkg.CommandInfo

GetCommandByOpcode returns the CommandInfo for the provided opcode.

Parameters:

  • opcode: The opcode of the command.

Returns:

  • *CommandInfo: The CommandInfo for the opcode. Nil if not found.

func (*CmdLineParser) Parse

func (cns *CmdLineParser) Parse(args []string) (*pkg.ParsedCommand, error)

Parse 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.

func (*CmdLineParser) SetDimensions

func (cns *CmdLineParser) 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 *ue.ErrInvalidParameter if width or height is less than or equal to 0.

type ErrCommandNotFound

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

ErrCommandNotFound represents an error where a command is not found.

func NewErrCommandNotFound

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

func (e *ErrCommandNotFound) Error() string

Error returns the error message: "command <command> not found".

Returns:

  • string: The error message.

type ErrOpcodeHelp

type ErrOpcodeHelp struct{}

ErrOpcodeHelp represents an error where the opcode help is reserved.

func NewErrOpcodeHelp

func NewErrOpcodeHelp() *ErrOpcodeHelp

NewErrOpcodeHelp creates a new ErrOpcodeHelp.

Returns:

  • *ErrOpcodeHelp: A pointer to the new ErrOpcodeHelp.

func (*ErrOpcodeHelp) Error

func (e *ErrOpcodeHelp) Error() string

Error returns the error message: "opcode \"help\" is reserved".

Returns:

  • string: The error message.

type FlagBuilder

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

func NewFlagBuilder

func NewFlagBuilder() *FlagBuilder

func (*FlagBuilder) Build

func (b *FlagBuilder) Build() ([]*pkg.FlagInfo, error)

func (*FlagBuilder) Reset

func (b *FlagBuilder) Reset()

func (*FlagBuilder) SetFlag

func (b *FlagBuilder) SetFlag(name string, isRequired bool, description []string, callback pkg.FlagCallbackFunc, argBuilder *ArgBuilder) *FlagBuilder

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

Parameters:

  • name: The name of the flag.
  • 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.

Directories

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

Jump to

Keyboard shortcuts

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