Oaksoft.ArgumentParser
1.3.0
See the version list below for details.
dotnet add package Oaksoft.ArgumentParser --version 1.3.0
NuGet\Install-Package Oaksoft.ArgumentParser -Version 1.3.0
<PackageReference Include="Oaksoft.ArgumentParser" Version="1.3.0" />
paket add Oaksoft.ArgumentParser --version 1.3.0
#r "nuget: Oaksoft.ArgumentParser, 1.3.0"
// Install Oaksoft.ArgumentParser as a Cake Addin #addin nuget:?package=Oaksoft.ArgumentParser&version=1.3.0 // Install Oaksoft.ArgumentParser as a Cake Tool #tool nuget:?package=Oaksoft.ArgumentParser&version=1.3.0
Command Line Arguments Parser Library for .Net
Oaksoft.ArgumentParser is a fluent and simple command line arguments parser library. It is currently under development but latest version is stable. And this documentation is for version v1.3.0.
This library is compatible with .Net 6.0+, .Net Standard 2.1
Quick Start Example
- Create a class to define your options.
- Register your options and build the parser.
- See following example for a quick start.
using Oaksoft.ArgumentParser;
using Oaksoft.ArgumentParser.Extensions;
namespace QuickStart;
internal class CalculatorOptions
{
public double Left { get; set; }
public double Right { get; set; }
public string? Operator { get; set; }
}
internal static class Program
{
private static void Main(string[] args)
{
var parser = CommandLine.CreateParser<CalculatorOptions>()
.AddNamedOption(p => p.Left)
.AddNamedOption(p => p.Right)
.AddNamedOption(o => o.Operator)
.Build();
parser.Run(EvaluateOptions, args);
}
private static void EvaluateOptions(CalculatorOptions options)
{
var result = options.Operator?.ToUpperInvariant() switch
{
"ADD" => $"{options.Left} + {options.Right} = {options.Left + options.Right}",
"SUB" => $"{options.Left} - {options.Right} = {options.Left - options.Right}",
"MUL" => $"{options.Left} * {options.Right} = {options.Left * options.Right}",
"DIV" => $"{options.Left} / {options.Right} = {options.Left / options.Right}",
_ => "Invalid argument!"
};
System.Console.WriteLine($"Result: {result}");
}
}
Sample Command line output for the above console application
Type the options and press enter. Type 'q' to quit.
./> -l 13 -r 8 -o MUL
Result: 13 * 8 = 104
Library Features & Overview
This documentation shows how to create a .NET command-line app that uses the Oaksoft.ArgumentParser library.
Please see Tutorial with Examples for a detailed tutorial.
In this documentation, you learn how to:
- Create named and value options.
- Specify default value for options.
- Specify allowed values for options.
- Create aliases for named options.
- Work with string, string[], int, bool, double, ... option types.
- Use custom code for parsing and validating options.
- Configure option and value counts.
- Configure sequential values.
- Configure option usage and description texts.
- Use built-in help and version options.
1. Option Types
There are two kinds of options. These are Named options and Value options.
1.1. Named Options
If an option has an alias, it is called a named option. Prefix of an alias can be two hyphens (--), one hyphen (-) or forward slash (/). These are some valid commands according to the default alias prefix rules.
./> myapp --open file.txt --read 100 --verbosity quiet
./> myapp /open file.txt /read 100 /verbosity quiet
./> myapp -o file.txt -r 100 -v quiet
First command is parsed by the library into these options: (--open file.txt), (--read 10), (--verbosity quiet).
Please see Parsing Rules for detailed parsing settings.
There are 4 types of named options.
- Scalar Named Option
Scalar named option requires zero or one argument value. Option name may be repeated more than one time. Scalar named option grabs only last value.
Example: --number 123 --number 456 (number: 456) - Sequential Named Option
Sequential named option requires one or more argument values. Option name may be repeated more than one time. A sequential named option grabs all values.
Example: --numbers 123 321 --numbers 456|789 (numbers: {123, 321, 456, 789}) - Switch Option
Switch option is a boolean type. If it is passed in the command-line, it default value will be true.
Example: --start (start: true) - Counter Option
Counter option counts occurrences of the option in the command-line.
Example: --next --next -n -n /n /next (next: 6)
Please see Named Options for detailed named option usages and settings.
1.2. Value Options
- They are options without an alias. They are unbound values. They don't begin with option prefixes.
- If you don't have any named option, you can simply parse command line with value options.
- ArgumentParser tries to parse value options according to the option registration order.
- Also you can register value options and named options to the same parser.
- See following examples.
class ExampleOptions
{
public int Count { get; set; }
public double Total { get; set; }
public List<string> Names { get; set; }
}
See, how the value option registration affects the parser result for the below command-line inputs.
var parser = CommandLine.CreateParser<ExampleOptions>()
.AddValueOption(p => p.Count) // Firstly, bind first integer token
.AddValueOption(p => p.Total) // Secondly, bind first double token from unbinded tokens
.AddValueOption(o => o.Names) // Thirdly, bind all remaining tokens
.Build();
./> frodo 10.5 sam 30 gandalf => count: 30, total: 10.5, names: {frodo, sam, gandalf}
./> frodo 10 sam 30.5 gandalf => count: 10, total: 30.5, names: {frodo, sam, gandalf}
var parser = CommandLine.CreateParser<ExampleOptions>()
.AddValueOption(p => p.Count) // firstly, bind first integer token
.AddValueOption(p => p.Names) // Secondly, bind all remaining tokens
.AddValueOption(o => o.Total) // Thirdly, bind first double token from unbinded tokens
.Build();
./> frodo 10.5 sam 30 gandalf => count: 30, total: 0, names: {frodo, 10.5, sam, gandalf}
./> frodo 10 sam 30.5 gandalf => count: 10, total: 0, names: {frodo, sam, 30.5, gandalf}
2. Default Value
Description will be added!
3. Allowed Option Values
Description will be added!
4. Option Aliases
Description will be added!
5. Custom Option Parser & Validator
Description will be added!
6. Arity Configuration
Description will be added!
7. Other Option Configurations
Description will be added!
8. Built-In Options
Description will be added!
9. Step by Step Tutorial
Description will be added!
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 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | 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.1
- No dependencies.
-
net6.0
- No dependencies.
-
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.