DQD.CommandLineParser 1.4.1

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

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.

Argument Values

There are several ways to restrict the domain of an argument. One way is to use an enum data type:

public enum ActionType
{
    [HiddenValue] Unspecified,

    Mix,
    Knead,
    Bake,
    Slice,
}

public class CommandLineArguments
{
    [Argument("/ACTION")]
    public ActionType Action;
}

You can also specify strings to be recognized on the command-line that differ from the enum member names:

public enum ActionType
{
    [HiddenValue] Unspecified,

    [TextValue("mix-bread")] Mix,
    [TextValue("knead-bread")] Knead,
    [TextValue("bake-bread")] Bake,
    [TextValue("slice-bread")] Slice,
}

The argument will automatically be supplied with a Description such as:

One of Mix, Knead, Bake, Slice

You can override Description. If you want to include a generated list of values in your custom description, you can use the insertion tag "@values":

public class CommandLineArguments
{
    [Argument("/ACTION", Description = "Tell me what to do! Any of these: @values")]
    public ActionType Action;
}

You can also specify allowable values explicitly by name:

public class CommandLineArguments
{
    [Argument("/ACTION", Values = ["Run", "Jump", "Kick"])]
    public string Action = "";
}

Finally, if you need to gather values in a custom way, you can create a class implementing IValuesSource or IValueMappingsSource and take full control:

public class FruitsValuesSource : IValuesSource
{
    public IEnumerable<string> EnumerateValues()
    {
        yield return "Apple";
        yield return "Banana";
        yield return "Cantaloupe";
    }
}

public class DistanceSource : IValuesSource
{
    public IEnumerable<ArgumentValueMapping> EnumerateValueMappings()
    {
        yield return ("centimeter", 10.0M);
        yield return ("inch", 25.4M);
        yield return ("decimeter", 100.0M);
        yield return ("foot", 304.8M);
    }
}

public class CommandLineArguments
{
    [Argument("/FRUIT", ValuesSourceType = typeof(FruitsValuesSource))]
    public string? Fruit;

    [Argument("/DISTANCE", ValuesSourceType = typeof(DistanceValuesSource))]
    public decimal DistanceMillimetres;
}

Autocompletion

Most shells have autocompletion, where if you type part of a filename or keyword and press Tab, the rest is filled in for you. Some shells allow you to hook into and customize this functionality.

DQD.CommandLineParser supports doing this for you automatically in PowerShell and Bash.

To use this in your own program, you must define two arguments in your args type:

		[Completer("--complete")]
		public string? Complete;
		[RegisterCompleter("--registercompleter", CommandName = "DQD.CommandLineParser.Sample")]
		public ShellType RegisterForShell;

The members can be fields or properties, and they can be named whatever you want. The exact switches can be whatever you want as well. They simply have to exist.

With these defined, running your application and using the switch supplied to RegisterCompleter will emit the shell code that needs to be sourced to complete registration.

The completion assumes that you will be using the stub executable that provides a direct binary for executing your application. This is a file called e.g. MyProject.exe, or simply MyProject on UNIX systems, in the build output directory.

The only constraint is that your application must not do anything significant prior to its call to CommandLine.Parse<T>. This is because invocations of the application are used to obtain completion options as the command-line is being built up.

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 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. 
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.4.2 118 11/6/2026
1.4.1 121 11/6/2026
1.4.0 97 11/6/2026
1.3.0 107 11/3/2026
1.2.0 403 7/3/2024
1.1.0 225 6/30/2024