Documentation
¶
Overview ¶
Package clifx provides simple integration between go.uber.org/fx and github.com/alecthomas/kong.
Index ¶
- func New[C any](args Arguments, options ...kong.Option) (cli C, kctx *kong.Context, err error)
- func NewConstructor[C any](args Arguments, options ...kong.Option) func() (C, *kong.Context, error)
- func Provide[C any](args Arguments, options ...kong.Option) fx.Option
- func SuppressExit() kong.Option
- type Arguments
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New parses the command-line given by args. The optional set of Kong options is used to build the parser. This is the most flexible way to bootstrap Kong within an fx application, as it allows a caller to control how all the various objects are seen by the fx.App.
The type C must be a struct. Otherwise, this function returns an error.
func NewConstructor ¶
NewConstructor returns a closure that uses New to build the configuration object and context. This function is a useful alternative to New when a caller does not wish to inject the arguments and options.
func Provide ¶
Provide implements the simplest use case: Global components for the command-line and the kong context are emitted based the supplied arguments and options.
Example (Basic) ¶
ExampleProvide_basic shows how to bootstrap a basic command line using clifx.
type cli struct { Address string `short:"a"` } var c cli var kctx *kong.Context fx.New( fx.NopLogger, Provide[cli]( AsArguments("-a", ":8080"), // can use StandardArguments here to pass the process command-line arguments SuppressExit(), // in case of an error, prevent this example from calling os.Exit ), fx.Populate( &c, &kctx, ), ) fmt.Println(c.Address) fmt.Println(kctx.Args)
Output: :8080 [-a :8080]
Example (Run) ¶
ExampleProvide_run shows how to execute a CLI as part of fx.New().
type cli struct { Address string `short:"a"` } fx.New( fx.NopLogger, fx.Supply(123), // just to illustrate another component Provide[cli]( AsArguments("-a", ":8080"), SuppressExit(), ), fx.Invoke( func(kctx *kong.Context, sh fx.Shutdowner, value int) error { defer sh.Shutdown() //nolint: errcheck fmt.Println(kctx.Args) fmt.Println(value) // see the Kong documentation for how to implement this: return kctx.Run(value) }, ), )
Output: [-a :8080] 123
func SuppressExit ¶
SuppressExit sets a noop exit function so suppress calling os.Exit when the command line parsing fails. This is mostly useful for testing and examples, but can be useful when an application wants to handle parsing errors in a custom manner.
Types ¶
type Arguments ¶
type Arguments []string
Arguments represent the raw command-line arguments passed to the program. This type exists mainly to allow unambiguous dependency injection.
func AsArguments ¶
AsArguments provides some syntactic sugar around converting a slice of strings into an Arguments.
func StandardArguments ¶
func StandardArguments() Arguments
StandardArguments returns the command-line arguments passed to this process, i.e. os.Args[1:].