Skip to content

CLI Tool

SourceGen.Ioc.Cli is a command-line tool that helps you quickly add [IocRegister] attributes to existing classes or generate [IocRegisterFor] annotations in your project.

Installation

bash
dotnet tool install -g SourceGen.Ioc.Cli

Commands

Add Attribute Command

Add [IocRegister] attribute to classes matching the specified criteria.

bash
sourcegen-ioc add [options]

Options

OptionDescriptionDefault
-t, --targetTarget directory or file, defaults to current directorynull (current directory)
-f, --file-patternFile pattern to filter files*.cs
-s, --search-sub-directoriesWhether to search sub directoriesfalse
-cn, --class-name-regexRegex pattern to match class namesnull
--full-regexFull regex pattern to match file contentnull
--attribute-nameName of the attribute to addIocRegister
-m, --max-applyHow many matches should apply, 0 means unlimited0
-n, --dry-runDry run, does not modify filesfalse
-v, --verboseDetailed logging messagefalse
--logLogging file path""

NOTE

You must specify at least one matcher: -cn/--class-name-regex or --full-regex.

Class Name Regex

When using -cn or --class-name-regex option, the actual regex used is:

regex
(public|internal)\s+(?!static\s+)[\w\s]*class\s+(classNameRegex)(?=\s|:|$)

This matches non-static public or internal classes.

IMPORTANT

The tool automatically converts .* to \w* and .+ to \w+ in your pattern to ensure only valid identifier characters are matched.

Examples

bash
# Search all .cs files in current directory and add attributes
sourcegen-ioc add -cn ".*"

# Search specified directory including subdirectories
sourcegen-ioc add -t ./src -s -cn ".*"

# Match only classes ending with Service
sourcegen-ioc add -cn ".*Service"

# Match only classes ending with Handler, including subdirectories
sourcegen-ioc add -cn ".*Handler" -s

# Dry run to see which files would be modified
sourcegen-ioc add -cn ".*Repository" -n -v

# Limit to 10 matches maximum
sourcegen-ioc add -cn ".*Service" -m 10

# Use full regex pattern
sourcegen-ioc add --full-regex "public\s+class\s+\w+Controller"

Generate IocRegisterFor Command

Collect classes matching the specified criteria and generate a file with [IocRegisterFor] assembly attributes. This is useful when you want to register classes from external assemblies or when you prefer assembly-level attributes.

bash
sourcegen-ioc generate ioc-for [options]

Options

OptionDescriptionDefault
-o, --output-pathOutput file path (required)N/A
-t, --targetTarget directory or file, defaults to current directorynull (current directory)
-f, --file-patternFile pattern to filter files*.cs
-s, --search-sub-directoriesWhether to search sub directoriesfalse
-cn, --class-name-regexRegex pattern to match class names (required)N/A
-m, --max-applyHow many matches should apply, 0 means unlimited0
-ig, --is-generic-attributeWhether to generate generic attribute syntaxfalse
-n, --dry-runDry run, does not create filefalse
-v, --verboseDetailed logging messagefalse
--logLogging file path""

Class Name and Namespace Extraction

When using -cn or --class-name-regex, the tool extracts the class name from regex group 2 and prepends the namespace.

NOTE

The namespace is extracted from only the first namespace declaration in each file. If a file contains multiple namespaces, only the first one will be used as the prefix for all matched classes in that file.

Examples

bash
# Generate IocRegisterFor for all Handler classes
sourcegen-ioc generate ioc-for -o ./Generated/Handlers.g.cs -t ./src -s -cn ".*Handler"

# Generate with generic attribute syntax
sourcegen-ioc generate ioc-for -o ./Generated/Services.g.cs -cn ".*Service" -ig

# Preview output without creating file (dry-run)
sourcegen-ioc generate ioc-for -o ./output.cs -cn ".*Repository" -n -v

# Limit to 5 classes
sourcegen-ioc generate ioc-for -o ./output.cs -cn ".*Service" -m 5

Output Format

The command generates a file with the following format:

Standard syntax (default):

csharp
// <auto-generated />
using SourceGen.Ioc;
[assembly: IocRegisterFor(typeof(MyApp.Services.UserService))]
[assembly: IocRegisterFor(typeof(MyApp.Services.OrderService))]
[assembly: IocRegisterFor(typeof(MyApp.Services.ProductService))]

Generic syntax (with -ig flag):

csharp
// <auto-generated />
using SourceGen.Ioc;
[assembly: IocRegisterFor<MyApp.Services.UserService>]
[assembly: IocRegisterFor<MyApp.Services.OrderService>]
[assembly: IocRegisterFor<MyApp.Services.ProductService>]

Generate IocRegisterDefaults Command

Collect classes matching the specified criteria and generate a file with [IocRegisterDefaults] assembly attributes. This command groups implementation classes by their base type/interface and generates registration attributes with ImplementationTypes.

bash
sourcegen-ioc generate ioc-defaults [options]

Options

OptionDescriptionDefault
-o, --output-pathOutput file path (required)N/A
-t, --targetTarget directory or file, defaults to current directorynull (current directory)
-f, --file-patternFile pattern to filter files*.cs
-s, --search-sub-directoriesWhether to search sub directoriesfalse
-cn, --class-name-regexRegex pattern to match class names (required)N/A
-b, --base-type-regexRegex pattern to match interface/base class (required)N/A
-m, --max-applyHow many matches should apply, 0 means unlimited0
-ig, --is-generic-attributeWhether to generate generic attribute syntaxfalse
-l, --lifetimeLifetime for the service registration. Valid values: Singleton, Scoped, TransientTransient
-n, --dry-runDry run, does not create filefalse
-v, --verboseDetailed logging messagefalse
--logLogging file path""

