command

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

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

	// Usage is the short usage description of the command.
	Usage string

	// Description is the detailed description of the command.
	Description string

	// Flags are the command-line flags for the command.
	Flags *flag.FlagSet

	// Run is the function to execute when the command is invoked.
	Run RunFunc

	// SubCommands are the subcommands of the command.
	SubCommands []*Command

	// ArgFlags maps flag names to a boolean indicating if the flag allows an argument.
	ArgFlags map[string]bool

	// ShortFlagMap maps short flag names to their corresponding long flag names.
	ShortFlagMap map[string]string

	// Logger is an instance of the active logger.
	Logger *log.DefaultLogger

	// LogInfo is the function to log informational messages.
	LogInfo func(format string, a ...any)

	// LogWarning is the function to log warning messages.
	LogWarning func(format string, a ...any)

	// LogError is the function to log error messages.
	LogError func(format string, a ...any)

	// LogSuccess is the function to log success messages.
	LogSuccess func(format string, a ...any)
}

Command represents a CLI command.

func (*Command) AddBoolFlag

func (c *Command) AddBoolFlag(name, shortName, usage string, defaultValue, allowArg bool) *bool

AddBoolFlag adds a boolean flag to the command.

Parameters:

  • name: The name of the flag.
  • usage: The usage description of the flag.
  • allowArg: Whether the flag allows an argument.

Returns:

  • A pointer to the boolean value of the flag.

func (*Command) AddFlag

func (c *Command) AddFlag(name, shortName, usage, defaultValue string, allowArg bool) *string

AddFlag adds a string flag to the command.

Parameters:

  • name: The name of the flag.
  • usage: The usage description of the flag.
  • allowArg: Whether the flag allows an argument.

Returns:

  • A pointer to the string value of the flag.

func (*Command) AddIntFlag

func (c *Command) AddIntFlag(name, shortName, usage string, defaultValue int, allowArg bool) *int

AddIntFlag adds an integer flag to the command.

Parameters:

  • name: The name of the flag.
  • usage: The usage description of the flag.
  • allowArg: Whether the flag allows an argument.

Returns:

  • A pointer to the integer value of the flag.

func (*Command) GetEnv

func (c *Command) GetEnv(key, defaultValue string) string

GetEnv retrieves the value of an environment variable.

Parameters:

  • key: The name of the environment variable.
  • defaultValue: The default value to return if the variable is not set.

Returns:

  • The value of the environment variable, or the default value if not set.

func (*Command) GetEnvBool

func (c *Command) GetEnvBool(key string, defaultValue bool) bool

GetEnvBool retrieves the boolean value of an environment variable.

Parameters:

  • key: The name of the environment variable.
  • defaultValue: The default value to return if the variable is not set or not a boolean.

Returns:

  • The boolean value of the environment variable, or the default value if not set or not a boolean.

func (*Command) GetEnvInt

func (c *Command) GetEnvInt(key string, defaultValue int) int

GetEnvInt retrieves the integer value of an environment variable.

Parameters:

  • key: The name of the environment variable.
  • defaultValue: The default value to return if the variable is not set or not an integer.

Returns:

  • The integer value of the environment variable, or the default value if not set or not an integer.

func (*Command) GetFlagBool

func (c *Command) GetFlagBool(name string) bool

GetFlagBool retrieves the boolean value of a flag.

Parameters:

  • name: The name of the flag.

Returns:

  • The boolean value of the flag, or false if the flag does not exist.

func (*Command) GetFlagInt

func (c *Command) GetFlagInt(name string) int

GetFlagInt retrieves the integer value of a flag.

Parameters:

  • name: The name of the flag.

Returns:

  • The integer value of the flag, or -1 if the flag does not exist or is not an integer.

func (*Command) GetFlagString

func (c *Command) GetFlagString(name string) string

GetFlagString retrieves the string value of a flag.

Parameters:

  • name: The name of the flag.

Returns:

  • The string value of the flag, or an empty string if the flag does not exist.

func (*Command) PrintCommandHelp

func (c *Command) PrintCommandHelp()

PrintCommandHelp prints the help message for a single command.

func (*Command) SetupLogger

func (c *Command) SetupLogger(name string)

setupLogger sets up the logger functions for the command.

Parameters:

  • name: The name of the command.

type RootFlags

type RootFlags struct {
	*flag.FlagSet
}

RootFlags represents the parsed flags of the root command.

func (*RootFlags) GetBool

func (rf *RootFlags) GetBool(name string) bool

GetBool retrieves the boolean value of a root flag.

func (*RootFlags) GetInt

func (rf *RootFlags) GetInt(name string) int

GetInt retrieves the integer value of a root flag.

func (*RootFlags) GetString

func (rf *RootFlags) GetString(name string) string

GetString retrieves the string value of a root flag.

type RunFunc

type RunFunc func(cmd *Command, rootFlags *RootFlags, args []string) error

Jump to

Keyboard shortcuts

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