DQD.CommandLineParser 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DQD.CommandLineParser --version 1.1.0                
NuGet\Install-Package DQD.CommandLineParser -Version 1.1.0                
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="DQD.CommandLineParser" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DQD.CommandLineParser --version 1.1.0                
#r "nuget: DQD.CommandLineParser, 1.1.0"                
#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 DQD.CommandLineParser as a Cake Addin
#addin nuget:?package=DQD.CommandLineParser&version=1.1.0

// Install DQD.CommandLineParser as a Cake Tool
#tool nuget:?package=DQD.CommandLineParser&version=1.1.0                

DQD.CommandLineParser

Parses command lines. 😃

How To Use

Step 1: Instantiate CommandLine.

Step 2: Ask it for your arguments type:

var args = new CommandLine().Parse<ArgsType>();

It finds the command-line of the current process automatically.

How To Make Arguments Type

In the world of DQD.CommandLineParser, there are two kinds of thing that can be found on the command-line:

  • Switches:

    Switches do not have values. The simplest switch is a bool value: Is it there, or is it not? Switches can also be int values, in which case the number of occurrences is counted.

    public class Arguments
    {
      [Switch] public bool Help;
    
      [Switch("/Horse")] public bool IncludeEquine;
    
      [Switch("/Debug")] public int DebugLevel;
    }
    
  • Arguments:

    Arguments collect values from the command-line. The simplest argument has a string value. DQD.CommandLineParser will try to coerce to other types as needed.

    public class Arguments
    {
      [Argument] public string FileToProcess;
      [Argument("/OUT")] public string OutputFilePath;
      [Argument] public int TimeoutSeconds; // E.g.: /TimeoutSeconds 10
    }
    

    Arguments can also populate members of a structure.

    public class Point3D
    {
      public double X, Y, Z;
    }
    
    public class Arguments
    {
      // E.g.: /CameraPosition 0 -100 6.5
      [Argument(Properties = [ "X", "Y", "Z" ])] public Point3D CameraPosition;
    }
    

    Arguments can also be "floating", which means they do not have an associated switch. For instance, if you want the output filename to be specified by any argument that isn't introduced with a switch:

    // E.g.: /InputFile A.txt /InputFile B.txt /GronkulationLevel 42 OutputFile.txt
    public class Arguments
    {
      [Argument] public int GronkulationLevel;
      [Argument] public List<string> InputFile;
      [Argument(IsFloating = true)] public string OutputFile;
    }
    

    Arguments also have an IsRequired property to do with as you see fit. Exceptions may be involved.

Other Sources Of Arguments

By default, arguments come from the current process's argv. You can override this, though, and parse arbitrary sequences.

  • The Parse method can be supplied an IEnumerable<string>.
  • The CommandLine object can persist an IEnumerable<string> to use in place of the default argument list. This is done by calling the SetArgumentData method.
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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.2.0 115 7/3/2024
1.1.0 109 6/30/2024