Доработано формирование аргументов
All checks were successful
CI / build-test (push) Successful in 29s
Release / pack-and-publish (release) Successful in 35s

This commit is contained in:
2025-11-27 10:41:57 +03:00
parent 6b17e6c067
commit 363c078321
3 changed files with 26 additions and 8 deletions

View File

@@ -136,10 +136,16 @@ public static class ArgumentsParser
return result;
}
public static string ToArguments<T>(T options)
/// <summary>
/// Формирование строки аргументов
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="options"></param>
/// <returns></returns>
public static string ToArguments<T>(T options, bool useShortName = false)
{
var props = typeof(T).GetProperties();
var parts = new List<string>();
var parts = new List<KeyValuePair<string, string>>();
foreach (var prop in props)
{
@@ -151,17 +157,22 @@ public static class ArgumentsParser
string prefix = "--" + opt.Name;
if (useShortName && !string.IsNullOrWhiteSpace(opt.ShortName))
{
prefix = "-" + opt.ShortName;
}
if (prop.PropertyType == typeof(bool))
{
if ((bool)value)
parts.Add(prefix);
parts.Add(new(prefix, ""));
}
else if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
{
var list = (System.Collections.IList)value;
if (list.Count > 0)
parts.Add($"{prefix} {string.Join(",", list.Cast<object>())}");
parts.Add(new(prefix, string.Join(",", list.Cast<object>())));
}
else if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
@@ -171,15 +182,15 @@ public static class ArgumentsParser
foreach (var k in dict.Keys)
items.Add($"{k}={dict[k]}");
if (items.Count > 0)
parts.Add($"{prefix} {string.Join(",", items)}");
parts.Add(new(prefix, string.Join(",", items)));
}
else
{
parts.Add($"{prefix} {value}");
parts.Add(new(prefix, $"{value}"));
}
}
return string.Join(" ", parts);
return string.Join(" ", parts.Select(kvp => kvp.Key + " \"" + kvp.Value + "\""));
}
private static bool TryParse(Type targetType, string input, IOptionConverter? converterAttr, out object? value)