Skip to content
This repository was archived by the owner on Jan 25, 2026. It is now read-only.

Commit 39db188

Browse files
committed
feat(ArgumentsBuilder): 新增参数构建器
1 parent bb2eede commit 39db188

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

PCL.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
<Compile Include="Service\TestService.cs" Condition="Exists('Service\TestService.cs')" />
197197
<Compile Include="Service\UpdateService.cs" />
198198
<Compile Include="Utils\AnyType.cs" />
199+
<Compile Include="Utils\ArgumentsBuilder.cs" />
199200
<Compile Include="Utils\AtomicVariable.cs" />
200201
<Compile Include="Utils\Download\Downloader.cs" />
201202
<Compile Include="Utils\Download\DownloadItem.cs" />

Utils/ArgumentsBuilder.cs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Collections.Generic;
2+
using System.Text;
3+
4+
namespace PCL.Core.Utils;
5+
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
10+
public class ArgumentsBuilder
11+
{
12+
private readonly Dictionary<string, string> _args = new();
13+
14+
/// <summary>
15+
/// 添加键值对参数(自动处理空格转义)
16+
/// </summary>
17+
/// <param name="key">参数名(不带前缀)</param>
18+
/// <param name="value">参数值</param>
19+
public ArgumentsBuilder Add(string key, string value)
20+
{
21+
_args[key] = _handleEscapeValue(value);
22+
return this;
23+
}
24+
25+
/// <summary>
26+
/// 添加标志参数(无值参数)
27+
/// </summary>
28+
/// <param name="flag">标志名(不带前缀)</param>
29+
public ArgumentsBuilder AddFlag(string flag)
30+
{
31+
_args[flag] = null;
32+
return this;
33+
}
34+
35+
/// <summary>
36+
/// 条件添加参数(仅当condition为true时添加)
37+
/// </summary>
38+
public ArgumentsBuilder AddIf(bool condition, string key, string value)
39+
{
40+
if (condition) Add(key, value);
41+
return this;
42+
}
43+
44+
/// <summary>
45+
/// 条件添加标志(仅当condition为true时添加)
46+
/// </summary>
47+
public ArgumentsBuilder AddFlagIf(bool condition, string flag)
48+
{
49+
if (condition) AddFlag(flag);
50+
return this;
51+
}
52+
53+
/// <summary>
54+
/// 构建参数字符串
55+
/// </summary>
56+
/// <param name="prefixStyle">前缀样式:0=自动(单字符用-,多字符用--),1=强制单横线,2=强制双横线</param>
57+
public string Build(int prefixStyle = 0)
58+
{
59+
var sb = new StringBuilder();
60+
61+
foreach (var arg in _args)
62+
{
63+
if (sb.Length > 0) sb.Append(' ');
64+
65+
// 添加前缀
66+
switch (prefixStyle)
67+
{
68+
case 1: // 强制单横线
69+
sb.Append('-').Append(arg.Key);
70+
break;
71+
case 2: // 强制双横线
72+
sb.Append("--").Append(arg.Key);
73+
break;
74+
default: // 自动判断
75+
sb.Append(arg.Key.Length == 1 ? "-" : "--").Append(arg.Key);
76+
break;
77+
}
78+
79+
// 添加值(如果有)
80+
if (arg.Value != null)
81+
{
82+
sb.Append('=').Append(arg.Value);
83+
}
84+
}
85+
86+
return sb.ToString();
87+
}
88+
89+
public override string ToString()
90+
{
91+
return Build();
92+
}
93+
94+
/// <summary>
95+
/// 清空所有参数
96+
/// </summary>
97+
public void Clear() => _args.Clear();
98+
99+
// 转义包含空格的值(用双引号包裹)
100+
private static string _handleEscapeValue(string value)
101+
{
102+
if (string.IsNullOrEmpty(value)) return "\"\"";
103+
104+
return value.Contains(' ') || value.Contains('"')
105+
? $"\"{value.Replace("\"", "\\\"")}\"" // 处理双引号转义
106+
: value;
107+
}
108+
}

0 commit comments

Comments
 (0)