Skip to content

Working with CLI

David Kallesen edited this page Dec 22, 2025 · 1 revision

πŸ› οΈ Working with the atc-rest-api-gen CLI

The atc-rest-api-gen CLI tool provides a command-line interface for setting up projects that use the Atc.Rest.Api.SourceGenerator. It validates OpenAPI specifications, creates project scaffolds, and manages configuration files.

πŸ“¦ Installation

# Install as a global tool
dotnet tool install -g Atc.Rest.Api.CliGenerator

# Or run directly from source
dotnet run --project src/Atc.Rest.Api.CliGenerator/Atc.Rest.Api.CliGenerator.csproj -- <commands>

πŸ“‹ Command Overview

atc-rest-api-gen
β”œβ”€β”€ validate
β”‚   └── schema          # βœ… Validate an OpenAPI specification file
β”œβ”€β”€ generate
β”‚   β”œβ”€β”€ server          # πŸ–₯️ Generate server contracts and domain handler scaffolds
β”‚   └── client          # πŸ“± Generate a client project
└── options
    β”œβ”€β”€ create          # πŸ“ Create a default ApiGeneratorOptions.json file
    └── validate        # βœ… Validate an ApiGeneratorOptions.json file

βœ… Validate Commands

validate schema

Validates an OpenAPI specification file against the ATC validation rules.

atc-rest-api-gen validate schema -s <PATH> [--no-strict]

βš™οΈ Options:

Option Description
-s, --specification <PATH> (Required) Path to the OpenAPI specification file (YAML or JSON)
--no-strict Disable strict validation mode

πŸ“ Examples:

# βœ… Validate with strict mode (default)
atc-rest-api-gen validate schema -s api.yaml

# πŸ“‹ Validate with standard mode (less strict)
atc-rest-api-gen validate schema -s api.yaml --no-strict

βš™οΈ Generate Commands

πŸ–₯️ generate server

Generates server contracts and optionally domain handler scaffolds from an OpenAPI specification.

atc-rest-api-gen generate server -s <PATH> -o <PATH> -n <NAME> [options]

πŸ“Œ Required Options:

Option Description
-s, --specification <PATH> Path to the OpenAPI specification file
-o, --output <PATH> Output directory for the contracts project
-n, --name <NAME> Name of the contracts project

πŸ”§ Common Options:

Option Description
--options <PATH> Path to ApiGeneratorOptions.json file
--no-strict Disable strict validation mode
--include-deprecated Include deprecated operations and schemas
--namespace <NAMESPACE> Custom namespace prefix

πŸ–₯️ Server-Specific Options:

Option Description
--sub-folder-strategy <STRATEGY> None, FirstPathSegment, or OpenApiTag
--versioning-strategy <STRATEGY> None, QueryString, Header, UrlSegment, or Combined
--default-api-version <VERSION> Default API version (e.g., 1.0)
--domain-output <PATH> Output path for domain handler scaffolds
--handler-suffix <SUFFIX> Suffix for handler class names
--stub-implementation <TYPE> throw-not-implemented, error-501, or default-value

πŸ—οΈ Scaffolding Options:

Option Description
--project-structure <TYPE> SingleProject, TwoProjects, or TreeProjects (default)
--no-coding-rules Skip adding ATC coding rules files
--host-ui <TYPE> None, Swagger, or Scalar (default)
--host-ui-mode <MODE> DevelopmentOnly (default) or Always
--aspire Create an Aspire AppHost project

πŸ“ Project Name Overrides:

Option Description
--host-project <NAME> Override the host project name
--contracts-project <NAME> Override the contracts project name
--domain-project <NAME> Override the domain project name

πŸ“› Namespace Overrides:

Option Description
--host-namespace <NAMESPACE> Override the host namespace
--contracts-namespace <NAMESPACE> Override the contracts namespace
--domain-namespace <NAMESPACE> Override the domain namespace

πŸ“ Examples:

# πŸ—οΈ Generate server with default settings (TreeProjects mode - 3 projects)
atc-rest-api-gen generate server \
  -s api.yaml \
  -o ./my-api \
  -n DemoApi

# πŸ”’ Generate server with URL segment versioning
atc-rest-api-gen generate server \
  -s api.yaml \
  -o ./my-api \
  -n DemoApi \
  --versioning-strategy UrlSegment \
  --default-api-version 2.0

