Let inputs read equal-separated values

This commit is contained in:
Matt Nadareski
2024-12-04 22:14:06 -05:00
parent 8c0bbfbf10
commit c7c700e567
14 changed files with 611 additions and 266 deletions

View File

@@ -30,6 +30,7 @@
- Add unused inputs to Aaru
- Add self-formatting to Input types
- Handle issue with old .NET
- Let inputs read equal-separated values
### 3.2.4 (2024-11-24)

View File

@@ -46,9 +46,13 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, true)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, true)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "true" }, 0, true, true)]
[InlineData("flag", true, new string[] { "flag", "false" }, 0, true, false)]
[InlineData("flag", true, new string[] { "flag=true" }, 0, true, true)]
[InlineData("flag", true, new string[] { "flag=false" }, 0, true, false)]
public void BooleanInputTest(string name, bool required, string[] parts, int index, bool success, bool? expected)
{
BooleanInput input = new BooleanInput(name, required);
@@ -77,9 +81,13 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, sbyte.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, sbyte.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (sbyte)1)]
[InlineData("flag", true, new string[] { "flag", "-1" }, 0, true, (sbyte)-1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (sbyte)1)]
[InlineData("flag", true, new string[] { "flag=-1" }, 0, true, (sbyte)-1)]
public void Int8InputTest(string name, bool required, string[] parts, int index, bool success, sbyte? expected)
{
Int8Input input = new Int8Input(name, required);
@@ -108,8 +116,11 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, byte.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, byte.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (byte)1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (byte)1)]
public void UInt8InputTest(string name, bool required, string[] parts, int index, bool success, byte? expected)
{
UInt8Input input = new UInt8Input(name, required);
@@ -138,9 +149,13 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, short.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, short.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (short)1)]
[InlineData("flag", true, new string[] { "flag", "-1" }, 0, true, (short)-1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (short)1)]
[InlineData("flag", true, new string[] { "flag=-1" }, 0, true, (short)-1)]
public void Int16InputTest(string name, bool required, string[] parts, int index, bool success, short? expected)
{
Int16Input input = new Int16Input(name, required);
@@ -169,8 +184,11 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, ushort.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, ushort.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (ushort)1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (ushort)1)]
public void UInt16InputTest(string name, bool required, string[] parts, int index, bool success, ushort? expected)
{
UInt16Input input = new UInt16Input(name, required);
@@ -199,9 +217,13 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, int.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, int.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (int)1)]
[InlineData("flag", true, new string[] { "flag", "-1" }, 0, true, (int)-1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (int)1)]
[InlineData("flag", true, new string[] { "flag=-1" }, 0, true, (int)-1)]
public void Int32InputTest(string name, bool required, string[] parts, int index, bool success, int? expected)
{
Int32Input input = new Int32Input(name, required);
@@ -230,8 +252,11 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, uint.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, uint.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (uint)1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (uint)1)]
public void UInt32InputTest(string name, bool required, string[] parts, int index, bool success, uint? expected)
{
UInt32Input input = new UInt32Input(name, required);
@@ -260,9 +285,13 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, long.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, long.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (long)1)]
[InlineData("flag", true, new string[] { "flag", "-1" }, 0, true, (long)-1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (long)1)]
[InlineData("flag", true, new string[] { "flag=-1" }, 0, true, (long)-1)]
public void Int64InputTest(string name, bool required, string[] parts, int index, bool success, long? expected)
{
Int64Input input = new Int64Input(name, required);
@@ -291,8 +320,11 @@ namespace MPF.ExecutionContexts.Test
// Valid name, invalid following
[InlineData("flag", true, new string[] { "flag", "invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag", "invalid" }, 0, true, ulong.MinValue)]
[InlineData("flag", true, new string[] { "flag=invalid" }, 0, false, null)]
[InlineData("flag", false, new string[] { "flag=invalid" }, 0, true, ulong.MinValue)]
// Valid name, valid following
[InlineData("flag", true, new string[] { "flag", "1" }, 0, true, (ulong)1)]
[InlineData("flag", true, new string[] { "flag=1" }, 0, true, (ulong)1)]
public void UInt64InputTest(string name, bool required, string[] parts, int index, bool success, ulong? expected)
{
UInt64Input input = new UInt64Input(name, required);
@@ -320,6 +352,7 @@ namespace MPF.ExecutionContexts.Test
[InlineData("flag", false, new string[] { "flag" }, 0, true, "")]
// Valid name, following
[InlineData("flag", true, new string[] { "flag", "value" }, 0, true, "value")]
[InlineData("flag", true, new string[] { "flag=value" }, 0, true, "value")]
public void StringInputTest(string name, bool required, string[] parts, int index, bool success, string? expected)
{
StringInput input = new StringInput(name, required);
@@ -331,46 +364,6 @@ namespace MPF.ExecutionContexts.Test
#endregion
#region DoesExist
[Fact]
public void DoesExist_Empty_False()
{
string[] parts = [];
int index = 0;
bool actual = Input.DoesExist(parts, index);
Assert.False(actual);
}
[Fact]
public void DoesExist_Negative_False()
{
string[] parts = ["item"];
int index = -1;
bool actual = Input.DoesExist(parts, index);
Assert.False(actual);
}
[Fact]
public void DoesExist_Greater_False()
{
string[] parts = ["item"];
int index = 1;
bool actual = Input.DoesExist(parts, index);
Assert.False(actual);
}
[Fact]
public void DoesExist_Valid_True()
{
string[] parts = ["item"];
int index = 0;
bool actual = Input.DoesExist(parts, index);
Assert.True(actual);
}
#endregion
#region ExtractFactorFromValue
[Theory]

View File

@@ -59,33 +59,58 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
Value = _required ? null : true;
return !_required;
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : true;
return !_required;
}
// If the next value is valid
if (!bool.TryParse(parts[index + 1], out bool value))
{
Value = _required ? null : true;
return !_required;
}
index++;
Value = value;
return true;
}
// If the next value is valid
if (!bool.TryParse(parts[index + 1], out bool value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
Value = _required ? null : true;
return !_required;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : true;
return !_required;
}
// If the next value is valid
if (!bool.TryParse(val, out bool value))
{
Value = _required ? null : true;
return !_required;
}
Value = value;
return true;
}
index++;
return value;
return false;
}
}
}