NOTE

-cn and -b options must be specified together.

-l is case-insensitive and is emitted as ServiceLifetime.{PascalCase} in generated output.

Regex

When using -cn and -b options, the actual regex used is:

regex
(public|internal)\s+(?!static\s+)[\w\s]*class\s+(classNameRegex)\s*:\s*(?:[^,{\n]+,\s*)*(baseTypeRegex)(?=\s*[,{]|$)

This matches non-static public or internal classes that inherit from the specified base type/interface.

  • Group 2: Class name (matched by classNameRegex)
  • Group 3: Base type/interface (matched by baseTypeRegex)

IMPORTANT

The tool sanitizes wildcard patterns before building the regex:

  • For -cn (class name): .* -> \w*, .+ -> \w+
  • For -b (base type): .* -> [\w.<>]*, .+ -> [\w.<>]+

Class Name and Namespace Extraction

When using -cn and -b, the tool extracts the class name from regex group 2 and the base type from regex group 3, then prepends the namespace to the class name.

NOTE

The namespace is extracted from only the first namespace declaration in each file. If a file contains multiple namespaces, only the first one will be used as the prefix for all matched classes in that file.

Examples

bash
# Generate IocRegisterDefaults for all Handler classes implementing IHandler
sourcegen-ioc generate ioc-defaults -o ./Generated/Handlers.g.cs -t ./src -s -cn ".*Handler" -b "IHandler"

# Generate with generic attribute syntax
sourcegen-ioc generate ioc-defaults -o ./Generated/Services.g.cs -cn ".*Service" -b "I.*Service" -ig

# Preview output without creating file (dry-run)
sourcegen-ioc generate ioc-defaults -o ./output.cs -cn ".*Repository" -b "IRepository" -n -v

# Match classes with generic interface
sourcegen-ioc generate ioc-defaults -o ./output.cs -cn ".*Handler" -b "IHandler<.*>" -s

# Generate with Singleton lifetime
sourcegen-ioc generate ioc-defaults -o ./Generated/Services.g.cs -cn ".*Service" -b "I.*Service" -l Singleton

Output Format

The command generates a file with the following format:

Standard syntax (default):

csharp
// <auto-generated />
using SourceGen.Ioc;
using Microsoft.Extensions.DependencyInjection;
[assembly: IocRegisterDefaults(typeof(IHandler), ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Handlers.CommandHandler), typeof(MyApp.Handlers.QueryHandler)])]
[assembly: IocRegisterDefaults(typeof(IService), ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Services.UserService)])]

Generic syntax (with -ig flag):

csharp
// <auto-generated />
using SourceGen.Ioc;
using Microsoft.Extensions.DependencyInjection;
[assembly: IocRegisterDefaults<IHandler>(ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Handlers.CommandHandler), typeof(MyApp.Handlers.QueryHandler)])]
[assembly: IocRegisterDefaults<IService>(ServiceLifetime.Transient, ImplementationTypes = [typeof(MyApp.Services.UserService)])]

CLI Schema Command

Output CLI schema in JSON format for LLM AI.

bash
sourcegen-ioc cli-schema [options]

Options

OptionDescriptionDefault
-c, --commandCommand namenull (all)
-t, --targetTarget file/folder to write CLI schemanull (stdout)
-n, --dry-runDry runfalse
-v, --verboseDetailed logging messagefalse
--logLogging file path""

Examples

bash
# Output schema for all commands
sourcegen-ioc cli-schema

# Output schema for specific command
sourcegen-ioc cli-schema -c "add"

# Write schema to file
sourcegen-ioc cli-schema -t ./cli-schema.json

Use Cases

Add DI Registration to Existing Project

When you have an existing project and want to batch add [IocRegister] attributes:

bash
# 1. Use dry-run first to preview which files will be affected
sourcegen-ioc add -t ./src/MyProject -s -cn ".*Service" -n -v

# 2. Execute after confirming the changes
sourcegen-ioc add -t ./src/MyProject -s -cn ".*Service"

Generate Registration file

When you want to register classes in one file:

bash
# Generate IocRegisterFor attributes for all handlers
sourcegen-ioc generate ioc-for -o ./Generated/Handlers.g.cs -t ../Project/src -s -cn ".*Handler"

Generate Defaults Registration file

When you want to register classes grouped by their base type/interface:

bash
# Generate IocRegisterDefaults for handlers grouped by interface
sourcegen-ioc generate ioc-defaults -o ./Generated/HandlerDefaults.g.cs -t ./src -s -cn ".*Handler" -b "IHandler"

# Generate for multiple base types
sourcegen-ioc generate ioc-defaults -o ./Generated/Defaults.g.cs -t ./src -s -cn ".*" -b "I.*"

Add Attributes to Handler Classes

bash
sourcegen-ioc add -t ./src/Handlers -s -cn ".*Handler"

Add Attributes to Repository Classes

bash
sourcegen-ioc add -t ./src/Repositories -s -cn ".*Repository"

← Back to Overview

Released under the MIT License.