# πŸ“¦ Single project mode
atc-rest-api-gen generate server \
  -s api.yaml \
  -o output/Demo.Api \
  -n Demo.Api \
  --project-structure SingleProject

# ☁️ Generate with Aspire AppHost
atc-rest-api-gen generate server \
  -s api.yaml \
  -o ./my-api \
  -n DemoApi \
  --aspire

# πŸ“ Custom project names
atc-rest-api-gen generate server \
  -s api.yaml \
  -o ./my-api \
  -n DemoApi \
  --host-project MyDemoApi \
  --contracts-project Api.Contracts \
  --domain-project MyDemo.DomainStuff

πŸ“‚ Repository Structure (TreeProjects mode):

my-api/
β”œβ”€β”€ DemoApi.slnx                        # πŸ“‹ Solution file
β”œβ”€β”€ .gitignore
β”œβ”€β”€ Directory.Build.props
β”œβ”€β”€ atc-coding-rules-updater.json
β”œβ”€β”€ atc-coding-rules-updater.ps1
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ DemoApi.Api/                    # πŸ–₯️ Host project
β”‚   β”‚   β”œβ”€β”€ DemoApi.Api.csproj
β”‚   β”‚   └── Program.cs
β”‚   β”œβ”€β”€ DemoApi.Api.Contracts/          # πŸ“ Contracts project
β”‚   β”‚   β”œβ”€β”€ DemoApi.Api.Contracts.csproj
β”‚   β”‚   β”œβ”€β”€ api.yaml
β”‚   β”‚   └── .atc-rest-api-server-contracts
β”‚   β”œβ”€β”€ DemoApi.Domain/                 # 🏒 Domain project
β”‚   β”‚   β”œβ”€β”€ DemoApi.Domain.csproj
β”‚   β”‚   β”œβ”€β”€ api.yaml
β”‚   β”‚   └── .atc-rest-api-server-handlers
β”‚   └── DemoApi.Aspire/                 # ☁️ Aspire AppHost (optional)
β”‚       β”œβ”€β”€ DemoApi.Aspire.csproj
β”‚       └── Program.cs
└── test/
    └── DemoApi.Tests/
        β”œβ”€β”€ DemoApi.Tests.csproj
        └── UnitTest1.cs

πŸ“± generate client

Generates a client project from an OpenAPI specification.

atc-rest-api-gen generate client -s <PATH> -o <PATH> -n <NAME> [options]

πŸ“Œ Required Options:

Option Description
-s, --specification <PATH> Path to the OpenAPI specification file
-o, --output <PATH> Output directory for the client project
-n, --name <NAME> Name of the client project

πŸ“± Client-Specific Options:

Option Description
--generation-mode <MODE> TypedClient or EndpointPerOperation
--client-suffix <SUFFIX> Suffix for HTTP client class name
--no-oauth Disable OAuth2 token management generation

πŸ“ Examples:

# πŸ“± Generate client with TypedClient mode (default)
atc-rest-api-gen generate client \
  -s api.yaml \
  -o output/Demo.Client \
  -n Demo.Client

# 🎯 Generate client with EndpointPerOperation mode
atc-rest-api-gen generate client \
  -s api.yaml \
  -o output/Demo.Client \
  -n Demo.Client \
  --generation-mode EndpointPerOperation

βš™οΈ Options Commands

πŸ“ options create

Creates a default ApiGeneratorOptions.json file.

atc-rest-api-gen options create -o <PATH> [--force]

βš™οΈ Options:

Option Description
-o, --output <PATH> (Required) Output path for the options file
--force Overwrite existing file if present

βœ… options validate

Validates an existing ApiGeneratorOptions.json file.

atc-rest-api-gen options validate -o <PATH>

πŸ“‹ ApiGeneratorOptions.json Configuration

The ApiGeneratorOptions.json file provides centralized configuration for all code generation settings. The CLI auto-discovers this file in:

  1. πŸ“ Explicitly specified via --options <PATH>
  2. πŸ“‚ Same directory as the OpenAPI specification
  3. πŸ“ Parent directory of the specification
  4. πŸ“‚ Current working directory

πŸ“„ Full Configuration Schema

