GetOpts 0.3.0
dotnet add package GetOpts --version 0.3.0
NuGet\Install-Package GetOpts -Version 0.3.0
<PackageReference Include="GetOpts" Version="0.3.0" />
<PackageVersion Include="GetOpts" Version="0.3.0" />
<PackageReference Include="GetOpts" />
paket add GetOpts --version 0.3.0
#r "nuget: GetOpts, 0.3.0"
#:package GetOpts@0.3.0
#addin nuget:?package=GetOpts&version=0.3.0
#tool nuget:?package=GetOpts&version=0.3.0
The main namespace of this library is DD.GetOpts.
First we need to define the required options:
var option1 = new Option( "a", "alpha", Argument.NONE, Occur.ONCE );
var option2 = new Option( "b", "beta", Argument.REQUIRED, Occur.OPTIONAL );
var option3 = new Option( "g", "gamma", Argument.OPTIONAL, Occur.MULTIPLE );
Every option consists a short and long name, as well as various occurrence and argument rules. A short name has to prefixed with a - in the supplied arguments while a long name has to be prefixed with a --. In the above example option1 can be parsed either via -a or --alpha. The argument rules of the above options are as follows:
option1doesn't have an argument (Only-aor--alphaare valid).option2requires and argument (Only-b argumentor--beta argumentare valid).option3can either have an argument or omit it.
The occurance rules of the above options are as follows:
option1must occur exactly once in the provided arguments.option2can occur once in the provided arguments but doesn't have to.option3can occur multiple times in the provided arguments but doesn't have to.
The next step is to add these options to the Options collection.
var options = new Options();
options.Add( option1 ).Add( options2 ).Add( options3 );
Options inherits IEnumerable so it can be iterated:
foreach ( var option in options ) {
Console.WriteLine( $"Included Option: {option}" );
}
Now we are ready to parse command line arguments. In the following snippet we assume args is string[] provided by the Main method:
var matches = options.Parse( args );
Should a supplied argument not match any Option in the Options collection then the method will throw a ArgumentException with a detailed description. The exception are free-standing arguments without a short or long prefix. The return value of the Parse() method is a IEnumerable where T is Match. Let's assume we parsed first -a -g foo -g bar last then the result would include three matches. The first one for the free standing arguments first and last, the second for -a, and the third for -g. A Match has the following properties:
ShortName: The short name of the matched option (In this example eitheraorg).LongName: The long name of the matched option (In this example eitheralphaorgamma).Count: The number of times theOptionwas matched. (1foraand2forg).Arguments: The read-only collection of all arguments supplied to the matched options. (Empty forabut would containfooandbarforg).
A match for free-standing arguments doesn't have a ShortName or LongName. The Count is set to the amount of free-standing arguments. Arguments will be a list of all free-standing arguments encountered (In this case "first" and "last").
The best way to query the matches is by using Linq extensions:
// Get all arguments for -g/--gamma
var arguments = matches.Where( x => x.ShortName == "g" ).First().Arguments;
The default option prefixes - and -- can be changed to something else during the initialization of Options:
var options = new Options( "/", "//" );
Now short-name options would have to be prefixed with / and long-name options with // instead of - and --.
| 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 was computed. 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.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.