MagnetArgs 0.2.4-beta

This is a prerelease version of MagnetArgs.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package MagnetArgs --version 0.2.4-beta
NuGet\Install-Package MagnetArgs -Version 0.2.4-beta
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="MagnetArgs" Version="0.2.4-beta" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MagnetArgs --version 0.2.4-beta
#r "nuget: MagnetArgs, 0.2.4-beta"
#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 MagnetArgs as a Cake Addin
#addin nuget:?package=MagnetArgs&version=0.2.4-beta&prerelease

// Install MagnetArgs as a Cake Tool
#tool nuget:?package=MagnetArgs&version=0.2.4-beta&prerelease

MagnetArgs

MagnetArgs helps with the task of mapping arguments to objects. Accelerating the process of define option variables in console applications or map complex objects in a variety of scenarios.

Basic Tasks

  1. Map your option objects.
  2. Magnetize them.
  3. Enjoy!

How to set an Option Object

It's as simple as define a class, extend from MagnetOption and define the Arg-ument attribute in the properties you want to map. You can define an alias if you need a shorcut word for your arguments.

Example:

class TypeObject : MagnetOption
{
    [Arg("string-value", Alias = "string")]
    public string StringValue { get; set; }

    [Arg("char-value", Alias = "char")]
    public char CharValue { get; set; }

    [Arg("integer-value", Alias = "int")]
    public int IntegerValue { get; set; }

    [Arg("long-value", Alias = "long")]
    public long LongValue { get; set; }

    [Arg("boolean-value", Alias = "bool")]
    public bool BooleanValue { get; set; }

    [Arg("float-value", Alias = "float")]
    public float FloatValue { get; set; }

    [Arg("double-value", Alias = "double")]
    public double DoubleValue { get; set; }

    [Arg("decimal-value", Alias = "decimal")]
    public decimal DecimalValue { get; set; }
}

How can you Magnetize an Object

If you have an instance of your option object, then you can magnetize it. Provide the object and the arguments to Magnet. It will be automatically mapped.

Example:

var args = new string[];
var obj = new TypeObject();

Magnet.Magnetize(obj, args);

How to manage Exceptions

What happens if an error occurred? All errors are stored in the Exceptions property in your option object. You can access them as in the example:

foreach (var ex in obj.Exceptions)
{
    throw ex;
}

What about Complex Options

Any complex object like classes need to be parsed. You can define how convert an input string to any object with the rules you define.

Define a custom parser implementing the interface IParser.

class CustomPointParser : IParser
{
    public object Parse(string value)
    {
        var values = value.Replace("[", "").Replace("]", "").Trim().Split(',');
        if (values.Length == 2)
        {
            var obj = new CustomPoint();
            obj.X = int.Parse(values[0]);
            obj.Y = int.Parse(values[1]);

            return obj;
        }
        else
        {
            throw new Exception("Error parsing CustomPoint");
        }
    }
}

Set within an argument the Parser attribute, then specify your custom parser.

class CustomObject : MagnetOption
{
    [Arg("custom-point", Alias = "point"), Parser(typeof(CustomPointParser))]
    public CustomPoint Point { get; set; }
}

It's done, now you can Magnetize your instance.

var obj = new ComplexObject();

Magnet.Magnetize(obj, args);

Argument Rules

With MagnetArgs you can define arguments with the following rules:

  • Is-Required
  • If-Present
  • Default
  • Parse
  • OptionSet

Use any combination to archieve the behaviour you need.

Is-Required Rule

This rule lauches an exception if there's no value or label in the arguments array.

Example:

// Option class
class ComplexObject : MagnetOption
{
    [Arg("required-value"), IsRequired]
    public string RequiredValue { get; set; }
}

// Magnetize
var obj = new ComplexObject();

Magnet.Magnetize(obj, args);

// If not found, complex object contains a IsRequiredException.
foreach (var ex in obj.Exceptions)
{
    throw ex;
}

If-Present Rule

This rule evaluates if exist a label in the arguments array. If option is of boolean type, it will set with the value 'true'. If option is of any other type, it will be set with the 'default' value.

Example:

// Option class
class ComplexObject : MagnetOption
{
    [Arg("present-value"), IfPresent]
    public bool PresentValue { get; set; }

    [Arg("default-value"), IfPresent, Default("25")]
    public int DefaultValue { get; set; }
}

// Magnetize
var obj = new ComplexObject();

Magnet.Magnetize(obj, args);

Default Value Rule

This rule sets a default value if no value provided in the arguments array. Complex values require a MagnetArgs.ParserAttribute definition.

Example:

// Option class
class ComplexObject : MagnetOption
{
    [Arg("default-value"), Default("25")]
    public int DefaultValue { get; set; }
}

// Magnetize
var obj = new ComplexObject();

Magnet.Magnetize(obj, args);

Parse Value Rule

This rule specifies the parser required to convert a text input to a class instance.

Example:

// Option class
class ComplexObject : MagnetOption
{
    [Arg("custom-point", Alias = "point"), Parser(typeof(CustomPointParser))]
    public CustomPoint Point { get; set; }
}

// Magnetize
var obj = new ComplexObject();

Magnet.Magnetize(obj, args);

Magnetize with OptionSet

When you have an object with multiple option classes, you can magnetize them at once using OptionSet. Specify the OptionSet attribute in the properties with options who implement MagnetArgs.ArgAttribute. You will be able to magnetize the full object.

Example:

// Option classes
class PresentObject : MagnetOption
{
    [Arg("present-value"), IfPresent]
    public bool PresentValue { get; set; }
}

class CustomObject : MagnetOption
{
    [Arg("custom-point", Alias = "point"), Parser(typeof(CustomPointParser))]
    public CustomPoint Point { get; set; }
}

// OptionSet class
class OptionSetObject
{
    [OptionSet]
    public PresentPassObject PObject { get; set; }

    [OptionSet]
    public CustomObject CObject { get; set; }
}

// Magnetize
var obj = new OptionSetObject();

Magnet.Magnetize(obj, args);

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 is compatible.  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 net40 is compatible.  net403 was computed.  net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  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.
  • .NETCoreApp 2.0

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MagnetArgs:

Package Downloads
EasyApp

A simple console framework.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.2.13 467 11/21/2023
0.6.2 286 11/19/2023
0.6.1 384 11/7/2023
0.4.0 778 7/9/2022
0.3.0 683 3/12/2021

Mapping simplification.
Minor improvements.