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