positionals

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoolPositionalArgument

type BoolPositionalArgument struct {
	Name     string
	Help     string // Help message
	Value    *bool  // Values
	Required bool
}

BoolPositionalArgument represents a positional command-line argument that expects a boolean value. This struct is used to define and handle a boolean input parameter, including its name, help message, and whether it is required.

Fields:

Name (string): The name of the argument, used for display and reference purposes.
Help (string): A help message describing the purpose of the argument, shown in usage instructions.
Value (*bool): A pointer to a boolean where the parsed value will be stored.
Required (bool): A flag indicating whether this argument must be provided. If set to true, the argument
                 is mandatory; otherwise, it is optional.

func (BoolPositionalArgument) Consume

func (arg BoolPositionalArgument) Consume(arguments []string) ([]string, error)

Consume processes the command-line arguments and sets the value of the BoolPositionalArgument.

The function iterates through the provided arguments and checks if any of them match the short or long name of the BoolPositionalArgument. If a match is found, it sets the value of the BoolPositionalArgument to true and returns the remaining arguments. If the argument is required and not found, it returns an error.

Parameters:

  • arguments: A slice of strings representing the command-line arguments.

Returns: - A slice of strings representing the remaining arguments after processing the BoolPositionalArgument.

func (BoolPositionalArgument) GetHelp

func (arg BoolPositionalArgument) GetHelp() string

GetHelp retrieves the help message associated with the positional argument.

Returns:

(string): The help message describing the argument.

func (BoolPositionalArgument) GetName

func (arg BoolPositionalArgument) GetName() string

GetName retrieves the name of the positional argument.

Returns:

(string): The name of the argument.

func (BoolPositionalArgument) GetValue

func (arg BoolPositionalArgument) GetValue() any

GetValue retrieves the current value of the positional argument.

Returns:

(any): The current boolean value of the argument, or nil if not set.

func (*BoolPositionalArgument) Init

func (arg *BoolPositionalArgument) Init(value *bool, name string, help string)

Init initializes the `BoolPositionalArgument` with a specified value, name, and help message.

Parameters:

value (*bool): A pointer to the boolean that will hold the argument's value.
name (string): The name of the positional argument.
help (string): The help message describing the argument.

Behavior:

This method sets up the `BoolPositionalArgument` and marks it as required.

func (BoolPositionalArgument) IsRequired

func (arg BoolPositionalArgument) IsRequired() bool

IsRequired indicates whether the positional argument is mandatory.

Returns:

(bool): Always returns true, as positional arguments are required.

type IntPositionalArgument

type IntPositionalArgument struct {
	Name     string
	Help     string // Help message
	Value    *int   // Values
	Required bool
}

IntPositionalArgument represents a positional command-line argument that expects an integer value. This struct is used to define and handle an integer input parameter, including its name, help message, and whether it is required.

Fields:

Name (string): The name of the argument, used for display and reference purposes.
Help (string): A help message describing the purpose of the argument, shown in usage instructions.
Value (*int): A pointer to an integer where the parsed value will be stored.
Required (bool): A flag indicating whether this argument must be provided. If set to true, the argument
                 is mandatory; otherwise, it is optional.

func (IntPositionalArgument) Consume

func (arg IntPositionalArgument) Consume(arguments []string) ([]string, error)

Consume processes the command-line arguments and sets the value of the IntPositionalArgument.

The function iterates through the provided arguments and checks if any of them match the short or long name of the IntPositionalArgument. If a match is found, it sets the value of the IntPositionalArgument to the next argument in the list and returns the remaining arguments. If the argument is required and not found, it returns an error.

Parameters:

  • arguments: A slice of strings representing the command-line arguments.

Returns: - A slice of strings representing the remaining arguments after processing the IntPositionalArgument.

func (IntPositionalArgument) GetHelp

func (arg IntPositionalArgument) GetHelp() string

GetHelp retrieves the help message associated with the positional argument.

Returns:

(string): The help message describing the argument.

func (IntPositionalArgument) GetName

func (arg IntPositionalArgument) GetName() string

GetName retrieves the name of the positional argument.

Returns:

(string): The name of the argument.

func (IntPositionalArgument) GetValue

func (arg IntPositionalArgument) GetValue() any

GetValue retrieves the current value of the positional argument.

Returns:

(any): The current integer value of the argument, or nil if not set.

func (*IntPositionalArgument) Init

