ConsoleApp.CommandLine
1.1.2
See the version list below for details.
dotnet add package ConsoleApp.CommandLine --version 1.1.2
NuGet\Install-Package ConsoleApp.CommandLine -Version 1.1.2
<PackageReference Include="ConsoleApp.CommandLine" Version="1.1.2" />
paket add ConsoleApp.CommandLine --version 1.1.2
#r "nuget: ConsoleApp.CommandLine, 1.1.2"
// Install ConsoleApp.CommandLine as a Cake Addin #addin nuget:?package=ConsoleApp.CommandLine&version=1.1.2 // Install ConsoleApp.CommandLine as a Cake Tool #tool nuget:?package=ConsoleApp.CommandLine&version=1.1.2
Quick Start
Basics
To start, you need to configure the entry point to the application. Where "ConsoleApp" will be your class with a command handler.
using System
class Program
{
public static int Main()
{
return CommandLine.Run<Program>(CommandLine.Arguments, defaultCommandName: "SayHello")
}
public static int SayHello()
{
Console.WriteLine("Hello!");
return 0;
}
}
CommandLine.Run relies on reflection to find methods. So they should be static and return int which is Exit Code
Now you can test your application:
myapp.exe SayHello
#>Hello!
myapp.exe SAYHELLO
#>Hello! -- command name is case-insensitive (parameters are not!)
myapp.exe
#>Hello! - Too because 'defaultCommandName' is set to 'SayHello'
Parameter bindings
Positional and named parameters
You can add parameters to your command which is automatically binds by name or position
public static int SayHello(string name)
{
Console.WriteLine("Hello " + name + "!");
return 0;
}
Testing:
myapp.exe SayHello Mike
#>Hello Mike!
myapp.exe SayHello --name Jake
#>Hello Jake!
List parameters
You can add array parameter to collect multiple values as shown below:
public static int SayHello(string[] names)
{
Console.WriteLine("Hello " + string.Join(", ", names) + "!");
return 0;
}
Testing:
myapp.exe SayHello --names Mike Jake
#>Hello Mike, Jake!
Parameter accepting multiple values can be only named.
Optional parameters
You can make any parameter optional by specifying default value.
public static int ShowOptionalParameter(int myOptionalParam = 100)
{
Console.WriteLine("My optional parameter is " + myOptionalParam);
return 0;
}
Testing:
myapp.exe ShowOptionalParameter --myOptionalParam 200
#>My optional parameter is 200
myapp.exe ShowOptionalParameter 300
#>My optional parameter is 300
myapp.exe ShowOptionalParameter
#>My optional parameter is 100
Flag parameters
You can have a flag(true/false) parameter. It's presence is considered to be "True" and absence is "False".
public static int ShowFlag(bool myFlag)
{
Console.WriteLine(myFlag ? "Flag is set" : "Flag is not set");
return 0;
}
Testing:
myapp.exe ShowFlag --myFlag
#>Flag is set
myapp.exe ShowFlag
#>Flag is not set
Parameters values starting with hyphen(-) symbol
Negative numbers (such as -1000) are interpreted as values by default. But strings like "-a", "-hello" are interpreted as named parameters. To stop this behavior you could start parameters with bare hyphen parameter.
myapp.exe - --message -hello --class -a
Enforcing positional parameters
You could pass bare double hyphen(--) and anything after it will be threated as positional parameters.
myapp.exe --type message -- value1 value2 value3
No special symbols are interpeted after double hyphen(--) even another double hypthen.
myapp.exe --type message -- -value1 --value2 --value3-- --
Supported Parameter Types
- Primitive types (int, byte, char ...)
- BCL types (String, DateTime, Decimal)
- Nullable types
- Enum types
- Types with TypeConverterAttribute (Point, Guid, Version, TimeSpan ...)
- Types with Parse(string value) method (IpAddress, Guid ...)
- Types with explicit/implicit conversion from string
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net35 is compatible. net40 was computed. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
- TypeConvert (>= 1.0.0.10)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on ConsoleApp.CommandLine:
Package | Downloads |
---|---|
CodeContractsRemover
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
# 1.1.2
-added bare double hyphen to enforce positional parameters
-added bare single hyphen to disable hyphen interpretation in values
-added special treatment for negative numbers
- added CommandLine.DescribeOnBindFailure which controls reaction on method binding failure (true to run CommandLine.Describe(), false to throw exception).
- added enum flags binding subroutine, now "--flag Flag1 Flag2 Flag3" arguments are supported.
- changed method binding order to from most parameters to less (original was chaotic), binding strategy is still - "first match".
- added non-generic Run and Describe methods
- fixed bug with positional parameters binding
# 1.0.0
- initial release