GetOpts 0.1.0
See the version list below for details.
dotnet add package GetOpts --version 0.1.0
NuGet\Install-Package GetOpts -Version 0.1.0
<PackageReference Include="GetOpts" Version="0.1.0" />
paket add GetOpts --version 0.1.0
#r "nuget: GetOpts, 0.1.0"
// Install GetOpts as a Cake Addin
#addin nuget:?package=GetOpts&version=0.1.0
// Install GetOpts as a Cake Tool
#tool nuget:?package=GetOpts&version=0.1.0
How to use
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 option2 = new Option( "g", "gamma", Argument.OPTIONAL, Occur.MULTIPLE );
Every option consists a short and long name, as well as various occurance 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-a argument
or--alpha argument
are valid).option2
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<T>
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 return value of the Parse()
method is a IEnumerable<T>
where T
is Match
. Let's assume we parsed -a -g foo -g bar
then the result would include two matches. One for -a
and one for -g
. A Match
has the following properties:
ShortName
: The short name of the matched option (In this example eithera
org
).LongName
: The long name of the matched option (In this example eitheralpha
orgamma
).Count
: The number of times theOption
was matched. (1
fora
and2
forg
).Arguments
: The read-only collection of all arguments supplied to the matched options. (Empty fora
but would contain"foo"
and"bar"
forg
).
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;
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. |
.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.