flagx

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: BSD-3-Clause Imports: 6 Imported by: 2

README

flagx

Build Status GoDoc Report Card

Package flagx implements some utilities for command-line flag parsing. It extends the functionalities of the standard flag package.

Main features:

  • sub commands management
  • alias of command and flag names
  • array of string flag type
  • check if a flag was passed

For example the next code defines an app Command instance with a sub-command with name action and aliases act, ac and a. Note that only the names of the sub-commands are defined; the command name itself is not defined in the Command type. The name of the root command is obtained from the os.Args[0] parameter.

var app = &flagx.Command{
    ParseExec: runApp,
    SubCmd: map[string]*flagx.Command{
        "action,act,ac,a": {
            ParseExec: runAction,
        },
    },
}

To execute the app Command instance with the command line arguments call the Run module function.

err := flagx.Run(app)

The Run module function in turn calls the ParseExec function of the app command or the action sub-command based on the command-line arguments. Each ParseExec function first parse the passed arguments, then execute the specific work.

func runAction(name string, arguments []string) error {
    var params []string
    fs := flag.NewFlagSet(name, flag.ContinueOnError)
    flagx.AliasedStringsVar(fs, &params, "params,p", "description of the parameters")

    err := fs.Parse(arguments)

    if err == nil {
        err = execAction(params) // execute the specific work
    }
    return err
}

The AliasedStringsVar function defines an array of strings flag with name params and alias p. The command-line

app action --params str1,str2 -p str3 -params str4 -p str5,str6

will execute the execAction function with []string{"str1", "str2", "str3", "str4", "str5", "str6"} strings.

See test for more informations and usage examples.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCommandName = errors.New("invalid command name")
	ErrNoExecFunc         = errors.New("exec function undefined")
	ErrCommandNotFound    = errors.New("command not found")
)

flagx defined inner errors

Functions

func AliasedBoolVar

func AliasedBoolVar(fs *flag.FlagSet, p *bool, names string, value bool, usage string)

AliasedBoolVar defines a bool flag with specified names, default value, and usage string. The specified usage string is used for the primary flag name only. The usage string of a secondary flag name specifies that it is an alias of the primary name. The argument p points to a bool variable in which to store the value of the flag.

func AliasedFloat64Var added in v0.1.1

func AliasedFloat64Var(fs *flag.FlagSet, p *float64, names string, value float64, usage string)

AliasedFloat64Var defines an float64 flag with specified names, default value, and usage string. The specified usage string is used for the primary flag name only. The usage string of a secondary flag name specifies that it is an alias of the primary name. The argument p points to an float64 variable in which to store the value of the flag.

func AliasedInt64Var added in v0.1.1

func AliasedInt64Var(fs *flag.FlagSet, p *int64, names string, value int64, usage string)

AliasedInt64Var defines an int64 flag with specified names, default value, and usage string. The specified usage string is used for the primary flag name only. The usage string of a secondary flag name specifies that it is an alias of the primary name. The argument p points to an int64 variable in which to store the value of the flag.

func AliasedIntVar

func AliasedIntVar(fs *flag.FlagSet, p *int, names string, value int, usage string)

AliasedIntVar defines an int flag with specified names, default value, and usage string. The specified usage string is used for the primary flag name only. The usage string of a secondary flag name specifies that it is an alias of the primary name. The argument p points to an int variable in which to store the value of the flag.

func AliasedStringVar

func AliasedStringVar(fs *flag.FlagSet, p *string, names string, value string, usage string)

AliasedStringVar defines a string flag with specified names, default value, and usage string. The `names` argument is the comma separated aliases of the flag. The specified usage string is used for the primary flag name only. The usage string of a secondary flag name specifies that it is an alias of the primary name. The argument p points to a string variable in which to store the value of the flag.

func AliasedStringsVar

func AliasedStringsVar(fs *flag.FlagSet, p *[]string, names string, usage string)

AliasedStringsVar defines a []string flag with specified names, and usage string. The specified usage string is used for the primary flag name only. The usage string of a secondary flag name specifies that it is an alias of the primary name. The argument p points to a []string variable in which to store the value of the flag. Note: no default value is given.

func IsPassed

func IsPassed(fs *flag.FlagSet, names string) bool

func Run

func Run(app *Command) error

Run execute the `app` command with the command-line arguments. The name of the `app` command is obtained from the `os.Args[0]` argument.

Types

type Command

type Command struct {
	SubCmd    map[string]*Command // sub-commands of the command
	ParseExec ParseExecFunc       // function to be executed by the command
}

Command represents a node of the commands tree. Each node has the function to be called if the command is executed and the children sub-commands.

type ParseExecFunc

type ParseExecFunc func(fullname string, arguments []string) error

ParseExecFunc is the signature of the function that is called when a Command is executed.

cmdfullname: the full name of the command (example: "app cmd subcmd") arguments: the arguments of the command

Jump to

Keyboard shortcuts

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