View File

@@ -50,19 +50,17 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
Value = true;
return true;
}
Value = true;
return true;
return false;
}
}
}

View File

@@ -91,15 +91,6 @@ namespace MPF.ExecutionContexts.Data
#region Helpers
/// <summary>
/// Returns whether or not the selected item exists
/// </summary>
/// <param name="parts">Parts array to be referenced</param>
/// <param name="index">Current index</param>
/// <returns>True if the next item exists, false otherwise</returns>
internal static bool DoesExist(string[] parts, int index)
=> index >= 0 && index < parts.Length;
/// <summary>
/// Get the trimmed value and multiplication factor from a value
/// </summary>

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : short.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out short? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : short.MinValue;
return !_required;
}
// If the next value is valid
if (short.TryParse(parts[index + 1], out short value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : short.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out short? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : short.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out short? output)
{
// If the next value is valid
if (short.TryParse(str, out short value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (short.TryParse(baseVal, out value))
{
index++;
Value = (short)(value * factor);
output = (short)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (short.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (short)(value * factor);
output = (short)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : short.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : int.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out int? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : int.MinValue;
return !_required;
}
// If the next value is valid
if (int.TryParse(parts[index + 1], out int value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : int.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out int? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : int.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out int? output)
{
// If the next value is valid
if (int.TryParse(str, out int value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (int.TryParse(baseVal, out value))
{
index++;
Value = (int)(value * factor);
output = (int)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (int.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (int)(value * factor);
output = (int)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : int.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : long.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out long? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : long.MinValue;
return !_required;
}
// If the next value is valid
if (long.TryParse(parts[index + 1], out long value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : long.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out long? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : long.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out long? output)
{
// If the next value is valid
if (long.TryParse(str, out long value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (long.TryParse(baseVal, out value))
{
index++;
Value = (long)(value * factor);
output = (long)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (long.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (long)(value * factor);
output = (long)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : long.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : sbyte.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out sbyte? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : sbyte.MinValue;
return !_required;
}
// If the next value is valid
if (sbyte.TryParse(parts[index + 1], out sbyte value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : sbyte.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out sbyte? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : sbyte.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out sbyte? output)
{
// If the next value is valid
if (sbyte.TryParse(str, out sbyte value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (sbyte.TryParse(baseVal, out value))
{
index++;
Value = (sbyte)(value * factor);
output = (sbyte)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (sbyte.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (sbyte)(value * factor);
output = (sbyte)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : sbyte.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -67,27 +67,44 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
Value = _required ? null : string.Empty;
return !_required;
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : string.Empty;
return !_required;
}
index++;
Value = parts[index];
return true;
}
index++;
Value = parts[index];
return true;
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : string.Empty;
return !_required;
}
Value = val;
return true;
}
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : ushort.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out ushort? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : ushort.MinValue;
return !_required;
}
// If the next value is valid
if (ushort.TryParse(parts[index + 1], out ushort value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : ushort.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out ushort? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : ushort.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out ushort? output)
{
// If the next value is valid
if (ushort.TryParse(str, out ushort value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (ushort.TryParse(baseVal, out value))
{
index++;
Value = (ushort)(value * factor);
output = (ushort)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (ushort.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (ushort)(value * factor);
output = (ushort)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : ushort.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : uint.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out uint? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : uint.MinValue;
return !_required;
}
// If the next value is valid
if (uint.TryParse(parts[index + 1], out uint value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : uint.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out uint? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : uint.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out uint? output)
{
// If the next value is valid
if (uint.TryParse(str, out uint value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (uint.TryParse(baseVal, out value))
{
index++;
Value = (uint)(value * factor);
output = (uint)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (uint.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (uint)(value * factor);
output = (uint)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : uint.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : ulong.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out ulong? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : ulong.MinValue;
return !_required;
}
// If the next value is valid
if (ulong.TryParse(parts[index + 1], out ulong value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : ulong.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out ulong? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : ulong.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out ulong? output)
{
// If the next value is valid
if (ulong.TryParse(str, out ulong value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (ulong.TryParse(baseVal, out value))
{
index++;
Value = (ulong)(value * (ulong)factor);
output = (ulong)(value * (ulong)factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (ulong.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (ulong)(value * (ulong)factor);
output = (ulong)(value * (ulong)factor);
return true;
}
// Return value based on required flag
Value = _required ? null : ulong.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}

View File

@@ -68,38 +68,79 @@ namespace MPF.ExecutionContexts.Data
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (parts.Length == 0)
return false;
// Check the index
if (index < 0 || index >= parts.Length)
return false;
// Check the name
if (parts[index] != Name && (_longName != null && parts[index] != _longName))
return false;
// Ensure the value exists
if (!DoesExist(parts, index + 1))
// Check for space-separated
if (parts[index] == Name || (_longName != null && parts[index] == _longName))
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : byte.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(parts[index + 1], out byte? value) && value != null)
{
index++;
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : byte.MinValue;
return !_required;
}
// If the next value is valid
if (byte.TryParse(parts[index + 1], out byte value))
// Check for equal separated
if (parts[index].StartsWith($"{Name}=") || (_longName != null && parts[index].StartsWith($"{_longName}=")))
{
index++;
Value = value;
// Split the string, using the first equal sign as the separator
string[] tempSplit = parts[index].Split('=');
string key = tempSplit[0];
string val = string.Join("=", tempSplit, 1, tempSplit.Length - 1);
// Ensure the value exists
if (string.IsNullOrEmpty(val))
{
Value = _required ? null : byte.MinValue;
return !_required;
}
// If the next value is valid
if (ParseValue(val, out byte? value) && value != null)
{
Value = value;
return true;
}
// Return value based on required flag
Value = _required ? null : byte.MinValue;
return !_required;
}
return false;
}
/// <summary>
/// Parse a value from a string
/// </summary>
private static bool ParseValue(string str, out byte? output)
{
// If the next value is valid
if (byte.TryParse(str, out byte value))
{
output = value;
return true;
}
// Try to process as a formatted string
string baseVal = ExtractFactorFromValue(parts[index + 1], out long factor);
string baseVal = ExtractFactorFromValue(str, out long factor);
if (byte.TryParse(baseVal, out value))
{
index++;
Value = (byte)(value * factor);
output = (byte)(value * factor);
return true;
}
@@ -107,14 +148,13 @@ namespace MPF.ExecutionContexts.Data
string hexValue = RemoveHexIdentifier(baseVal);
if (byte.TryParse(hexValue, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value))
{
index++;
Value = (byte)(value * factor);
output = (byte)(value * factor);
return true;
}
// Return value based on required flag
Value = _required ? null : byte.MinValue;
return !_required;
// The value could not be parsed
output = null;
return false;
}
}
}