func (arg *IntPositionalArgument) Init(value *int, name string, help string)

Init initializes the `IntPositionalArgument` with a specified value, name, and help message.

Parameters:

value (*int): A pointer to the integer that will hold the argument's value.
name (string): The name of the positional argument.
help (string): The help message describing the argument.

Behavior:

This method sets up the `IntPositionalArgument` and marks it as required.

func (IntPositionalArgument) IsRequired

func (arg IntPositionalArgument) IsRequired() bool

IsRequired indicates whether the positional argument is mandatory.

Returns:

(bool): True if the argument is required; otherwise, false.

type PositionalArgument

type PositionalArgument interface {
	// Retrieve the name
	GetName() string
	// Retrieve the help message
	GetHelp() string
	// Retrieve the value
	GetValue() any
	// Check if the argument is required
	IsRequired() bool
	// Parse the argument from the provided input
	Consume(arguments []string) ([]string, error)
}

PositionalArgument is an interface that defines the common behavior for all positional argument types. Any struct that implements this interface can be used as a positional argument, allowing for a consistent approach to parsing and handling different argument types.

Methods:

GetName() string: Retrieves the name of the argument, used for display and reference purposes.
GetHelp() string: Retrieves the help message describing the purpose of the argument.
GetValue() any: Retrieves the current value of the argument, which can be of any type.
IsRequired() bool: Indicates whether this argument is mandatory. Returns true if the argument must be provided.
Consume(arguments []string) ([]string, error): Parses and consumes the argument from a list of input strings. Returns the remaining arguments and an error if the parsing fails.

type StringPositionalArgument

type StringPositionalArgument struct {
	Name     string
	Help     string  // Help message
	Value    *string // Values
	Required bool
}

StringPositionalArgument represents a positional command-line argument that expects a string value. This struct is used to define and handle a string input parameter, including its name, help message, and whether it is required.

Fields:

Name (string): The name of the argument, used for display and reference purposes.
Help (string): A help message describing the purpose of the argument, shown in usage instructions.
Value (*string): A pointer to a string where the parsed value will be stored.
Required (bool): A flag indicating whether this argument must be provided. If set to true, the argument
                 is mandatory; otherwise, it is optional.

func (StringPositionalArgument) Consume

func (arg StringPositionalArgument) Consume(arguments []string) ([]string, error)

Consume processes the command-line arguments and sets the value of the StringPositionalArgument.

The function iterates through the provided arguments and checks if any of them match the short or long name of the StringPositionalArgument. If a match is found, it sets the value of the StringPositionalArgument to the next argument in the list and returns the remaining arguments. If the argument is required and not found, it returns an error.

Parameters:

  • arguments: A slice of strings representing the command-line arguments.

Returns: - A slice of strings representing the remaining arguments after processing the StringPositionalArgument.

func (StringPositionalArgument) GetHelp

func (arg StringPositionalArgument) GetHelp() string

GetHelp retrieves the help message associated with the positional argument.

Returns:

(string): The help message describing the argument.

func (StringPositionalArgument) GetName

func (arg StringPositionalArgument) GetName() string

GetName retrieves the name of the positional argument.

Returns:

(string): The name of the argument.

func (StringPositionalArgument) GetValue

func (arg StringPositionalArgument) GetValue() any

GetValue retrieves the current value of the positional argument.

Returns:

(any): The current value of the argument, or nil if not set.

func (*StringPositionalArgument) Init

func (arg *StringPositionalArgument) Init(value *string, name string, help string)

Init initializes the StringPositionalArgument with the provided values.

The function sets the short name, long name, help message, value, and default value for the StringPositionalArgument. It ensures that the short name and long name are properly formatted with leading dashes.

Parameters:

  • value: A pointer to a string where the value of the argument will be stored.
  • shortName: The short name of the argument (single character). If empty, it will be set to an empty string.
  • longName: The long name of the argument (string). If empty, it will be set to an empty string.
  • defaultValue: The default value of the argument.
  • help: The help message describing the argument.

The function uses the utils.StripLeftDashes function to remove any leading dashes from the short name and long name before adding a single dash for the short name and a double dash for the long name.

func (StringPositionalArgument) IsRequired

func (arg StringPositionalArgument) IsRequired() bool

IsRequired indicates whether the positional argument is mandatory.

Returns:

(bool): True if the argument is required; otherwise, false.

Jump to

Keyboard shortcuts

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