Tenekon.CommandLine.Extensions.PolyType
0.0.1-alpha.1
Prefix Reserved
dotnet add package Tenekon.CommandLine.Extensions.PolyType --version 0.0.1-alpha.1
NuGet\Install-Package Tenekon.CommandLine.Extensions.PolyType -Version 0.0.1-alpha.1
<PackageReference Include="Tenekon.CommandLine.Extensions.PolyType" Version="0.0.1-alpha.1" />
<PackageVersion Include="Tenekon.CommandLine.Extensions.PolyType" Version="0.0.1-alpha.1" />
<PackageReference Include="Tenekon.CommandLine.Extensions.PolyType" />
paket add Tenekon.CommandLine.Extensions.PolyType --version 0.0.1-alpha.1
#r "nuget: Tenekon.CommandLine.Extensions.PolyType, 0.0.1-alpha.1"
#:package Tenekon.CommandLine.Extensions.PolyType@0.0.1-alpha.1
#addin nuget:?package=Tenekon.CommandLine.Extensions.PolyType&version=0.0.1-alpha.1&prerelease
#tool nuget:?package=Tenekon.CommandLine.Extensions.PolyType&version=0.0.1-alpha.1&prerelease
Tenekon.CommandLine.Extensions.PolyType
System.CommandLine is a powerful parser, but composing a class-based CLI can get verbose. Tenekon.CommandLine.Extensions.PolyType provides a declarative, attribute-driven layer on top of System.CommandLine using PolyType for fast, strongly-typed binding with no runtime reflection.
Getting started
Install the package:
dotnet add package Tenekon.CommandLine.Extensions.PolyType
Add PolyType so shapes are generated for your command types:
dotnet add package PolyType
Prerequisites
- Any project that can reference
netstandard2.0(the package also shipsnet10.0). - Use the PolyType source generator (
[GenerateShape]) for command types. - Command types must be
partial(enforced by diagnostics).
Usage (class-based model)
In Program.cs:
using Tenekon.CommandLine.Extensions.PolyType;
var app = CommandLineApp.CreateFromType<RootCommand>();
return app.Run(args);
Create a command class:
using PolyType;
using PolyType.SourceGenModel;
using Tenekon.CommandLine.Extensions.PolyType;
[GenerateShape(IncludeMethods = MethodShapeFlags.PublicInstance)]
[CommandSpec(Description = "A root command")]
public partial class RootCommand
{
[OptionSpec(Description = "Greeting target")]
public string Name { get; set; } = "world";
[ArgumentSpec(Description = "Input file")]
public string? File { get; set; }
public int Run(CommandLineContext context)
{
if (context.IsEmptyCommand())
{
context.ShowHelp();
return 0;
}
Console.WriteLine($"Hello {Name}");
Console.WriteLine($"File = {File}");
return 0;
}
}
Async handler:
public Task<int> RunAsync(CommandLineContext context, CancellationToken token)
{
// ...
return Task.FromResult(0);
}
Summary
- Mark command classes with
[CommandSpec]. - Mark properties with
[OptionSpec]or[ArgumentSpec]. - Add
Run/RunAsynchandler methods. - Use
CommandLineApp.CreateFromType<TCommand>()to run.
Handler signatures
Supported signatures:
void Run()/int Run()Task RunAsync()/Task<int> RunAsync()
Optional parameters:
CommandLineContextas the first parameterCancellationTokenas the last parameter- Any other parameters are resolved from
IServiceProvider
Parsing without invocation
var app = CommandLineApp.CreateFromType<RootCommand>();
var result = app.Parse(args);
if (result.ParseResult.Errors.Count > 0)
{
// handle errors
}
var instance = result.Bind<RootCommand>();
Inspecting results
CommandLineResult exposes helpers for more advanced flows:
BindAll()/BindCalled()TryBindCalled(out object?)Contains<T>()/IsCalled<T>()TryGetCalledType(out Type?)TryGetBinder(Type commandType, Type targetType, out Action<object, ParseResult>?)
CommandLineApp creation
You can create an app using a custom shape provider:
var app = CommandLineApp.CreateFromProvider(
commandType: typeof(RootCommand),
commandTypeShapeProvider: provider,
settings: null,
serviceProvider: null);
Help output
Help output comes from System.CommandLine and is generated automatically from your specs.
The header uses assembly metadata, the description comes from [CommandSpec].Description.
You can also call helpers on CommandLineContext:
ShowHelp()ShowHierarchy()ShowValues()IsEmptyCommand()
Naming conventions
By default, names are auto-generated:
- Command/option/argument names are generated from class/property names.
- Common suffixes (
Command,Option,Argument,Directive, etc.) are stripped. - Names are converted to
kebab-case. - Options get
--longand short aliases like-o1.
You can override naming via [CommandSpec]:
Name,Alias,AliasesNameAutoGenerate,NameCasingConvention,NamePrefixConventionShortFormAutoGenerate,ShortFormPrefixConventionOrder,Hidden,TreatUnmatchedTokensAsErrors
Command composition
Use parent/child relationships or nested types:
[CommandSpec(Description = "Root")]
public partial class RootCommand
{
[CommandSpec(Description = "Child command")]
public partial class ChildCommand
{
public void Run() { }
}
}
Or link by type:
[CommandSpec(Description = "Root", Children = new[] { typeof(ChildCommand) })]
public partial class RootCommand { }
[CommandSpec(Parent = typeof(RootCommand), Description = "Child")]
public partial class ChildCommand { }
Options and arguments
[OptionSpec] supports:
Name,Alias,Aliases,Description,HelpName,Hidden,OrderRecursive,AllowMultipleArgumentsPerTokenAllowedValuesRequired,ArityValidationRules,ValidationPattern,ValidationMessage
[ArgumentSpec] supports:
Name,Description,HelpName,Hidden,OrderAllowedValuesRequired,ArityValidationRules,ValidationPattern,ValidationMessage
If Required isn’t specified, it’s inferred from nullability and default values.
Arity can be forced via the attribute or inferred for required arguments.
Validation and allowed values
ValidationRules includes file/path/URL rules and can be combined with bitwise OR.
Use ValidationPattern and ValidationMessage for custom regex validation.
Directives
Define custom directives via [DirectiveSpec]:
[DirectiveSpec]
public bool Debug { get; set; }
Supported directive property types:
boolstringstring[]
[DirectiveSpec] supports:
Name,Description,Hidden,Order
Built-in directives are configurable in CommandLineSettings:
EnableDiagramDirectiveEnableSuggestDirectiveEnableEnvironmentVariablesDirective
Response files
System.CommandLine response files are supported. You can customize token replacement via:
CommandLineSettings.ResponseFileTokenReplacer.
Dependency injection
Provide a service provider for constructor injection and handler parameters:
var services = new ServiceCollection();
services.AddSingleton<MyService>();
var provider = services.BuildServiceProvider();
var app = CommandLineApp.CreateFromType<RootCommand>(
settings: null,
serviceProvider: provider,
commandTypeShapeProvider: null);
return app.Run(args);
Per-invocation override:
var config = new CommandInvocationConfiguration { ServiceProvider = provider };
return app.Run(args, config);
Interface-based specs
Option/argument specs can live on interfaces. Use PolyType’s [GenerateShapeFor]
to generate shapes for those interfaces, and the attributes will be picked up from the interface definition.
Settings
CommandLineSettings controls:
- default exception handler
- help on empty commands
- built-in directives
- response file token replacement
- output/error writers
- POSIX bundling
Trimming and AOT
This library is reflection-free at runtime when you use PolyType source-generated shapes, which makes it friendly for trimming and Native AOT deployments.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
- PolyType (>= 1.1.1)
- System.CommandLine (>= 2.0.3)
- Tenekon.MethodOverloads.SourceGenerator (>= 0.0.5)
-
net10.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.3)
- PolyType (>= 1.1.1)
- System.CommandLine (>= 2.0.3)
- Tenekon.MethodOverloads.SourceGenerator (>= 0.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-alpha.1 | 35 | 2/12/2026 |
Initial public release. Attribute-based command definitions for System.CommandLine powered by PolyType (no reflection), with strongly-typed binding, DI support, and command composition.