GetOpts 0.3.0

dotnet add package GetOpts --version 0.3.0
NuGet\Install-Package GetOpts -Version 0.3.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GetOpts" Version="0.3.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GetOpts --version 0.3.0
#r "nuget: GetOpts, 0.3.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GetOpts as a Cake Addin
#addin nuget:?package=GetOpts&version=0.3.0

// Install GetOpts as a Cake Tool
#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:

  • option1 doesn't have an argument (Only -a or --alpha are valid).
  • option2 requires and argument (Only -b argument or --beta argument are valid).
  • option3 can either have an argument or omit it.

The occurance rules of the above options are as follows:

  • option1 must occur exactly once in the provided arguments.
  • option2 can occur once in the provided arguments but doesn't have to.
  • option3 can 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 either a or g).
  • LongName: The long name of the matched option (In this example either alpha or gamma).
  • Count: The number of times the Option was matched. (1 for a and 2 for g ).
  • Arguments: The read-only collection of all arguments supplied to the matched options. (Empty for a but would contain foo and bar for g).

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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
0.3.0 1,084 6/3/2018
0.2.0 959 5/30/2018
0.1.0 877 5/21/2018