{
  "general": {
    "validateSpecificationStrategy": "Strict",
    "includeDeprecated": false
  },
  "scaffolding": {
    "projectStructure": "TreeProjects",
    "noCodingRules": false,
    "testFramework": "xunit",
    "targetFramework": "net10.0",
    "hostUi": "Scalar",
    "hostUiMode": "DevelopmentOnly"
  },
  "server": {
    "namespace": null,
    "subFolderStrategy": "FirstPathSegment",
    "useMinimalApiPackage": "auto",
    "useValidationFilter": "auto",
    "useGlobalErrorHandler": "auto",
    "versioningStrategy": "None",
    "defaultApiVersion": "1.0",
    "domain": {
      "generateHandlersOutput": "ApiHandlers",
      "subFolderStrategy": "None",
      "handlerSuffix": "Handler",
      "stubImplementation": "throw-not-implemented"
    }
  },
  "client": {
    "namespace": null,
    "generationMode": "TypedClient",
    "clientSuffix": "Client",
    "generateOAuthTokenManagement": true
  }
}

πŸ“š Configuration Options Reference

πŸ”§ General Options

Option Type Default Description
validateSpecificationStrategy enum "Strict" None, Standard, or Strict
includeDeprecated bool false Include deprecated operations

πŸ—οΈ Scaffolding Options

Option Type Default Description
projectStructure enum "TreeProjects" SingleProject, TwoProjects, or TreeProjects
noCodingRules bool false Skip ATC coding rules files
testFramework string "xunit" Test framework for generated test project
targetFramework string "net10.0" Target framework
hostUi enum "Scalar" None, Swagger, or Scalar
hostUiMode enum "DevelopmentOnly" DevelopmentOnly or Always

πŸ–₯️ Server Options

Option Type Default Description
namespace string? null Custom namespace (null = auto-detect)
subFolderStrategy enum "FirstPathSegment" None, FirstPathSegment, OpenApiTag
versioningStrategy enum "None" None, QueryString, Header, UrlSegment, Combined
defaultApiVersion string "1.0" Default API version
useMinimalApiPackage enum "auto" auto, enabled, disabled
useValidationFilter enum "auto" auto, enabled, disabled

🏒 Server Domain Options

Option Type Default Description
generateHandlersOutput string "ApiHandlers" Output folder for handlers
subFolderStrategy enum "None" None, FirstPathSegment, OpenApiTag
handlerSuffix string "Handler" Handler class name suffix
stubImplementation string "throw-not-implemented" throw-not-implemented, error-501, default-value

πŸ“± Client Options

Option Type Default Description
namespace string? null Custom namespace (null = auto-detect)
generationMode enum "TypedClient" TypedClient or EndpointPerOperation
clientSuffix string "Client" HTTP client class name suffix
generateOAuthTokenManagement bool true Generate OAuth2 token management

πŸ”„ CLI vs Source Generator

The atc-rest-api-gen CLI is a setup tool, not a code generator itself. It:

  1. βœ… Validates your OpenAPI specification
  2. πŸ“ Creates project scaffolds with correct structure
  3. βš™οΈ Configures marker files that trigger the source generator
  4. πŸ“ Generates .csproj files with proper references

The actual code generation happens when you run dotnet build on the created projects.

πŸ”„ Workflow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     πŸ› οΈ atc-rest-api-gen CLI                   β”‚
β”‚                                                               β”‚
β”‚  1. βœ… Validates OpenAPI specification                        β”‚
β”‚  2. πŸ“ Creates project directories                            β”‚
β”‚  3. πŸ“„ Copies specification files                             β”‚
β”‚  4. βš™οΈ Creates marker files (.atc-rest-api-*)                 β”‚
β”‚  5. πŸ“ Creates .csproj files with source generator reference  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                 β”‚
                                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       πŸ”¨ dotnet build                         β”‚
β”‚                                                               β”‚
β”‚  1. πŸ“– Source generator reads marker file configuration       β”‚
β”‚  2. πŸ“„ Source generator parses OpenAPI specification          β”‚
β”‚  3. βš™οΈ Source generator emits C# code to obj/Generated/       β”‚
β”‚  4. πŸ”§ Compiler includes generated code in compilation        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”’ Exit Codes

Code Description
0 βœ… Success
1 ❌ Failure (validation errors, file errors, etc.)

🏠 Home

πŸ“– Getting Started

βš™οΈ Features

πŸ“‹ Reference


πŸ”— Resources

Clone this wiki locally