func(cmd *Command, rootFlags *RootFlags, args []string) error
Command represents a CLI command.
AddCommand adds a subcommand to a command.
Parameters:
- cmd: The subcommand to add.
{
cmd.Logger = &log.DefaultLogger{ComponentName: cmd.Name}
c.SubCommands = append(c.SubCommands, cmd)
}
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.
{
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 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.
{
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 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.
{
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 retrieves the string value of a flag.
Parameters:
- name: The name of the flag.
{
f := c.Flags.Lookup(name)
if f == nil {
return ""
}
return f.Value.String()
}
GetFlagInt retrieves the integer value of a flag.
Parameters:
- name: The name of the flag.
{
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 retrieves the boolean value of a flag.
Parameters:
- name: The name of the flag.
{
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 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.
{
value := os.Getenv(key)
if value == "" {
return defaultValue
}
return value
}
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.
{
value := os.Getenv(key)
if value == "" {
return defaultValue
}
intValue, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
return intValue
}
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.
{
value := os.Getenv(key)
if value == "" {
return defaultValue
}
boolValue, err := strconv.ParseBool(value)
if err != nil {
return defaultValue
}
return boolValue
}
PrintCommandHelp prints the help message for a single command.
{
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 sets up the logger functions for the command.
Parameters:
- name: The name of the command.
{
c.Logger = &log.DefaultLogger{ComponentName: name} //assegnazione logger
}
RootFlags represents the parsed flags of the root command.
GetString retrieves the string value of a root flag.
{
if rf == nil {
return ""
}
f := rf.Lookup(name)
if f == nil {
return ""
}
return f.Value.String()
}
GetInt retrieves the integer value of a root flag.
{
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 retrieves the boolean value of a root flag.
{
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
}
import "flag"
import "fmt"
import "os"
import "strconv"
import "github.com/mirkobrombin/go-cli-builder/v1/log"
import "strconv"
import "flag"
import "github.com/mirkobrombin/go-cli-builder/v1/log"