CommandLinePlus 2.4.0
dotnet add package CommandLinePlus --version 2.4.0
NuGet\Install-Package CommandLinePlus -Version 2.4.0
<PackageReference Include="CommandLinePlus" Version="2.4.0" />
<PackageVersion Include="CommandLinePlus" Version="2.4.0" />
<PackageReference Include="CommandLinePlus" />
paket add CommandLinePlus --version 2.4.0
#r "nuget: CommandLinePlus, 2.4.0"
#:package CommandLinePlus@2.4.0
#addin nuget:?package=CommandLinePlus&version=2.4.0
#tool nuget:?package=CommandLinePlus&version=2.4.0
CommandLinePlus
This project started because I needed a command line parser that could:
- Have a primary option
- Have a sub option
- Accept command line parameters
This allows a developer to construct a command line in the following form:
myprog.exe Option SubOption -param:value
Supports
- .NET 9.0
- .NET Standard 2.0
How it works
Create a class which descends from BaseCommandLine, specify the Name (primary option) and create methods (each method is a sub option).
Add CmdLineDescription and CmdLineAbbreviation attributes to methods, parameters and processor classes for self-describing help information.
In the following example we can use these command lines:
myprog.exe Plugin
myprog.exe Plugin Add -p:myplugin
myprog.exe Plugin Remove --p:myplugin
myprog.exe Plugin Disable /p:myplugin
myprog.exe Plugin Enable -p:myplugin
using CommandLinePlus;
[CmdLineDescription("Processes plugins for entire application")]
internal class PluginProcessor : BaseCommandLine, IDisposable
{
public override string Name => "Plugin";
public override int SortOrder => 0;
public override bool IsEnabled => true;
public override void DisplayHelp()
{
}
public override int Execute(string[] args)
{
return 0;
}
[CmdLineDescription("Adds a new plugin to the application")]
public void Add(
[CmdLineAbbreviation("p", "Name of the plugin to be added")] string pluginName)
{
if (IsEnabled)
Display.WriteLine(VerbosityLevel.Quiet, $"Add plugin {pluginName}");
}
[CmdLineDescription("Removes an existing plugin from the application")]
public void Remove(
[CmdLineAbbreviation("p", "Name of the plugin to be removed")] string pluginName)
{
if (IsEnabled)
Display.WriteLine(VerbosityLevel.Quiet, $"Remove plugin {pluginName}");
}
[CmdLineDescription("Disables a plugin from being used by the application")]
public void Disable(
[CmdLineAbbreviation("p", "Name of the plugin to be disabled")] string pluginName)
{
if (IsEnabled)
Display.WriteLine(VerbosityLevel.Quiet, $"Disable plugin {pluginName}");
}
[CmdLineDescription("Enables a plugin within the application")]
public void Enable(
[CmdLineAbbreviation("p", "Name of the plugin to be enabled")] string pluginName)
{
if (IsEnabled)
Display.WriteLine(VerbosityLevel.Quiet, $"Enable plugin {pluginName}");
}
[CmdLineDescription("Updates a plugins configuration")]
public void Update(
[CmdLineDescription("Name of the plugin to be enabled")] string pluginName)
{
if (IsEnabled)
Display.WriteLine(VerbosityLevel.Quiet, $"Enable plugin {pluginName}");
}
[CmdLineDescription("Updates a plugins configuration")]
public void Update(
[CmdLineAbbreviation("p", "Name of the plugin to be enabled")] string pluginName,
[CmdLineDescription("Boolean option A")] bool optionA,
[CmdLineDescription("Int options B")] int optionB)
{
if (IsEnabled)
Display.WriteLine(VerbosityLevel.Quiet, $"Enable plugin {pluginName}; Option A: {optionA}; Options B: {optionB}");
}
[CmdLineHidden]
public void Dispose()
{
throw new NotImplementedException();
}
}
Wiring it up
Add a reference to the CommandLinePlus package, create your processors, then bootstrap them from the application entry point. The demo (CmdLineTest) uses C# top-level statements:
using CmdLineTest;
IConsoleProcessorFactory factory = new ConsoleProcessorFactory();
object[] processors = new object[]
{
new PluginProcessor(),
};
IConsoleProcessor consoleProcessor = factory.Create("MyProg", processors);
switch (consoleProcessor.Run(out int resultCode))
{
case RunResult.CandidateFound:
Console.WriteLine("finished");
break;
case RunResult.DisplayHelp:
break;
default:
throw new InvalidOperationException("Didn't work");
}
return resultCode;
The demo project makes the CommandLinePlus types available via a global using in its .csproj:
<Using Include="CommandLinePlus" />
If you don't use that, add using CommandLinePlus; to your file instead. The MyProg argument passed to Create is the process name shown in help; replace it with your application's name.
Run returns a RunResult:
| Value | Meaning |
|---|---|
None |
No operation performed (default value) |
DisplayHelp |
Built-in -? help was shown |
NotEnoughCandidates |
No processor matched the primary option |
TooManyCandidates |
More than one processor matched the primary option |
DefaultSubOptionUsed |
Sub-option did not match a method, Execute was called instead |
InvalidParameters |
A method matched but an argument could not be converted |
CandidateFound |
A sub-option method ran successfully |
Built-in options
-?(or/?) shows help for all processors, one processor's sub-options, or a sub-option's parameters.-v:<level>(or/v:<level>) sets the verbosity level:0Quiet,1Normal,2Diagnostic,3Full. The default is1(Normal).
Parameters
Sub-option methods receive values from command-line parameters. Parameter names are matched case-insensitively, sub-option method names are matched case-insensitively, and the primary option name is case-sensitive by default.
bool,int,enumandGuidvalues are converted automatically.- Parameters with a default value are optional.
- A method may return
voidorint; anintresult becomes the process exit code.
Available parameter separators
The following characters can be used as parameter separators
=(equals):(colon)
Available parameter identifiers
The following are used to identify parameter values
-(single dash)--(double dash)/(forward slash)
| Product | Versions 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. net9.0 is compatible. 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. |
| .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. |
-
.NETStandard 2.0
- No dependencies.
-
net9.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.
Supports netstandard2.0 and net9.0