DQD.CommandLineParser
1.4.2
dotnet add package DQD.CommandLineParser --version 1.4.2
NuGet\Install-Package DQD.CommandLineParser -Version 1.4.2
<PackageReference Include="DQD.CommandLineParser" Version="1.4.2" />
<PackageVersion Include="DQD.CommandLineParser" Version="1.4.2" />
<PackageReference Include="DQD.CommandLineParser" />
paket add DQD.CommandLineParser --version 1.4.2
#r "nuget: DQD.CommandLineParser, 1.4.2"
#:package DQD.CommandLineParser@1.4.2
#addin nuget:?package=DQD.CommandLineParser&version=1.4.2
#tool nuget:?package=DQD.CommandLineParser&version=1.4.2
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
boolvalue: Is it there, or is it not? Switches can also beintvalues, 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
stringvalue. 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
IsRequiredproperty 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
Parsemethod can be supplied anIEnumerable<string>. - The
CommandLineobject can persist anIEnumerable<string>to use in place of the default argument list. This is done by calling theSetArgumentDatamethod.
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 | Versions 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. |
-
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.