Simplified.System.CommandLine-beta 1.0.1

dotnet add package Simplified.System.CommandLine-beta --version 1.0.1                
NuGet\Install-Package Simplified.System.CommandLine-beta -Version 1.0.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="Simplified.System.CommandLine-beta" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Simplified.System.CommandLine-beta --version 1.0.1                
#r "nuget: Simplified.System.CommandLine-beta, 1.0.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.
// Install Simplified.System.CommandLine-beta as a Cake Addin
#addin nuget:?package=Simplified.System.CommandLine-beta&version=1.0.1

// Install Simplified.System.CommandLine-beta as a Cake Tool
#tool nuget:?package=Simplified.System.CommandLine-beta&version=1.0.1                

Simplified.System.CommandLine

A simplifying wrapper for the Microsoft System.CommandLine package.

What does the System.CommandLine package do?

Microsoft currently has a new Command line Interface for Console Apps under development.

As of November 2022, this is still in beta 2.0.0-beta4.22272.1 Command Line Api.

The project Github says:

The System.CommandLine library provides functionality that is commonly needed by command-line apps, such as parsing the command-line input and displaying help text.

This implementation is perfect for compatibility with the CLI and provides outstanding support for this.

This is an exceedingly powerful implementation: consequently it is also extremely complex and a little challenging for everyday users.

For example, to read just one parameter from the command line you can use this code:

See full Example

    var messageArgument = new Argument<string>
        ("message", "An argument that is parsed as a string.");

    var rootCommand = new RootCommand();
    rootCommand.Add(messageArgument);

    rootCommand.SetHandler((delayArgumentValue, messageArgumentValue) =>
        {
            Console.WriteLine($"<message> argument = {messageArgumentValue}");
        },
        delayArgument, messageArgument);

    await rootCommand.InvokeAsync(args);

For me, the workflow here is a little hard to follow. And, it is not that clear exactly where you can access the returned values. It appears that the handler is the only place that you can do this at this time.

You can certainly validate the results in the Handler, but there is support for specific Validators that can be used like this:

See full description here

  var delayOption = new Option<int>("--delay");
  delayOption.AddValidator(result =>
  {
      if (result.GetValueForOption(delayOption) < 1)
      {
          result.ErrorMessage = "Must be greater than 0";
      }
  });

There is also a more powerful ParseArgument to give greater control of the result.

What does this Package do?

The purpose of this package, as the name suggests, is to provide a simpler approach for the common use case: Read in parameters, validate them, and make it available to use

The power of the underlying System.CommandLine is still available for more advanced handling.

Getting Started.

TODO: <nuget package>

Single Argument Example

Read an IP address from the command line (validating that is in the correct format)

The simplest form of this is:

    var ipAddress = new ParameterInfo<string>("IP Address", "The IP of the Computer to connect to.")
        {
            ValidationMessage = $"Must be a valid IP4 address",
            ValidationExpression = @"((\d){1,3}\.){3}\d{1,3}\b"
        };

    SimplifiedCommandLineHandler.ExtractParameter(args, ipAddress);

    if (ipAddress.IsErrorOrEmpty)
        Console.Error.WriteLine($"Cant connect to an invalid IP Address");
    else
        Console.WriteLine($"Connecting to host '{ipAddress.Value}'");

If there is a validation failure, you get a message like this:

image

You see that Help messages are also available.

A slight variation using more a fluent syntax with the FirstParameter method is also available - it is more consistent with the approach used Model Binding

using Simplified.System.Commandline;

var ipAddress = SimplifiedCommandLineHandler
     .FirstParameter(args,
         new ParameterInfo<string>("IP Address", "The IP of the Computer to connect to.")
         {
             ValidationMessage = $"Must be a valid IP4 address",
             ValidationExpression = @"((\d){1,3}\.){3}\d{1,3}\b"
         });

if (ipAddress.IsErrorOrEmpty)
    Console.Error.WriteLine($"Cant connect to an invalid IP Address");
else
    Console.WriteLine($"Connecting to host '{ipAddress.Value}'");

In this example, as we have no action methods, ie it doesnt try to act on the results, there is no need to run asynchronously.

Multi-Argument Example

Adding Options

Not yet implemented... use CommandLine Options for now.

Modifying Help

Not yet implemented... use CommandLine Options for now.

Alternate Validators

The static RegExArgumentValidator<T> class supports Regex and the No Validator case.

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. 
.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 was computed.  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.0.1 559 11/5/2022
1.0.0 152 11/5/2022

Limited Implementation supporting only Command line Arguments (no options)