program

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 5 Imported by: 0

README

Program

Go Reference

Program provides a common entrypoint for CLI-based applications using di.

Install

go get github.com/livebud/program

Example

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/livebud/cli"
	"github.com/livebud/di"
	"github.com/livebud/program"
)

// Provide an arbitrary CLI that implements `cli.Parser`.
func provideCLI(in di.Injector) (cli.Parser, error) {
	cli := cli.New("cli", "some cli")
	cmd := cli.Command("say", "say hello")
	cmd.Run(func(ctx context.Context) error {
		fmt.Println("hello")
		return nil
	})
	return cli, nil
}

func main() {
	in := di.New()
	di.Provide[cli.Parser](in, provideCLI)
	os.Exit(program.Run(in))
}

Contributors

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(in di.Injector) (cli.Parser, error)

Load a CLI parser from the dependency injector.

func Parse

func Parse(ctx context.Context, in di.Injector, args ...string) error

Parse runs a program, returning an error if there is one.

func Run

func Run(in di.Injector) int

Run a program, returning an exit code.

Example
package main

import (
	"context"
	"fmt"

	"github.com/livebud/cli"
	"github.com/livebud/di"
	"github.com/livebud/program"
)

func main() {
	provideCLI := func(in di.Injector) (cli.Parser, error) {
		cli := cli.New("cli", "some cli")
		cmd := cli.Command("say", "say hello")
		cmd.Run(func(ctx context.Context) error {
			fmt.Println("hello")
			return nil
		})
		return cli, nil
	}
	in := di.New()
	di.Provide[cli.Parser](in, provideCLI)
	ctx := context.Background()
	program.Parse(ctx, in, "say")
}
Output:

hello

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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