Use System.CommandLine for entry point parsing

This commit is contained in:
Huo Yaoyuan committed 2026-11-07 16:43:27 +08:00
1 parent 32209999ca
commit e5ba59e751
3 files changed
+66 -28

No files matched your search

@@ -12,8 +12,13 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<RootNamespace>DeepSeek.Cua.Windows</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="2.0.14" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../protocol/CuaProtocol.csproj" />
</ItemGroup>
@@ -1,35 +1,59 @@
// The executable is a private helper launched by the dsh Windows provider.
using System.CommandLine;
using DeepSeek.Cua.Protocol;
using DeepSeek.Cua.Windows;
namespace DeepSeek.Cua.Windows;
internal static class Program
try
{
private static int Main(string[] args)
#if NETFRAMEWORK
if (Environment.OSVersion.Version.Build < 19041)
{
try
{
if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 19041)) throw new NativeFailure("UNSUPPORTED", "Windows CUA requires Windows 10 build 19041 or later");
var server = args.Length == 7 && args[0] == "--shared-server";
var bridge = args.Length == 7 && args[0] == "--bridge";
var connector = args.Length == 9 && args[0] == "--serve" && args[1] == "--instance-id" && Guid.TryParseExact(args[2], "N", out _);
var offset = server || bridge ? 1 : 3;
if ((!server && !connector && !bridge) || args[offset] != "--runtime-key" || args[offset + 2] != "--connect-timeout-ms"
|| args[offset + 4] != "--grace-ms"
|| !int.TryParse(args[offset + 3], out var connectTimeoutMs) || connectTimeoutMs <= 0
|| !int.TryParse(args[offset + 5], out var graceMs) || graceMs <= 0)
throw new NativeFailure("INVALID_ARGUMENT", "The helper must be launched by the dsh Windows provider");
var runtime = Applications.Digest(Environment.ProcessPath!);
if (runtime != args[offset + 1]) throw new NativeFailure("NATIVE_STARTUP", "The helper executable changed before startup");
if (server) SharedRuntime.Serve(runtime, connectTimeoutMs, graceMs);
else if (bridge) SharedRuntime.Bridge(runtime, connectTimeoutMs).GetAwaiter().GetResult();
else SharedRuntime.Connect(args[2], runtime, connectTimeoutMs, graceMs).GetAwaiter().GetResult();
return 0;
}
catch (Exception error)
{
Console.Error.WriteLine(error is NativeFailure failure ? $"{failure.Code}: {failure.Message}" : $"NATIVE_STARTUP: {error.Message}");
return 1;
}
throw new NativeFailure("UNSUPPORTED", "Windows CUA requires Windows 10 build 19041 or later");
}
#endif
var serve = new Option<bool>("--serve") { Arity = ArgumentArity.Zero };
var sharedServer = new Option<bool>("--shared-server") { Arity = ArgumentArity.Zero };
var bridge = new Option<bool>("--bridge") { Arity = ArgumentArity.Zero };
var instanceId = new Option<Guid>("--instance-id");
var runtimeKey = new Option<string>("--runtime-key") { Required = true };
var connectTimeoutMs = new Option<int>("--connect-timeout-ms") { Required = true };
var graceMs = new Option<int>("--grace-ms") ;
var rootCommand = new RootCommand
{
Options =
{
serve,
sharedServer,
bridge,
instanceId,
runtimeKey,
connectTimeoutMs,
graceMs,
}
};
var parseResult = rootCommand.Parse(args);
if (parseResult.Errors.Count != 0)
throw new NativeFailure("INVALID_ARGUMENT", "The helper must be launched by the dsh Windows provider");
var runtime = Applications.Digest(Environment.ProcessPath!);
if (runtime != parseResult.GetValue(runtimeKey)) throw new NativeFailure("NATIVE_STARTUP", "The helper executable changed before startup");
if (parseResult.GetValue(sharedServer))
SharedRuntime.Serve(runtime, parseResult.GetRequiredValue(connectTimeoutMs), parseResult.GetRequiredValue(graceMs));
else if (parseResult.GetValue(bridge))
await SharedRuntime.Bridge(runtime, parseResult.GetRequiredValue(connectTimeoutMs));
else if (parseResult.GetValue(serve))
await SharedRuntime.Connect(parseResult.GetRequiredValue(instanceId).ToString("N"), runtime, parseResult.GetRequiredValue(connectTimeoutMs), parseResult.GetRequiredValue(graceMs));
else
throw new NativeFailure("INVALID_ARGUMENT", "The helper must be launched by the dsh Windows provider");
return 0;
}
catch (Exception error)
{
Console.Error.WriteLine(error is NativeFailure failure ? $"{failure.Code}: {failure.Message}" : $"NATIVE_STARTUP: {error.Message}");
return -1;
}
@@ -17,6 +17,15 @@
"resolved": "1.15.0",
"contentHash": "FbU0El+EEjdpuIX4iDbeS7ki1uzpJPx8vbqOzEtqnl1GZeAGJfq+jCbxeJL2y0EPnUNk8dRnnqR2xnYXg9Tf+g=="
},
"System.CommandLine": {
"type": "Direct",
"requested": "[2.0.14, )",
"resolved": "2.0.14",
"contentHash": "15fyVO4aG6poR6IKmhM74zf7PUzaU6FZAGm2d3SQSO9yIyGAeo44e6t9qeKjjaqyMEqYdqcb2G1B0gpLoDU2cA==",
"dependencies": {
"System.Memory": "4.5.5"
}
},
"System.Text.Json": {
"type": "Direct",
"requested": "[10.0.14, 10.0.14]",