Documentation
¶
Overview ¶
Package program builds upon the Github packages knadh/koanf and urfave/cli/v3 to make it extremely simple to use the features of those two excellent packages in concert.
Every program using program will expose a standard set of command-line flags (--json, --log, --trace, --verbose) in addition to the standard flags provided by urfave/cli/v3 (--help and --version).
If a configuration struct is provided to the Run() function, then a further command=line flag (--config) is added to provide the source(s) of values for fields in the struct.
Example (Action) ¶
// Include an Action function
(&cli.Command{
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("hello")
return nil
},
Name: "action",
Version: "1",
}).Run(context.Background(), []string{"action"})
Output: hello
Example (Basic) ¶
// The most basic example of urfave/cli/v3
(&cli.Command{Name: "basic"}).Run(context.Background(), os.Args)
Output: NAME: basic - A new cli application USAGE: basic [global options] GLOBAL OPTIONS: --help, -h show help
Example (Flag1) ¶
// Include a custom flag
(&cli.Command{
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("hello")
return nil
},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "i",
Usage: "An integer",
},
},
Name: "action",
Version: "1",
}).Run(context.Background(), []string{"action"})
Output: hello
Example (Flag2) ¶
// Include a custom flag
(&cli.Command{
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Println("hello")
return nil
},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "i",
Usage: "An integer",
Value: 22,
},
},
Name: "action",
Version: "1",
}).Run(context.Background(), []string{"action", "--help"})
Output: NAME: action - A new cli application USAGE: action [global options] VERSION: 1 GLOBAL OPTIONS: -i value An integer (default: 22) --help, -h show help --version, -v print the version
Example (Version) ¶
// Include a Version field in the Command
(&cli.Command{
Name: "version",
Version: "1",
}).Run(context.Background(), os.Args)
Output: NAME: version - A new cli application USAGE: version [global options] VERSION: 1 GLOBAL OPTIONS: --help, -h show help --version, -v print the version
Index ¶
- func Run(ctx context.Context, command *cli.Command, options ...Option)
- type Configurator
- type FlagOption
- func DescTag(tag string) FlagOption
- func EnvDivider(divider string) FlagOption
- func EnvPrefix(prefix string) FlagOption
- func FlagDivider(divider string) FlagOption
- func FlagTag(tag string) FlagOption
- func Flatten(flatten bool) FlagOption
- func Prefix(prefix string) FlagOption
- func Validator(val sflags.ValidateFunc) FlagOption
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Configurator ¶ added in v0.1.0
type Configurator interface {
Validate() error
}
Configurator is the interface for a configuration struct
type FlagOption ¶ added in v0.1.0
type FlagOption func()
FlagOption is a functional option parameter for the ConfigFlags function
func DescTag ¶ added in v0.1.0
func DescTag(tag string) FlagOption
DescTag sets the struct tag where usage text is configured
func EnvDivider ¶ added in v0.1.0
func EnvDivider(divider string) FlagOption
EnvDivider is the character in between parts of an environment variable bound to a command line struct-bound flag
func EnvPrefix ¶ added in v0.1.0
func EnvPrefix(prefix string) FlagOption
EnvPrefix is an optional prefix for an environment variable bound to a command line struct-bound flag
func FlagDivider ¶ added in v0.1.0
func FlagDivider(divider string) FlagOption
FlagDivider is the character in between parts of a struct-bound command line flag's name
func FlagTag ¶ added in v0.1.0
func FlagTag(tag string) FlagOption
FlagTag is the struct tag used to configure struct-bound command line flags
func Flatten ¶ added in v0.1.0
func Flatten(flatten bool) FlagOption
Flatten determines the name of a command line flag bound to a an anonymous struct field
func Prefix ¶ added in v0.1.0
func Prefix(prefix string) FlagOption
Prefix is an optional prefix for the names of all struct-bound command line flags
func Validator ¶ added in v0.1.0
func Validator(val sflags.ValidateFunc) FlagOption
Validator is an optional function that will be called to to validate each struct-field bound flag
type Option ¶
Option is a functional parameter for Run()
func ConfigFlags ¶ added in v0.1.0
func ConfigFlags(configs []Configurator, ops ...FlagOption) Option
ConfigFlags creates and configures program.Run to have command line flags bound to the fields of one or more parts of a configuration struct
func Configuration ¶
func Configuration(config Configurator) Option
Configuration is an Option helper to define a configuration structure that will be populated from the sources given on a --config command-line flag
func NoDefaultFlags ¶ added in v0.1.0
func NoDefaultFlags() Option
NoDefaultFlags is a convenience function which is equivalent to calling all of NoJSON, NoLog, NoTrace, and NoVerbose