CommandLineParser.DependencyInjection 1.1.1

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

CommandLineParser.DependencyInjection

This is a simple wrapper around the amazing commandlineparser/commandline library which adds support for Microsoft's DI with help from Scrutor. This is done by allow the following interfaces to be implemented which DI will find and user for executing the commandlineparser.

Interface Description
ICommandLineOptions This is an empty interface that is used solely for DI to find options! Make a class with Options just like the CommandLineParser library instructs!
ICommandLineParser< TResult > This is the DI Service you will use to access the Parser and execute the relevant DI Service for IExecuteCommandLineOptions/IExecuteCommandLineOptionsAsync. It has the ability to run both synchronously or asynchronously.
IExecuteCommandLineOptions<TCommandLineOptions, TResult> Interface that implements Execute(options) synchronously with the Parser's Command Line Options.
IExecuteCommandLineOptionsAsync<TCommandLineOptions, TResult> Interface that implements ExecuteAsync(options) asynchronously with the Parser's Command Line Options.
IExecuteParsingFailure<TResult> If no Options/Handler found, this is called synchronously with the executed arguments array and parser errors to handle the inability to handle the command line input arguments.
IExecuteParsingFailureAsync< TResult > If no Options/Handler found, this is called asynchronously with the executed arguments array and parser errors to handle the inability to handle the command line input arguments.

Example

Here is an example of the QuickStart from commandlineparser/commandline implemented using DI. In this case though, we're writing to Microsoft's Logging ILogger that is getting injected using DI instead of writing directly to console!

using CommandLine;
using CommandLineParser.DependencyInjection.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
 
new ServiceCollection() // Create Service Collection
        .AddCommandLineParser(typeof(Options).Assembly) // Add CommandLineParser registrations to DI
        .AddLogging(c => c.AddConsole()) // Add Console Logging
    .BuildServiceProvider() // Build Service Provider
        .GetRequiredService<ICommandLineParser<int>>() // Get Parser Service
            .ParseArguments(args, -1) // Call Parser with Arguments (Will have Options as well as and ExecuteOptions from DI)
    ;
 
public class Options: ICommandLineOptions
{
    [Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
    public bool Verbose { get; set; }
}
 
public class ExecuteOptions(ILogger<ExecuteOptions> log) : IExecuteCommandLineOptions<Options, int>
{
    #region Implementation of IExecuteCommandLineOptions<in Options,out int>
 
    /// <summary>
    /// Execute Command Synchronously.
    /// </summary>
    /// <param name="options">Command Line Options</param>
    /// <returns>Result</returns>
    public int Execute(Options options)
    {
        if (options.Verbose)
        {
            log.LogInformation($"Verbose output enabled. Current Arguments: -v {options.Verbose}");
            log.LogWarning("Quick Start Example! App is in Verbose mode!");
        }
        else
        {
            log.LogInformation($"Current Arguments: -v {options.Verbose}");
            log.LogInformation("Quick Start Example!");
        }
 
        return 0;
    }
 
    #endregion
}

Unit Tests

See the Test's project for example.

The Tests project has 3 folders:

  • Options: These are the CommandLineOptions, exactly how the commandlineparser/commandline library defines them, with the ICommandLineOptions interface added for DI discovering. The only difference is that one is used for calling async instead of sync in tests.
  • ExecuteAskOptions: These are both Sync and Async dummy implementations of services that handle the AskOptions as well as handle Parsing failures.
  • Services: Dummy service that returns a string based on the arguments provided.
Product 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 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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.1.1 724 7/29/2024

Added support for Async parsing/execution.