Ubiquity.NET.CommandLine 20.1.9-beta

This is a prerelease version of Ubiquity.NET.CommandLine.

Requires NuGet 4.9.0 or higher.

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

Ubiquity.NET.CommandLine

Common Text CommandLine support. This provides a number of support classes for Text based UI/UX, including command line parsing extensions. This is generally only relevant for console based apps.

Example Command line parsing

Normal single command application: Options binding types are normally split into two partial classes. The normal types that would contain attributes for source generation and the generated partial implementation. While there is no source generator (yet) for the options [That is intended for a future release] this keeps the responsibilities clearer and aids in migration to a source generator.

Options type

Options.cs:

using System.IO;

using Ubiquity.NET.CommandLine;

namespace TestSample
{
    internal partial class Options
    {
        // Option; DefaultValue = MsgLevel.Information
        internal MsgLevel Verbosity { get; init; }

        // Additional option here...
    }
}

Options.g.cs

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Ubiquity.NET.CommandLine;

namespace TestSample
{
    internal partial class Options
        : ICommandLineOptions<Options>
    {
        private Options()
        {
        }

        public static Options Bind(ParseResult parseResults)
        {
            return new()
            {
                Verbosity = parseResults.GetValue(Descriptors.Verbosity),
            };
        }

        public static AppControlledDefaultsRootCommand BuildRootCommand(CmdLineSettings settings)
        {
            return new AppControlledDefaultsRootCommand( settings, "TestSample App" )
            {
                Descriptors.Verbosity,
            };
        }
    }

    file static class Descriptors
    {
        internal static readonly Option<MsgLevel> Verbosity
            = new Option<MsgLevel>("-v")
            {
                Aliases = {"--verbosity"},
                Description = "Verbosity level",
                DefaultValueFactory = ar => MsgLevel.Information,
            };
    }
}

Common single command/no command case

Most applications don't have or use any concept of commands/sub-commands. Thus, there is support to make this scenario as simple as possible.

public static int Main( string[] args )
{
    // start with information level for parsing; parsed options might specify different level
    var reporter = new ColoredConsoleReporter( MsgLevel.Information );
    var settings = new CmdLineSettings()
    {
        ShowHelpOnErrors = false, // Errors in parsing should NOT show help, only show it if asked to do so.
    };

    return Options.BuildRootCommand( settings, options => AppMain( options, reporter ) )
                  .ParseAndInvokeResult( reporter, settings, args );
}

private static int AppMain( Options options, ColoredConsoleReporter reporter )
{
    // Real main here.
    // The pre-parsed and validated options and the reporter are provided as arguments
    // to this function
}

This is simplified usage for the common case of an app without commands/sub-commands.

Advanced app control of dispatching and validation

Advanced, parsing for customized behavior

public static int Main( string[] args )
{
    var reporter = new ColoredConsoleReporter(MsgLevel.Information);
    var settings = new CmdLineSettings()
    {
        ShowHelpOnErrors = false, // Errors in parsing won't show help, only show it if asked to do so.
    };

    if(!ArgsParsing.TryParse<Options>( args, settings, reporter, out Options? options, out int exitCode ))
    {
        return exitCode;
    }

    // ... Deal with options as parsed.
}

The ONLY automatic invocation applied here is the default Help and version options However, even those can be disabled using a CmdLineSettings instance. Options is a custom class that has properties for all parsed commands, arguments and options allowing for validation of them all in context (including each other). The App can then dispatch behavior based on the commands/options etc... as needed. NO ASSUMPTION IS MADE ABOUT THE USE OF COMMANDS NOR THE BEHAVIOR OF THEM. The app is entirely in control of how they are used. While the underlying System.Commandline does have validation built-in, it is only possible to validate an item individually. Mutual exclusion or other complex validation is not supported. Thus this more advanced use case allows such validations before dispatching to a handler.

Supported Functionality

IDiagnosticReporter interface is at the core of the UX. It is similar in many ways to many of the logging interfaces available. The primary distinction is with the intention of use. IDiagnosticReporter specifically assumes the use for UI/UX rather than a debugging/diagnostic log. These have VERY distinct use cases and purposes and generally show very different information. (Not to mention the overly complex requirements of the anti-pattern DI container assumed in Microsoft.Extensions.Logging)

Messages

All messages for the UX use a simple immutable structure to store the details of a message represented as DiagnosticMessage.

Pre-Built Reporters

There are a few pre-built implementation of the IDiagnosticReporter interface.

  • TextWriterReporter
    • Base class for writing UX to a TextWriter
  • ConsoleReporter
    • Reporter that reports errors to Console.Error and all other messages to Console.Out
  • ColoredConsoleReporter
    • ConsoleReporter that colorizes output using ANSI color codes
      • Colors are customizable, but contains a common default
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Ubiquity.NET.CommandLine:

Package Downloads
Ubiquity.NET.Runtime.Utils

General use Support for interpreters/runtimes

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
20.1.9-beta 193 12/5/2025
20.1.8 497 11/17/2025
20.1.8-rc.3 238 11/16/2025
20.1.8-rc.2 178 11/14/2025
20.1.8-rc.1 237 11/13/2025
20.1.8-rc 184 11/2/2025
20.1.8-epsilon 77 10/11/2025
20.1.8-delta 167 9/7/2025
20.1.8-beta 161 9/4/2025