> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/gum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/gum/cli/cli/codegen.md).

# codegen

```
gumcli codegen <project.gumx> [--element <name>...]
```

Generates C# code for elements in a Gum project. Runs error checks before generating each element — elements with errors are skipped; elements with only warnings are still generated.

## Options

* `<project.gumx>` — Path to the `.gumx` project file
* `--element <name>` — Generate code only for the named element. Can be specified multiple times. Supports folder-qualified names.

## Examples

```
gumcli codegen MyProject/MyProject.gumx
gumcli codegen MyProject/MyProject.gumx --element Button
gumcli codegen MyProject/MyProject.gumx --element Button --element Slider
gumcli codegen MyProject/MyProject.gumx --element Controls/Button
```

**Output on success:**

```
Generated code for 12 element(s).
```

**Output with blocked elements:**

```
error: ButtonComponent: Base type 'ButtonBase' not found.
Generated code for 11 element(s).
1 element(s) skipped due to errors.
```

## Notes

* Requires `ProjectCodeSettings.codsj` with `CodeProjectRoot` configured. If the file is missing, `codegen` attempts auto-detection first and writes it before continuing. Exit code 2 only if auto-detection also fails — in that case, run [`codegen-init`](/gum/cli/cli/codegen-init.md) first.
* Only Screens and Components are generated. StandardElements are excluded.
* Elements marked `NeverGenerate` in their per-element settings are silently skipped.
* When `--element` is specified, `codegen` also auto-generates code for any referenced elements whose code files do not yet exist.
* Errors go to stderr; the summary line goes to stdout.

## Exit Codes

| Code | Meaning                                                                       |
| ---- | ----------------------------------------------------------------------------- |
| 0    | All eligible elements generated successfully                                  |
| 1    | One or more elements were skipped due to errors                               |
| 2    | Project could not be loaded or code generation settings could not be resolved |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/gum/cli/cli/codegen.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
