using System.Collections; using System.Text.RegularExpressions; using OneOf; namespace StabilityMatrix.Core.Processes; /// /// Parameter type for command line arguments /// Implicitly converts between string and string[], /// with no parsing if the input and output types are the same. /// public partial class ProcessArgs : OneOfBase, IEnumerable { /// public ProcessArgs(OneOf input) : base(input) { } /// /// Whether the argument string contains the given substring, /// or any of the given arguments if the input is an array. /// public bool Contains(string arg) => Match(str => str.Contains(arg), arr => arr.Any(Contains)); public ProcessArgs Concat(ProcessArgs other) => Match( str => new ProcessArgs(string.Join(' ', str, other.ToString())), arr => new ProcessArgs(arr.Concat(other.ToArray()).ToArray()) ); public ProcessArgs Prepend(ProcessArgs other) => Match( str => new ProcessArgs(string.Join(' ', other.ToString(), str)), arr => new ProcessArgs(other.ToArray().Concat(arr).ToArray()) ); /// public IEnumerator GetEnumerator() { return ToArray().AsEnumerable().GetEnumerator(); } /// public override string ToString() { return Match(str => str, arr => string.Join(' ', arr.Select(ProcessRunner.Quote))); } /// IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public string[] ToArray() => Match( str => ArgumentsRegex().Matches(str).Select(x => x.Value.Trim('"')).ToArray(), arr => arr ); // Implicit conversions public static implicit operator ProcessArgs(string input) => new(input); public static implicit operator ProcessArgs(string[] input) => new(input); public static implicit operator string(ProcessArgs input) => input.ToString(); public static implicit operator string[](ProcessArgs input) => input.ToArray(); [GeneratedRegex("""[\"].+?[\"]|[^ ]+""", RegexOptions.IgnoreCase)] private static partial Regex ArgumentsRegex(); }