RunFunc type

Type Definition:

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

Command struct

Command represents a CLI command.

Fields:

  • Name (string)
  • Usage (string)
  • Description (string)
  • Flags (*flag.FlagSet)
  • RequiredFlags ([]string)
  • BeforeRun (RunFunc)
  • Run (RunFunc)
  • AfterRun (RunFunc)
  • SubCommands ([]*Command)
  • ArgFlags (map[string]bool)
  • ShortFlagMap (map[string]string)
  • Logger (*log.DefaultLogger)
  • LogInfo (func(format string, a ...any))
  • LogWarning (func(format string, a ...any))
  • LogError (func(format string, a ...any))
  • LogSuccess (func(format string, a ...any))

Methods:

AddCommand

AddCommand adds a subcommand to a command.

Parameters:

- cmd: The subcommand to add.


Parameters:
  • cmd *Command

Show/Hide Method Body
{
	cmd.Logger = &log.DefaultLogger{ComponentName: cmd.Name}
	c.SubCommands = append(c.SubCommands, cmd)
}

AddFlag

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.


Parameters:
  • name string
  • shortName string
  • usage string
  • defaultValue string
  • allowArg bool
  • required bool

Returns:
  • *string

Show/Hide Method Body
{
	if c.Flags == nil {
		c.Flags = flag.NewFlagSet(c.Name, flag.ExitOnError)
		c.ArgFlags = make(map[string]bool)
	}
	if c.ShortFlagMap == nil {
		c.ShortFlagMap = make(map[string]string)
	}
	c.ArgFlags[name] = allowArg
	if shortName != "" {
		c.ShortFlagMap[shortName] = name
	}
	if required {
		c.RequiredFlags = append(c.RequiredFlags, name)
	}
	return c.Flags.String(name, defaultValue, usage)
}

AddIntFlag

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.


Parameters:
  • name string
  • shortName string
  • usage string
  • defaultValue int
  • allowArg bool
  • required bool

Returns:
  • *int

Show/Hide Method Body
{
	if c.Flags == nil {
		c.Flags = flag.NewFlagSet(c.Name, flag.ExitOnError)
		c.ArgFlags = make(map[string]bool)
	}
	if c.ShortFlagMap == nil {
		c.ShortFlagMap = make(map[string]string)
	}
	c.ArgFlags[name] = allowArg
	if shortName != "" {
		c.ShortFlagMap[shortName] = name
	}
	if required {
		c.RequiredFlags = append(c.RequiredFlags, name)
	}
	return c.Flags.Int(name, defaultValue, usage)
}

AddBoolFlag

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.


Parameters:
  • name string
  • shortName string
  • usage string
  • defaultValue bool
  • allowArg bool
  • required bool

Returns:
  • *bool

Show/Hide Method Body
{
	if c.Flags == nil {
		c.Flags = flag.NewFlagSet(c.Name, flag.ExitOnError)
		c.ArgFlags = make(map[string]bool)
	}
	if c.ShortFlagMap == nil {
		c.ShortFlagMap = make(map[string]string)
	}
	c.ArgFlags[name] = allowArg
	if shortName != "" {
		c.ShortFlagMap[shortName] = name
	}
	if required {
		c.RequiredFlags = append(c.RequiredFlags, name)
	}

	return c.Flags.Bool(name, defaultValue, usage)
}

GetFlagString

GetFlagString retrieves the string value of a flag.

Parameters:

- name: The name of the flag.


Parameters:
  • name string

Returns:
  • string

Show/Hide Method Body
{
	f := c.Flags.Lookup(name)
	if f == nil {
		return ""
	}
	return f.Value.String()
}

GetFlagInt

GetFlagInt retrieves the integer value of a flag.

Parameters:

- name: The name of the flag.


Parameters:
  • name string

Returns:
  • int

