Ookii.CommandLine
3.1.0
See the version list below for details.
dotnet add package Ookii.CommandLine --version 3.1.0
NuGet\Install-Package Ookii.CommandLine -Version 3.1.0
<PackageReference Include="Ookii.CommandLine" Version="3.1.0" />
paket add Ookii.CommandLine --version 3.1.0
#r "nuget: Ookii.CommandLine, 3.1.0"
// Install Ookii.CommandLine as a Cake Addin #addin nuget:?package=Ookii.CommandLine&version=3.1.0 // Install Ookii.CommandLine as a Cake Tool #tool nuget:?package=Ookii.CommandLine&version=3.1.0
Ookii.CommandLine
Ookii.CommandLine is a powerful and flexible command line argument parsing library for .Net applications.
- Easily define arguments by creating a class with properties.
- Create applications with multiple subcommands.
- Generate fully customizable usage help.
- Supports PowerShell-like and POSIX-like parsing rules.
Ookii.CommandLine is provided in versions for .Net Standard 2.0, .Net Standard 2.1, and .Net 6.0 and later
Overview
Ookii.CommandLine is a library that lets you parse the command line arguments for your application into a set of strongly-typed, named values. You can easily define the accepted arguments, and then parse the command line supplied to your application for those arguments. In addition, you can generate usage help that can be displayed to the user.
Ookii.CommandLine can be used with any kind of .Net application, whether console or GUI. Some functions, such as creating usage help, are primarily designed for console applications, but even those can be easily adapted for use with other styles of applications.
Two styles of command line parsing rules are supported: the default mode uses rules similar to those used by PowerShell, and the alternative long/short mode uses a style influenced by POSIX conventions, where arguments have separate long and short names with different prefixes. Many aspects of the parsing rules are configurable.
To determine which arguments are accepted, you create a class, with constructor parameters and properties that define the arguments. Attributes are used to specify names, create required or positional arguments, and to specify descriptions for use in the generated usage help.
For example, the following class defines four arguments: a required positional argument, an optional positional argument, a named-only argument, and a switch argument (sometimes also called a flag):
class MyArguments
{
[CommandLineArgument(Position = 0, IsRequired = true)]
[Description("A required positional argument.")]
public string? Required { get; set; }
[CommandLineArgument(Position = 1)]
[Description("An optional positional argument.")]
public int Optional { get; set; }
[CommandLineArgument]
[Description("An argument that can only supplied by name.")]
public DateTime Named { get; set; }
[CommandLineArgument]
[Description("A switch argument, which doesn't require a value.")]
public bool Switch { get; set; }
}
Each argument has a different type that determines the kinds of values it can accept.
To parse these arguments, all you have to do is add the following line to your Main
method:
var arguments = CommandLineParser.Parse<MyArguments>();
This code will take the arguments from Environment.GetCommandLineArgs()
(you can also manually
pass a string[]
array if you want), will handle and print errors to the console, and will print
usage help if needed. It returns an instance of MyArguments
if successful, and null
if not.
If the arguments are invalid, or help is requested, this application will print the following usage help:
Usage: MyApplication [-Required] <String> [[-Optional] <Int32>] [-Help] [-Named <DateTime>]
[-Switch] [-Version]
-Required <String>
A required positional argument.
-Optional <Int32>
An optional positional argument.
-Help [<Boolean>] (-?, -h)
Displays this help message.
-Named <DateTime>
An argument that can only supplied by name.
-Switch [<Boolean>]
A switch argument, which doesn't require a value.
-Version [<Boolean>]
Displays version information.
The usage help includes the descriptions given for the arguments.
The application also has two arguments that weren't in the class, -Help
and -Version
, which are
automatically added by default.
An example invocation of this application, specifying all the arguments, would look like this (argument names are case insensitive by default):
./MyApplication foo 42 -switch -named 2022-08-14
In addition, Ookii.CommandLine can be used to create applications that have multiple subcommands, each with their own arguments.
For more information, including a tutorial and samples, see the full documentation on GitHub.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. 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. |
.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 is compatible. |
.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.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (2)
Showing the top 2 popular GitHub repositories that depend on Ookii.CommandLine:
Repository | Stars |
---|---|
Ourpalm/ILRuntime
Pure C# IL Intepreter Runtime, which is fast and reliable for scripting requirement on enviorments, where jitting isn't possible.
|
|
servicetitan/Stl.Fusion.Samples
A collection of samples for Fusion library: https://github.com/servicetitan/Stl.Fusion
|
Version | Downloads | Last updated |
---|---|---|
4.2.0 | 776 | 9/12/2024 |
4.2.0-preview2 | 95 | 8/29/2024 |
4.2.0-preview | 95 | 8/28/2024 |
4.1.0 | 7,126 | 1/26/2024 |
4.0.1 | 1,539 | 9/19/2023 |
4.0.0 | 704 | 7/20/2023 |
3.1.1 | 5,242 | 3/29/2023 |
3.1.0 | 367 | 3/21/2023 |
3.0.0 | 6,729 | 12/1/2022 |
2.4.0 | 2,714 | 9/1/2022 |
2.3.0 | 32,318 | 9/5/2019 |
2.2.0 | 103,434 | 2/6/2013 |
2.1.0 | 2,687 | 2/20/2012 |
This version contains breaking changes compared to version 2.x. For details, please view: https://www.ookii.org/Link/CommandLineVersionHistory