Files
MPF/MPF.ExecutionContexts/Data/StringInput.cs

126 lines
3.6 KiB
C#
Raw Normal View History

using System;
2024-12-04 21:20:38 -05:00
using System.Text;
2024-12-04 20:22:47 -05:00
namespace MPF.ExecutionContexts.Data
{
/// <summary>
/// Represents a string flag with an optional trailing value
/// </summary>
public class StringInput : Input<string>
2024-12-05 13:22:17 -05:00
2024-12-04 20:22:47 -05:00
{
2024-12-05 13:22:17 -05:00
#region Properties
/// <summary>
/// Indicates whether quotes are used in output or not
/// </summary>
public bool Quotes { get; set; } = false;
#endregion
2024-12-04 20:22:47 -05:00
#region Constructors
/// <inheritdoc/>
public StringInput(string name)
: base(name) { }
/// <inheritdoc/>
public StringInput(string name, bool required)
: base(name, required) { }
/// <inheritdoc/>
public StringInput(string shortName, string longName)
: base(shortName, longName) { }
/// <inheritdoc/>
public StringInput(string shortName, string longName, bool required)
: base(shortName, longName, required) { }
/// <inheritdoc/>
public StringInput(string[] names)
: base(names) { }
/// <inheritdoc/>
public StringInput(string[] names, bool required)
: base(names, required) { }
2024-12-04 20:22:47 -05:00
#endregion
2024-12-04 21:20:38 -05:00
/// <inheritdoc/>
public override string Format(bool useEquals)
{
// Do not output if there is no value
if (Value == null)
return string.Empty;
// Build the output format
var builder = new StringBuilder();
// Flag name
builder.Append(Name);
2024-12-04 21:20:38 -05:00
// Only output separator and value if needed
if (_required || (!_required && Value != string.Empty))
{
// Separator
if (useEquals)
builder.Append("=");
else
builder.Append(" ");
// Value
2024-12-05 13:22:17 -05:00
if (Quotes)
builder.Append($"\"{Value}\"");
else
builder.Append(Value);
2024-12-04 21:20:38 -05:00
}
return builder.ToString();
}
2024-12-04 20:22:47 -05:00
/// <inheritdoc/>
public override bool Process(string[] parts, ref int index)
{
// Check the parts array
if (index < 0 || index >= parts.Length)
return false;
2024-12-04 22:14:06 -05:00
// Check for space-separated
string part = parts[index];
if (part == Name || (_altNames.Length > 0 && Array.FindIndex(_altNames, n => n == part) > -1))
2024-12-04 22:14:06 -05:00
{
// Ensure the value exists
if (index + 1 >= parts.Length)
{
Value = _required ? null : string.Empty;
return !_required;
}
index++;
Value = parts[index].Trim('"');
2024-12-04 22:14:06 -05:00
return true;
}
2024-12-04 20:22:47 -05:00
2024-12-04 22:14:06 -05:00
// Check for equal separated
if (part.StartsWith($"{Name}=") || (_altNames.Length > 0 && Array.FindIndex(_altNames, n => part.StartsWith($"{n}=")) > -1))
2024-12-04 20:22:47 -05:00
{
2024-12-04 22:14:06 -05:00
// Split the string, using the first equal sign as the separator
string[] tempSplit = part.Split('=');
2024-12-04 22:14:06 -05:00
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.Trim('"');
2024-12-04 22:14:06 -05:00
return true;
2024-12-04 20:22:47 -05:00
}
2024-12-04 22:14:06 -05:00
return false;
2024-12-04 20:22:47 -05:00
}
}
}