DQD.CommandLineParser
1.1.0
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
<PackageReference Include="DQD.CommandLineParser" Version="1.1.0" />
paket add DQD.CommandLineParser --version 1.1.0
#r "nuget: DQD.CommandLineParser, 1.1.0"
// 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 beint
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 anIEnumerable<string>
. - The
CommandLine
object can persist anIEnumerable<string>
to use in place of the default argument list. This is done by calling theSetArgumentData
method.
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. |
-
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.