ArgumentString 1.0.4

dotnet add package ArgumentString --version 1.0.4
NuGet\Install-Package ArgumentString -Version 1.0.4
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="ArgumentString" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ArgumentString --version 1.0.4
#r "nuget: ArgumentString, 1.0.4"
#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 ArgumentString as a Cake Addin
#addin nuget:?package=ArgumentString&version=1.0.4

// Install ArgumentString as a Cake Tool
#tool nuget:?package=ArgumentString&version=1.0.4

ArgumentString

Logo
.NET 6 Build and Test

What is an argument string, you might ask? The idea is borrowed from connection strings.
So an argument string literal looks like this: "foo=bar;version=1". The library allows you to access these arguments easily by key or by index. You can customize the library's behavior and easily deal with faulty and default values in case the key was not found.

By providing options you can customize this library to your needs, e. g.

  • setting mandatory fields that are checked on object instantiation
  • change argument and key-value separators (like the "foo->bar|version->1" syntax more?)
  • accessing faulty keys will not throw an exception by default, but you can throw one if you like to
  • accessing faulty keys will always return an empty string by default, but you can return null if you like to (unless you are providing a default value)

This is a tiny but fully tested and stable library.

Installation

  • Install via NuGet: PM> Install-Package ArgumentString
  • Build from your own

Object creation

Simplest examples:

var arguments = new ArgumentString("foo=bar");
var arguments = new ArgumentString("foo=bar;version=1");

Examples with options:

var arguments = new ArgumentString("foo=bar", new ParseOptions("foo"));

var arguments = new ArgumentString("foo->bar|version->1", options => { 
    options.MandatoryKeys = new List<string> { "foo" };
    options.ArgumentSeparator = "|";
    options.KeyValueSeparator = "->";
    options.ThrowOnAccessIfKeyNotFound = true;
    options.ReturnEmptyStringInsteadOfNull = false;
});

Getting values

  • When getting values you can choose from using the Get() or Get<T>() method or the indexer[].
  • All methods and indexers behave the same way
  • If a default value other than null gets provided the ReturnEmptyStringInsteadOfNull option has no effect.
  • See Big O notation (complexity) in the methods description

Signatures:

  • Get(string key, string? defaultValue = null)
  • Get(int index, string? defaultValue = null)
  • Get<T>(string key, T? defaultValue = null)
  • Get<T>(int index, T? defaultValue = null)
  • Indexer this[string key, T? defaultValue = null]
  • Indexer this[int index, T? defaultValue = null]

Accessing values is the most fun part:

var arguments = new ArgumentString("foo=bar;version=1");

string foo = arguments.Get("foo"); // -> bar
string foo = arguments["foo"]; // -> bar

string foo = arguments.Get(0); // -> bar
string foo = arguments[0]; // -> bar

Dealing with faulty values:

var arguments = new ArgumentString("foo=bar;version=1");

string foo = arguments.Get("missing"); // -> `string.Empty` if `ThrowOnAccessIfKeyNotFound` is false (default)
string foo = arguments.Get("missing"); // -> `MissingArgumentException` if `ThrowOnAccessIfKeyNotFound` is true
string foo = arguments["missing"]; // -> same as above

string foo = arguments.Get(2); // -> `string.Empty` if `ThrowOnAccessIfKeyNotFound` is false (default)
string foo = arguments.Get(2); // -> `MissingArgumentException` if `ThrowOnAccessIfKeyNotFound` is true
string foo = arguments[2]; // -> same as above

Dealing with default values (second parameter defaultValue on Get() method):

var arguments = new ArgumentString("foo=bar;version=1");

string foo = arguments.Get("missing", "bar"); // -> `bar` if `ThrowOnAccessIfKeyNotFound` is false (default)
string foo = arguments.Get("missing", "bar"); // -> `MissingArgumentException` if `ThrowOnAccessIfKeyNotFound` is true
string foo = arguments["missing", "bar"]; // -> same as above

string foo = arguments.Get(2, "bar"); // -> `bar` if `ThrowOnAccessIfKeyNotFound` is false (default)
string foo = arguments.Get(2, "bar"); // -> `MissingArgumentException` if `ThrowOnAccessIfKeyNotFound` is true
string foo = arguments[2, "bar"]; // -> same as above

Need to work with a specific format (type conversion)?
You should pay attention to pass correct values for the conversion to work. For that reason there are some more exceptions that will be thrown.

var arguments = new ArgumentString("foo=bar;version=1");

float version = arguments.Get<float>("version"); // -> (float)1 
float version = arguments.Get<float>("missing", 99); // -> (float)99 

float version = arguments.Get<float>(1); // -> (float)1 
float version = arguments.Get<float>(2, 99); // -> (float)99 
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ArgumentString:

Package Downloads
Appi.Infrastructure

The goal is to query your sources for information through one tool; all at once, in groups or individually, highly extensible. Use this package to create your own Appi plugins with pre-built infrastructure like SQL Server or MySQL to speed up your development.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 588 8/25/2023
1.0.3 554 8/15/2023
1.0.2 523 8/14/2023
1.0.1 565 8/12/2023
1.0.0 554 8/11/2023

New version with default values.