CommandLine.EasyBuilder 2.0.0-rc1.1

This is a prerelease version of CommandLine.EasyBuilder.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package CommandLine.EasyBuilder --version 2.0.0-rc1.1
                    
NuGet\Install-Package CommandLine.EasyBuilder -Version 2.0.0-rc1.1
                    
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="CommandLine.EasyBuilder" Version="2.0.0-rc1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CommandLine.EasyBuilder" Version="2.0.0-rc1.1" />
                    
Directory.Packages.props
<PackageReference Include="CommandLine.EasyBuilder" />
                    
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 CommandLine.EasyBuilder --version 2.0.0-rc1.1
                    
#r "nuget: CommandLine.EasyBuilder, 2.0.0-rc1.1"
                    
#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.
#:package CommandLine.EasyBuilder@2.0.0-rc1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CommandLine.EasyBuilder&version=2.0.0-rc1.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=CommandLine.EasyBuilder&version=2.0.0-rc1.1&prerelease
                    
Install as a Cake Tool

CommandLine.EasyBuilder

Building upon the terrific System.CommandLine, CommandLine.EasyBuilder makes it easier than ever to make a command line app, offering view-model type of auto-binding input command-line options and arguments to the properties of a POCO class. Simply decorate a class with a Command attribute, and decorate any of its properties with Option or Argument attributes, and quite literally, CommandLine.EasyBuilder takes care of the rest! More often than not this can replace the need to manually wire up via the traditional way. This also makes your command-line much easier to understand, to edit and to alter, and so forth. It's basically declarative decorations on a class and it's properties, building on top of all the tremendous work that System.CommandLine already does.

using System.CommandLine;
using CommandLine.EasyBuilder;
using static System.Console;

namespace EasyBuilder.Samples;

public class ExampleApp_HelloWorld
{
	public static RootCommand GetApp()
	{
		RootCommand rootCmd = new("Command line is cool");
		rootCmd.AddAutoCommand<HellowWorld>();
		return rootCmd;
	}
}

[Command("hello", "Hello commandline world!")]
public class HellowWorld
{
	[Option("--name", "-n", Required = true)]
	public string Name { get; set; }

	[Option(name: "--age", DefVal = 42)]
	public int Age { get; set; }

	[Option("--animal", "-a", Required = true)]
	public FavoriteAnimal FavAnimal { get; set; } = FavoriteAnimal.Cheetah;

	public void Handle()
		=> WriteLine($"Hello {Name} ({Age}), glad to see you love {FavAnimal}s!");
}

public enum FavoriteAnimal { None = 0, Dog = 1, Cat = 2, Cheetah = 3, Rhino = 4 }

In this example, simply add this class to a Rootcommand (or sub-command Command): rootCmd.AddAutoCommand<HelloWorld>(), and all the magic is taken care of. From there, run the traditional way, once you have a Rootcommand. We're not demonstrating that here, because none of that changes, but you can see the example app.

Add either a void Handle or Task HandleAsync method to your POCO class, and they will be auto-found and called when Invoke or InvokeAsync is called on the ParseResult (ie same as usual).

Other examples are shown in the EasyBuilder.SampleConsoleApps app.

For a larger example, see GetStartedTutorialSimple.cs shown below. This takes the final app demonstrated in this System.CommandLine getting started tutorial, but instead builds it in the CommandLine.EasyBuilder style with POCO command classes. This example with inline comments can serve as documentation for a number of scenarios.

using System.CommandLine;
using CommandLine.EasyBuilder;
using static System.Console;
using FileIO = System.IO.File;

namespace EasyBuilder.Samples;

public class GetStartedTutorial_Auto
{
	public RootCommand GetApp()
	{
		RootCommand rootCmd = new("Sample app for System.CommandLine");

		Command quotesCmd = rootCmd.AddAutoCommand<QuotesCmd>();

		Command readCmd = quotesCmd.AddAutoCommand<ReadCmd>();
		quotesCmd.AddAutoCommand<DeleteCmd>();
		quotesCmd.AddAutoCommand<AddCmd>();

		ShowExtraOptions(readCmd);

		return rootCmd;
	}

	/// <summary>Demonstrates how to modify auto constructed options / arguments</summary>
	void ShowExtraOptions(Command readCmd)
	{
		readCmd.Options.First(o => o.Name == "--fgcolor").Alias("-fg");

		// example if you need the fully generic typed version eg Option<T> ...
		Option<bool> opt = readCmd.Options.First(o => o.Name == "--light-mode") as Option<bool>;
		opt.Alias("-lm");
		//opt.Arity = new ArgumentArity(0, 2);
	}
}

