Albatross.CommandLine.CodeGen 7.8.7

Prefix Reserved
This package has a SemVer 2.0.0 package version: 7.8.7+ba93161.
dotnet add package Albatross.CommandLine.CodeGen --version 7.8.7
                    
NuGet\Install-Package Albatross.CommandLine.CodeGen -Version 7.8.7
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Albatross.CommandLine.CodeGen" Version="7.8.7">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Albatross.CommandLine.CodeGen" Version="7.8.7" />
                    
Directory.Packages.props
<PackageReference Include="Albatross.CommandLine.CodeGen">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Albatross.CommandLine.CodeGen --version 7.8.7
                    
#r "nuget: Albatross.CommandLine.CodeGen, 7.8.7"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Albatross.CommandLine.CodeGen&version=7.8.7
                    
Install Albatross.CommandLine.CodeGen as a Cake Addin
#tool nuget:?package=Albatross.CommandLine.CodeGen&version=7.8.7
                    
Install Albatross.CommandLine.CodeGen as a Cake Tool

About

A companion code generator used by the Albatross.CommandLine library. It supports generation of arguments, commands, subcommands and options with dependency injection.

Quick Start

As a development dependency of Albatross.CommandLine library, codegen will be referenced automatically as a PrivateAssets when the reference for Albatross.CommandLine is added to a project. The code generator looks for options classes those are annotated with the Albatross.CommandLine.VerbAttribute and generate the appropriate command classes. In the example below, the class TestOptions, TestCommandHandler and the first TestCommand class are created manually and the second TestCommand class is generated.

  • The command is created as a partial class which allows user to add additional functionalities. To customize a command, create a partial command class of the same name with the interface Albatross.CommandLine.IRequireInitialization.
  • Nullable property are declared as optional and vice vesa. However, requirement can be overwritten using the Albatross.CommandLine.OptionAttribute as shown in the Value property in the example.
  • Option alias can be created using the Albatross.CommandLine.OptionAttribute. Aliases are prefixed with a single dash ('-') if the dash has not been prefixed already.
[Verb("test", typeof(TestCommandHandler), Description = "A test command")]
public record class TestOptions {
	// required since its type is not nullable
	public string Name { get; set; } = string.Empty;
	// not required since its type is nullable
	public string? Description { get; set; }
	// not required since the default behavior is overwritten by the Option attribute
	[Option("v", "value", Required = false, Description = "An integer value")]
	public int Value { get; set; }
}
// implement your command handler logic here
// optionally use BaseHandler<OptionType> class as the base class
public class TestCommandHandler : ICommandHandler {
	...
}
public partial class TestCommand : IRequireInitialization {
	// this method will be call right after object construction
	public void Init() {
		// customize your command here
		...
	}
}
// generated code.  Option properties are created with the Prefix of `Option_`
public sealed partial class TestCommand : Command {
	public TestCommand() : base("test", "A test command") {
		this.Option_Name = new Option<string>("--name", null) {
			IsRequired = true
		};
		this.AddOption(Option_Name);
		this.Option_Description = new Option<string?>("--description", null);
		this.AddOption(Option_Description);
		this.Option_Value = new Option<int>("--value", "An integer value");
		Option_Value.AddAlias("-v");
		this.AddOption(Option_Value);
	}

	public Option<string> Option_Name { get; }
	public Option<string?> Option_Description { get; }
	public Option<int> Option_Value { get; }
}

The second part of the code generator will create the service registration and option binding code. The RegisterCommands method should be invoked by service registration code in the Setup class. AddCommands method is part of the bootstrap code in program.cs file. See Albatross.CommandLine for details.

public static class RegistrationExtensions
	{
		public static IServiceCollection RegisterCommands(this IServiceCollection services) {
			services.AddKeyedScoped<ICommandHandler, TestCommandHandler>("test");
			services.AddOptions<TestOptions>().BindCommandLine();
			return services;
		}

		public static Setup AddCommands(this Setup setup) {
			setup.AddCommand<TestCommand>();
			return setup;
		}
	}

Troubleshooting

In visual studio, EmitCompilerGeneratedFiles property can be added to view the generated code but Visual Studio doesn't work reliably. As an alternative, the code generator will output the debug file albatross.commandline.codegen.txt if the following properties are set in the project file.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <EmitAlbatrossCodeGenDebugFile>true</EmitAlbatrossCodeGenDebugFile>
    </PropertyGroup>
    <ItemGroup>
        <CompilerVisibleProperty Include="EmitAlbatrossCodeGenDebugFile"/>
    </ItemGroup>
</Project>

Rider can display the roslyn generated file reliably. They can be found under Project -> Dependencies -> .NET (Version) -> Source Generators.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Albatross.CommandLine.CodeGen:

Package Downloads
Albatross.CommandLine

An integration library that simplifies the creation of console program using the System.CommandLine library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.8.7 65 3/24/2025
7.8.6 46 3/18/2025
7.8.5 49 3/16/2025
7.8.4 198 3/5/2025
7.8.1 77 12/25/2024
7.8.0 78 12/8/2024
7.6.0 85 11/26/2024
7.5.8 72 11/11/2024
7.5.6 69 11/8/2024
7.5.5 63 11/7/2024