package cli_test import ( "context" "fmt" "io" "log/slog" urfavecli "github.com/urfave/cli/v3" "go.auvem.com/corestack/lifecycle" "go.auvem.com/corestack/lifecycle/cli" ) func Example() { cfg := lifecycle.Config{Logger: slog.New(slog.NewTextHandler(io.Discard, nil))} for _, args := range [][]string{{"app", "serve"}, {"app", "inspect", "--verbose"}} { var database string db := lifecycle.Module{Name: "database", Setup: func(context.Context) error { database = "connected"; fmt.Println("open database"); return nil }, Teardown: func(context.Context) error { fmt.Println("close database"); database = ""; return nil }, } server := lifecycle.Module{Name: "server", Setup: func(context.Context) error { fmt.Println("open server"); return nil }, // A real server blocks until cancellation; this finite service keeps the example executable. Run: func(context.Context) error { fmt.Println("serve:", database); return nil }, Teardown: func(context.Context) error { fmt.Println("close server"); return nil }, } cliRoot := &urfavecli.Command{Name: "app", Commands: []*urfavecli.Command{ {Name: "serve", Action: cli.Serve(cfg, []lifecycle.Module{db, server})}, {Name: "inspect", Flags: []urfavecli.Flag{&urfavecli.BoolFlag{Name: "verbose"}}, Action: cli.Action(cfg, []lifecycle.Module{db}, func(_ context.Context, cmd *urfavecli.Command) error { fmt.Println("inspect:", database, "verbose:", cmd.Bool("verbose")) return nil })}, }} code, err := cli.Run(context.Background(), cliRoot, args) fmt.Println("exit:", code, err) // main may call os.Exit(code) here, after adapter cleanup. } // Output: // open database // open server // serve: connected // close server // close database // exit: 0 // open database // inspect: connected verbose: true // close database // exit: 0 }