Show/Hide Method Body
{
	f := c.Flags.Lookup(name)
	if f == nil {
		return -1
	}
	val, err := strconv.Atoi(f.Value.String())
	if err != nil {
		return -1
	}
	return val
}

GetFlagBool

GetFlagBool retrieves the boolean value of a flag.

Parameters:

- name: The name of the flag.


Parameters:
  • name string

Returns:
  • bool

Show/Hide Method Body
{
	f := c.Flags.Lookup(name)
	if f == nil {
		return false
	}
	val, err := strconv.ParseBool(f.Value.String())
	if err != nil {
		return false
	}
	return val
}

GetEnv

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.


Parameters:
  • key string
  • defaultValue string

Returns:
  • string

Show/Hide Method Body
{
	value := os.Getenv(key)
	if value == "" {
		return defaultValue
	}
	return value
}

GetEnvInt

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.


Parameters:
  • key string
  • defaultValue int

Returns:
  • int

Show/Hide Method Body
{
	value := os.Getenv(key)
	if value == "" {
		return defaultValue
	}
	intValue, err := strconv.Atoi(value)
	if err != nil {
		return defaultValue
	}
	return intValue
}

GetEnvBool

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.


Parameters:
  • key string
  • defaultValue bool

Returns:
  • bool

Show/Hide Method Body
{
	value := os.Getenv(key)
	if value == "" {
		return defaultValue
	}
	boolValue, err := strconv.ParseBool(value)
	if err != nil {
		return defaultValue
	}
	return boolValue
}

PrintCommandHelp

PrintCommandHelp prints the help message for a single command.


Show/Hide Method Body
{
	fmt.Printf("%s: %s\n\nUsage: %s %s [flags]\n\n", c.Name, c.Description, os.Args[0], c.Name)
	if c.Flags != nil {
		fmt.Println("Flags:")
		c.Flags.PrintDefaults()
	}
	if c.SubCommands != nil {
		fmt.Println("Subcommands:")
		for _, subCmd := range c.SubCommands {
			fmt.Printf("  %s: %s\n", subCmd.Name, subCmd.Description)
		}
	}
}

SetupLogger

setupLogger sets up the logger functions for the command.

Parameters:

- name: The name of the command.


Parameters:
  • name string

Show/Hide Method Body
{
	c.Logger = &log.DefaultLogger{ComponentName: name} //assegnazione logger
}

RootFlags struct

RootFlags represents the parsed flags of the root command.

Methods:

GetString

GetString retrieves the string value of a root flag.


Parameters:
  • name string

Returns:
  • string

Show/Hide Method Body
{
	if rf == nil {
		return ""
	}
	f := rf.Lookup(name)
	if f == nil {
		return ""
	}
	return f.Value.String()
}

GetInt

GetInt retrieves the integer value of a root flag.


Parameters:
  • name string

Returns:
  • int

Show/Hide Method Body
{
	if rf == nil {
		return -1
	}
	f := rf.Lookup(name)
	if f == nil {
		return -1
	}
	val, err := strconv.Atoi(f.Value.String())
	if err != nil {
		return -1
	}
	return val
}

GetBool

GetBool retrieves the boolean value of a root flag.


Parameters:
  • name string

Returns:
  • bool

Show/Hide Method Body
{
	if rf == nil {
		return false
	}
	f := rf.Lookup(name)
	if f == nil {
		return false
	}
	val, err := strconv.ParseBool(f.Value.String())
	if err != nil {
		return false
	}
	return val
}

flag import

Import example:

import "flag"

fmt import

Import example:

import "fmt"

os import

Import example:

import "os"

strconv import

Import example:

import "strconv"

github.com/mirkobrombin/go-cli-builder/v1/log import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/log"

strconv import

Import example:

import "strconv"

flag import

Import example:

import "flag"

github.com/mirkobrombin/go-cli-builder/v1/log import

Import example:

import "github.com/mirkobrombin/go-cli-builder/v1/log"