Documentation
¶
Overview ¶
Package optgen implements the optgen code generator CLI as a library: the optgen binary is a thin main around Run, and programs that register their own plugins call Run with WithPlugins.
The package exposes three entry points:
- New returns a ready-to-execute cobra.Command tree (generate, list-plugins, and --version).
- Run is a convenience wrapper that accepts arguments and executes the root command.
- NewGenerate and NewListPlugins return individual subcommands.
Configuration is loaded from .optgen.yaml (see [config.File]) unless --no-config is set. CLI flags always take precedence over config-file values.
Usage ¶
cmd := optgen.New() err := cmd.ExecuteContext(ctx)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New creates the CLI root command. Options such as WithPlugins apply to every subcommand.
func NewGenerate ¶
NewGenerate returns a cobra command that generates functional option functions from struct field tags. The returned command validates inputs in PersistentPreRunE (directory existence, identifier validity, config loading) before running generation.
func NewListPlugins ¶
NewListPlugins returns a cobra command that prints all registered plugins, type default providers, modifier plugins, and optcheck keys. The builtin plugins are registered explicitly for each command execution.
Types ¶
type GenerateCommand ¶
GenerateCommand implements the "generate" subcommand.
It parses the target directory for Go source files, locates the struct type identified by --type, collects opt-tagged fields, runs the plugin pipeline, and writes the generated WithXxx functions to --output (default options_gen.go). Use --dry-run to preview output without writing files.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
optgen
command
Package main runs the optgen command-line application.
|
Package main runs the optgen command-line application. |
|
Package config decodes and merges .optgen.yaml configuration documents.
|
Package config decodes and merges .optgen.yaml configuration documents. |
|
examples
|
|
|
cache
Package cache shows options for a generic struct and --with-isset.
|
Package cache shows options for a generic struct and --with-isset. |
|
customplugin
Package customplugin shows generation with plugins of your own.
|
Package customplugin shows generation with plugins of your own. |
|
customplugin/cmd/optgen
command
Command optgen is the optgen CLI with the plugins of examples/customplugin/myplugins registered after the built-in ones.
|
Command optgen is the optgen CLI with the plugins of examples/customplugin/myplugins registered after the built-in ones. |
|
customplugin/myplugins
Package myplugins implements two optgen plugins: the optval:"collapse" transform and the optcheck:"hostport" check.
|
Package myplugins implements two optgen plugins: the optval:"collapse" transform and the optcheck:"hostport" check. |
|
pipeline
Package pipeline shows several option types generated in one run and a package-local .optgen.yaml.
|
Package pipeline shows several option types generated in one run and a package-local .optgen.yaml. |
|
server
Package server shows the strict end of optgen: options that return errors, optcheck rules re-checked by the constructor, and the built-in field plugins.
|
Package server shows the strict end of optgen: options that return errors, optcheck rules re-checked by the constructor, and the built-in field plugins. |
|
Package internal organizes the private implementation of optgen.
|
Package internal organizes the private implementation of optgen. |
|
application
Package application coordinates generation through consumer-owned ports.
|
Package application coordinates generation through consumer-owned ports. |
|
generator
Package generator renders prepared option declarations using injected plugins.
|
Package generator renders prepared option declarations using injected plugins. |
|
naming
Package naming derives exported option names from private struct field names.
|
Package naming derives exported option names from private struct field names. |
|
output
Package output formats generated source and publishes successful output atomically.
|
Package output formats generated source and publishes successful output atomically. |
|
parser
Package parser extracts opt-tagged declarations without applying plugin policies.
|
Package parser extracts opt-tagged declarations without applying plugin policies. |
|
plugin/builtin
Package builtin registers every built-in plugin: checks, type defaults, field plugins, guards, post-processors, and transforms.
|
Package builtin registers every built-in plugin: checks, type defaults, field plugins, guards, post-processors, and transforms. |
|
plugin/builtin/check
Package check implements value-validation plugins.
|
Package check implements value-validation plugins. |
|
plugin/builtin/defaults
Package defaults provides default expressions during semantic preparation.
|
Package defaults provides default expressions during semantic preparation. |
|
plugin/builtin/fields
Package fields implements functional-option field renderers.
|
Package fields implements functional-option field renderers. |
|
plugin/builtin/fieldtmpl
Package fieldtmpl provides templates and option data shared by the built-in field plugins.
|
Package fieldtmpl provides templates and option data shared by the built-in field plugins. |
|
plugin/builtin/guard
Package guard implements conditional runtime guards.
|
Package guard implements conditional runtime guards. |
|
plugin/builtin/postprocess
Package postprocess implements value cleanup after transformation.
|
Package postprocess implements value cleanup after transformation. |
|
plugin/builtin/transform
Package transform implements string-expression transformations.
|
Package transform implements string-expression transformations. |
|
rootcmd
Package rootcmd constructs CLI roots with generated options and panic recovery.
|
Package rootcmd constructs CLI roots with generated options and panic recovery. |
|
semantic
Package semantic validates declarations and applies explicit plugin policies: the option name prefix, check applicability, type defaults and default normalization, and the default modifiers derived from plugin metadata.
|
Package semantic validates declarations and applies explicit plugin policies: the option name prefix, check applicability, type defaults and default normalization, and the default modifiers derived from plugin metadata. |
|
source
Package source reads Go packages and extracts declarations.
|
Package source reads Go packages and extracts declarations. |
|
Package model defines the public data structures shared between optgen's parser, generator, and plugin subsystems.
|
Package model defines the public data structures shared between optgen's parser, generator, and plugin subsystems. |
|
Package plugin defines extension contracts and per-session plugin execution.
|
Package plugin defines extension contracts and per-session plugin execution. |