DigitalZenWorks.CommandLine.Commands 1.5.23

dotnet add package DigitalZenWorks.CommandLine.Commands --version 1.5.23
                    
NuGet\Install-Package DigitalZenWorks.CommandLine.Commands -Version 1.5.23
                    
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="DigitalZenWorks.CommandLine.Commands" Version="1.5.23" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DigitalZenWorks.CommandLine.Commands" Version="1.5.23" />
                    
Directory.Packages.props
<PackageReference Include="DigitalZenWorks.CommandLine.Commands" />
                    
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 DigitalZenWorks.CommandLine.Commands --version 1.5.23
                    
#r "nuget: DigitalZenWorks.CommandLine.Commands, 1.5.23"
                    
#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 DigitalZenWorks.CommandLine.Commands@1.5.23
                    
#: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=DigitalZenWorks.CommandLine.Commands&version=1.5.23
                    
Install as a Cake Addin
#tool nuget:?package=DigitalZenWorks.CommandLine.Commands&version=1.5.23
                    
Install as a Cake Tool

DigitalZenWorks.CommandLine.Commands

NuGet version NuGet downloads Build and tests License: MIT GitHub issues

A lightweight C# library for named commands, short and long options, option values, positional arguments, JSON command definitions, and help output. The application retains control of command execution.

Installation

Add the package to your application:

dotnet add package DigitalZenWorks.CommandLine.Commands

Or use Visual Studio's Package Manager Console:

Install-Package DigitalZenWorks.CommandLine.Commands

Quick start

In a console application, define the accepted commands and options, then validate the supplied arguments:

using System;
using System.Collections.Generic;
using DigitalZenWorks.CommandLine.Commands;

Command help = new("help");
help.Description = "Show available commands";

CommandOption encoding = new("e", "encoding", true);
CommandOption dryRun = new("n", "dryrun");
List<CommandOption> options = new();
options.Add(encoding);
options.Add(dryRun);

Command convert = new("convert", options, 1, "Convert a data file");
List<Command> commands = new();
commands.Add(help);
commands.Add(convert);

CommandLineInstance commandLine = new(commands, args);

if (commandLine.ValidArguments == false)
{
	Console.Error.WriteLine(commandLine.ErrorMessage);
	commandLine.ShowHelp();
	return;
}

Command command = commandLine.Command;

if (command.Name == "help")
{
	commandLine.ShowHelp();
	return;
}

string inputPath = command.Parameters[0];
CommandOption encodingOption = command.GetOption("e", "encoding");
string encodingName = "utf-8";

if (encodingOption != null)
{
	encodingName = encodingOption.Parameter;
}

bool dryRunRequested = command.DoesOptionExist("n", "dryrun");
Console.WriteLine("Input: " + inputPath);
Console.WriteLine("Encoding: " + encodingName);
Console.WriteLine("Dry run: " + dryRunRequested);

Example invocations:

dotnet run -- convert --encoding utf-8 --dryrun input.txt
dotnet run -- convert -e utf-8 -n input.txt
dotnet run -- help

The 1 in the Command constructor is the minimum number of positional arguments. Setting requiresParameter to true on a CommandOption makes it consume the following argument as its value. Supply values separately, as in --encoding utf-8.

JSON command definitions

Commands can also be defined in a JSON file, such as commands.json:

[
	{
		"command": "help",
		"description": "Show available commands"
	},
	{
		"command": "convert",
		"description": "Convert a data file",
		"parameterCount": 1,
		"options":
		[
			{
				"shortName": "e",
				"longName": "encoding",
				"requiresParameter": true
			},
			{
				"shortName": "n",
				"longName": "dryrun",
				"requiresParameter": false
			}
		]
	}
]

Load the definitions instead of constructing the command list in code:

using System.IO;
using DigitalZenWorks.CommandLine.Commands;

string jsonText = File.ReadAllText("commands.json");
CommandsSet commandSet = new(jsonText);
CommandLineInstance commandLine = new(commandSet, args);

Check ValidArguments before accessing the selected command, its options, or its positional arguments. See the XML documentation in the library source for individual API contracts.

Target frameworks

The current Release configuration targets .NET Standard 2.0, .NET Framework 4.7.2, and .NET 5, 6, 7, 8, 9, and 10. Debug builds target .NET 10 only. See the package page for a published version's frameworks and its dependencies.

Build and test

Install the .NET 10 SDK, then clone and build the repository:

git clone https://github.com/jamesjohnmcguire/CommandLineCommands.git
cd CommandLineCommands
dotnet build CommandLineCommands.slnx --configuration Debug
dotnet test CommandLineCommands.slnx --configuration Debug

See the development notes for Release builds, versioning, trusted publishing, and the build.cmd helper.

Contributing

Report bugs and propose improvements through GitHub issues.

For a pull request:

  1. Fork the Project
  2. Create your focused branch
  3. Add your code changes
  4. Add or update tests to validate your changes
  5. Commit and Push your Changes
  6. Open a Pull Request. Include the description of the change, its validation, and any related issue in the PR.

Coding style

Please match the current coding style. Most notably:

  1. One operation per line
  2. Use complete, self-documenting English words in variable and method names
  3. Attempt to declare variable and method names in a self-documenting manner
  4. Use tabs and Allman braces

Keep public API compatibility in mind: this library is consumed as NuGet package.

License

Distributed under the MIT License.

Contact

James John McGuire — jamesjohnmcguire@gmail.com

Project repository. If the library is useful to you, consider giving it a star.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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 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 is compatible.  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 is compatible.  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. 
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
1.5.23 39 11/5/2026
1.5.22 265 6/29/2026
1.5.18 388 10/28/2025
1.5.16 277 10/27/2025
1.5.14 276 10/22/2025
1.4.12 351 3/22/2025
1.4.11 364 3/10/2025
1.3.10 271 1/12/2025
1.2.8 301 8/7/2024
1.1.6 314 4/9/2024
1.1.5 398 10/20/2023
1.1.4 261 9/10/2023
1.0.2 402 10/31/2022