Skip to content

Commit 9f16252

Browse files
committed
refactor(options): 代码质量改进
OptionsDebugger: - 提取魔法数字为常量 MaxDisplayElements 和 DefaultConsoleWidth - 添加 private 访问修饰符 - 修复数组类型显示的尾随空格 OptionsProvider: - 重命名变量 cachedOptions2 为 finalOptions - 使用 ArgumentNullException.ThrowIfNull 替代显式抛出 OptionAttribute: - 添加 DebuggerDisplay 特性提升调试体验
1 parent 4dc12a9 commit 9f16252

3 files changed

Lines changed: 23 additions & 18 deletions

File tree

GameFrameX.Foundation.Options/Attributes/OptionAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace GameFrameX.Foundation.Options.Attributes;
3737
/// 表示一个命令行选项的特性
3838
/// </summary>
3939
[AttributeUsage(AttributeTargets.Property)]
40+
[System.Diagnostics.DebuggerDisplay("LongName = {LongName}, Required = {Required}, DefaultValue = {DefaultValue}")]
4041
public class OptionAttribute : Attribute
4142
{
4243
/// <summary>

GameFrameX.Foundation.Options/OptionsDebugger.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,18 @@ namespace GameFrameX.Foundation.Options
4141
/// </summary>
4242
public static class OptionsDebugger
4343
{
44-
const string OptionHeader = "选项 (Option)";
45-
const string ValueHeader = "值 (Value)";
46-
const string RequiredHeader = "必需 (Required)";
47-
const string TypeNameHeader = "类型 (Type)";
48-
const string DescriptionHeader = "描述 (Description)";
49-
const string DefaultValueHeader = "默认值 (Default)";
50-
const string RequiredYesLabel = "是 (Yes)";
51-
const string RequiredNoLabel = "否 (No)";
52-
const string NoDescriptionLabel = "无描述 (No Description)";
53-
const string NoOptionAttributeLabel = "无选项特性 (No Option Attribute)";
44+
private const string OptionHeader = "选项 (Option)";
45+
private const string ValueHeader = "值 (Value)";
46+
private const string RequiredHeader = "必需 (Required)";
47+
private const string TypeNameHeader = "类型 (Type)";
48+
private const string DescriptionHeader = "描述 (Description)";
49+
private const string DefaultValueHeader = "默认值 (Default)";
50+
private const string RequiredYesLabel = "是 (Yes)";
51+
private const string RequiredNoLabel = "否 (No)";
52+
private const string NoDescriptionLabel = "无描述 (No Description)";
53+
private const string NoOptionAttributeLabel = "无选项特性 (No Option Attribute)";
54+
private const int MaxDisplayElements = 5;
55+
private const int DefaultConsoleWidth = 120;
5456

5557
/// <summary>
5658
/// 打印解析完成后的选项对象
@@ -174,7 +176,7 @@ public static void PrintParsedOptions<T>(T options) where T : class
174176
}
175177
catch
176178
{
177-
consoleWidth = 120;
179+
consoleWidth = DefaultConsoleWidth;
178180
}
179181

180182
int maxTableWidth = Math.Max(60, consoleWidth - 1);
@@ -452,13 +454,13 @@ private static string FormatPropertyValue(object value)
452454
{
453455
var array = (Array)value;
454456
var elements = new List<string>();
455-
for (int i = 0; i < Math.Min(array.Length, 5); i++)
457+
for (int i = 0; i < Math.Min(array.Length, MaxDisplayElements); i++)
456458
{
457459
elements.Add(array.GetValue(i)?.ToString() ?? "null");
458460
}
459461

460462
var result = $"[{string.Join(", ", elements)}]";
461-
if (array.Length > 5)
463+
if (array.Length > MaxDisplayElements)
462464
{
463465
result += $" (Total {array.Length} elements / 共{array.Length}个元素)";
464466
}
@@ -470,13 +472,13 @@ private static string FormatPropertyValue(object value)
470472
{
471473
var list = (System.Collections.IList)value;
472474
var elements = new List<string>();
473-
for (int i = 0; i < Math.Min(list.Count, 5); i++)
475+
for (int i = 0; i < Math.Min(list.Count, MaxDisplayElements); i++)
474476
{
475477
elements.Add(list[i]?.ToString() ?? "null");
476478
}
477479

478480
var result = $"[{string.Join(", ", elements)}]";
479-
if (list.Count > 5)
481+
if (list.Count > MaxDisplayElements)
480482
{
481483
result += $" (Total {list.Count} elements / 共{list.Count}个元素)";
482484
}
@@ -539,7 +541,7 @@ private static string GetFriendlyTypeName(Type type)
539541

540542
if (type.IsArray)
541543
{
542-
return $"Array of {GetFriendlyTypeName(type.GetElementType())} ";
544+
return $"Array of {GetFriendlyTypeName(type.GetElementType())}";
543545
}
544546

545547
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))

GameFrameX.Foundation.Options/OptionsProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ private static bool ShouldEnableDebugOutput(bool? enableDebugOutput = null)
152152
var options = builder.Build(skipValidation);
153153

154154
// 缓存选项(使用 GetOrAdd 确保线程安全)
155-
var cachedOptions2 = OptionsCache.GetOrAdd(type, options);
155+
var finalOptions = OptionsCache.GetOrAdd(type, options);
156156

157157
// 如果是刚添加的,使用新构建的 options;如果是其他线程已添加的,使用缓存的
158-
var result = ReferenceEquals(cachedOptions2, options) ? options : (T)cachedOptions2;
158+
var result = ReferenceEquals(finalOptions, options) ? options : (T)finalOptions;
159159

160160
// 如果启用调试输出,打印解析后的选项对象
161161
if (shouldDebug)
@@ -221,8 +221,10 @@ public static void RemoveFromCache<T>() where T : class
221221
/// </summary>
222222
/// <typeparam name="T">选项类型</typeparam>
223223
/// <param name="options">选项对象</param>
224+
/// <exception cref="ArgumentNullException">当 options 为 null 时抛出</exception>
224225
public static void PrintOptionsInfo<T>(T options) where T : class
225226
{
227+
ArgumentNullException.ThrowIfNull(options);
226228
OptionsDebugger.PrintParsedOptions(options);
227229
}
228230

0 commit comments

Comments
 (0)