[Command("quotes", "Work with a file that contains quotes.")]
public class QuotesCmd { }

[Command("read", "Read and display the file.")]
public class ReadCmd : FileBase
{
	[Option("--delay", "-d", "Delay between lines, specified as milliseconds per character in a line.", DefVal = 42)] // Arity = ArgumentArity.Zero)]
	public int Delay { get; set; }

	[Option("--fgcolor", description: "Foreground color of text displayed on the console.", DefVal = ConsoleColor.White, MaxArity = 3)]
	public ConsoleColor FGColor { get; set; }

	[Option("--light-mode", description: "Background color of text displayed on the console: default is black, light mode is white.")]
	public bool LightMode { get; set; }

	public void Handle()
	{
		BackgroundColor = LightMode ? ConsoleColor.White : ConsoleColor.Black;
		ForegroundColor = FGColor;

		foreach(string line in FileIO.ReadLines(File.FullName)) {
			WriteLine(line);
			Thread.Sleep(TimeSpan.FromMilliseconds(Delay * line.Length));
		}
		ResetColor(); // Improvement: Reset console colors to avoid affecting future output
	}
}

[Command("delete", "Delete lines from the file.")]
public class DeleteCmd : FileBase
{
	[Option("--search-terms", description: "Strings to search for when deleting entries.", Required = true, AllowMultipleArgumentsPerToken = true)]
	public string[] SearchTerms { get; set; }

	public async Task HandleAsync() // auto looks for a `Handle` or else a `HandleAsync` method
	{
		if(!FileExists())
			return;

		WriteLine("Deleting from file");
		var lines = FileIO.ReadLines(File.FullName).Where(line => SearchTerms.All(s => !line.Contains(s))).ToArray();
		FileIO.WriteAllLines(File.FullName, lines);
	}
}

[Command("add", "Add an entry to the file.", Alias = "insert")]
public class AddCmd : FileBase
{
	[Argument("quote", "Text of quote.")]
	public string Quote { get; set; }

	[Argument("byline", "Byline of quote.")]
	public string Byline { get; set; }

	public void Handle()
	{
		if(!FileExists())
			return;

		WriteLine("Adding to file");

		using StreamWriter writer = File.AppendText();
		writer.WriteLine($"{Environment.NewLine}{Environment.NewLine}{Quote}");
		writer.WriteLine($"{Environment.NewLine}-{Byline}");
	}
}

public class FileBase
{
	/// <summary>Enter "" for `name` to use static method for getting Option or Argument (see below)</summary>
	[Option<FileInfo>("")]
	public FileInfo File { get; set; }

	/// <summary>
	/// For advanced options needing a direct Option or Argument (needed for some features like DefaultValueFactory),
	/// make a static function named `Get{PropName}`. May prefix with 'Option' or 'Opt" / "Argument" or "Arg".
	/// </summary>
	public static Option<FileInfo> GetFileOption() =>
		new("--file", "-f") {
			Description = "An option whose argument is parsed as a FileInfo",
			Required = true,
			DefaultValueFactory = result => {
				if(result.Tokens.Count == 0)
					return new FileInfo("sampleQuotes.txt");

				string filePath = result.Tokens.Single().Value;
				if(FileIO.Exists(filePath))
					return new FileInfo(filePath);

				result.AddError("File does not exist");
				return null;
			}
		};

	public bool FileExists() => File != null && File.Exists;

	/// <summary>Get access to `ParseResult`: make a property named `ParseResult` or `ParsedResult`</summary>
	public ParseResult ParseResult { get; set; }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0-rc1.7 127 11/24/2025
2.0.0-rc1.6 328 11/21/2025
2.0.0-rc1.5 347 11/19/2025
2.0.0-rc1.4 250 11/17/2025
2.0.0-rc1.3 169 11/16/2025
2.0.0-rc1.2 144 11/14/2025
2.0.0-rc1.1 219 11/13/2025
2.0.0-rc1.0 222 11/12/2025
1.10.0-beta1.1 222 11/11/2025
1.10.0-beta1.0 223 11/11/2025
1.9.0-beta1.1 227 11/11/2025
1.0.6 192 12/17/2024
1.0.5 129 12/8/2024
1.0.4 90 11/29/2024