cli: nested
Exhibit path: docs/examples/codegen/cli/nested/
A two-level command tree: a parent command with two subcommands. Shows how parent=<name> links subcommands to their parent and how the generated registry wires the full tree.
Input (in.go)
go
//go:build glacier_codegen_fixture
// Package main is the entry point for the myapp CLI.
package main
import (
"context"
)
// MyApp is the root command.
//
// +glacier:command name=myapp
// +glacier:root
type MyApp struct{}
// Run is a no-op; subcommands do the work.
func (MyApp) Run(_ context.Context) error { return nil }
// StartCmd starts the server.
//
// +glacier:command name=start parent=myapp
type StartCmd struct {
// Port is the TCP port to listen on.
//
// +glacier:default 8080
// +glacier:short s
Port int
}
// Run starts the server.
func (c *StartCmd) Run(_ context.Context) error {
// implementation goes here
return nil
}
// StopCmd stops the server gracefully.
//
// +glacier:command name=stop parent=myapp
type StopCmd struct {
// Timeout is the graceful-shutdown timeout in seconds.
//
// +glacier:default 30
Timeout int
}
// Run stops the server.
func (c *StopCmd) Run(_ context.Context) error {
// implementation goes here
return nil
}Output (out.go)
go
//go:build glacier_codegen_fixture
// Code generated by glaciergen. DO NOT EDIT.
// Source: in.go
package main
import (
"github.com/nathanbrophy/glacier/cli"
)
func init() {
cli.Default.Register(
cli.WithName("myapp"),
cli.WithRoot(),
cli.WithRunner(func() cli.Runner { return &MyApp{} }),
)
cli.Default.Register(
cli.WithName("start"),
cli.WithParent("myapp"),
cli.WithRunner(func() cli.Runner { return &StartCmd{} }),
cli.WithFlagDefault("Port", 8080),
cli.WithFlagShort("Port", 's'),
)
cli.Default.Register(
cli.WithName("stop"),
cli.WithParent("myapp"),
cli.WithRunner(func() cli.Runner { return &StopCmd{} }),
cli.WithFlagDefault("Timeout", 30),
)
}What the generator did
- Registered the root command via
+glacier:root. - Registered
StartCmdandStopCmdas children of the root usingparent=myapp. - The generated registry dispatches
myapp startandmyapp stopto the respectiveRunmethods. +glacier:short sonStartCmd.Portemittedcli.WithFlagShort("Port", 's')in the generated file.