From 32694e92b75d049bf9dd2bc32e1c1ca3dabf35b4 Mon Sep 17 00:00:00 2001 From: TautCony Date: Fri, 22 Dec 2023 23:15:07 +0800 Subject: [PATCH] feat: upgrade System.CommandLine --- OKP.Core/OKP.Core.csproj | 7 +- OKP.Core/Program.cs | 305 ++++++++++++++++++--------------------- nuget.config | 6 + 3 files changed, 151 insertions(+), 167 deletions(-) create mode 100644 nuget.config diff --git a/OKP.Core/OKP.Core.csproj b/OKP.Core/OKP.Core.csproj index dc90100..d3bd399 100644 --- a/OKP.Core/OKP.Core.csproj +++ b/OKP.Core/OKP.Core.csproj @@ -5,7 +5,7 @@ net8.0 enable enable - true + true 1.0.2-beta Copyright (C) 2023 AmusementClub Released under the GNU GPLv3+. @@ -18,8 +18,9 @@ Released under the GNU GPLv3+. - - + + + diff --git a/OKP.Core/Program.cs b/OKP.Core/Program.cs index 783d2f1..2f2940f 100644 --- a/OKP.Core/Program.cs +++ b/OKP.Core/Program.cs @@ -1,7 +1,4 @@ using System.CommandLine; -using System.CommandLine.Builder; -using System.CommandLine.Help; -using System.CommandLine.Parsing; using OKP.Core.Interface; using OKP.Core.Interface.Acgnx; using OKP.Core.Interface.Acgrip; @@ -12,7 +9,6 @@ using Serilog; using Serilog.Core; using Serilog.Events; -using Spectre.Console; using Constants = OKP.Core.Utils.Constants; namespace OKP.Core @@ -22,49 +18,46 @@ internal class Program { public static void Main(string[] args) { - var torrentArgument = new Argument?>( - name: "torrent", - description: "Torrents to be published. (Or Cookie file exported by Get Cookies.txt.)" - ); + var torrentArgument = new CliArgument?>("torrent") + { + Description = "Torrents to be published. (Or Cookie file exported by Get Cookies.txt.)" + }; - var cookiesOption = new Option( - name: "--cookies", - getDefaultValue: () => null, - description: "Cookie file to be used." - ); + var cookiesOption = new CliOption("--cookies") + { + DefaultValueFactory = _ => null, + Description = "Cookie file to be used." + }; - var settingOption = new Option( - name: "--setting", - getDefaultValue: () => Constants.DefaultSettingFileName, - description: "(Not required) Specific setting file." - ); - settingOption.AddAlias("-s"); + var settingOption = new CliOption("--setting", "-s") + { + DefaultValueFactory = _ => Constants.DefaultSettingFileName, + Description = "(Not required) Specific setting file." + }; - var logLevelOption = new Option( - name: "--log_level", - getDefaultValue: () => "Debug", - description: "Log level." - ); - logLevelOption.AddAlias("-l"); + var logLevelOption = new CliOption("--log_level", "-l") + { + DefaultValueFactory = _ => "Debug", + Description = "Log level." + }; - var logFileOption = new Option( - name: "--log_file", - getDefaultValue: () => Constants.DefaultLogFileName, - description: "Log file." - ); + var logFileOption = new CliOption("--log_file") + { + DefaultValueFactory = _ => Constants.DefaultLogFileName, + Description = "Log file." + }; - var noReactionOption = new Option( - name: "--no_reaction", - description: "Skip reaction." - ); - noReactionOption.AddAlias("-y"); + var noReactionOption = new CliOption("--no_reaction", "-y") + { + Description = "Skip reaction." + }; - var allowSkipOption = new Option( - name: "--allow_skip", - description: "Ignore login fail and continue publishing." - ); + var allowSkipOption = new CliOption("--allow_skip") + { + Description = "Ignore login fail and continue publishing." + }; - var rootCommand = new RootCommand("One Key Publish") + var rootCommand = new CliRootCommand("One Key Publish") { torrentArgument, cookiesOption, @@ -75,145 +68,129 @@ public static void Main(string[] args) allowSkipOption, }; - rootCommand.SetHandler(( - torrentFile, - cookies, - settingFile, - logLevel, - logFile, - noReaction, - allowSkip) => + rootCommand.SetAction((result, _) => + { + var torrentFile = result.GetValue(torrentArgument); + var cookies = result.GetValue(cookiesOption); + var settingFile = result.GetValue(settingOption); + var logLevel = result.GetValue(logLevelOption); + var logFile = result.GetValue(logFileOption); + var noReaction = result.GetValue(noReactionOption); + var allowSkip = result.GetValue(allowSkipOption); + + ActionHandler(torrentFile, cookies , settingFile, logLevel, logFile, noReaction, allowSkip); + return Task.CompletedTask; + }); + + rootCommand.Parse(args).Invoke(); + IOHelper.ReadLine(); + } + + private static void ActionHandler(IEnumerable? torrentFile, string? cookies, string? settingFile, string? logLevel, string logFile, bool noReaction, bool allowSkip) + { + var levelSwitch = new LoggingLevelSwitch + { + MinimumLevel = logLevel?.ToLower() switch { - var levelSwitch = new LoggingLevelSwitch - { - MinimumLevel = logLevel?.ToLower() switch - { - "verbose" => LogEventLevel.Verbose, - "debug" => LogEventLevel.Debug, - "info" => LogEventLevel.Information, - _ => LogEventLevel.Debug - } - }; - Log.Logger = new LoggerConfiguration() - .MinimumLevel.ControlledBy(levelSwitch) - .WriteTo.Console() - .WriteTo.File(logFile, - rollingInterval: RollingInterval.Month, - rollOnFileSizeLimit: true) - .CreateLogger(); - IOHelper.NoReaction = noReaction; - if (torrentFile is null) - { - Log.Fatal("o.TorrentFile is null"); - return; - } - var addCookieCount = 0; - foreach (var file in torrentFile) - { - if (!File.Exists(file)) - { - Log.Error("文件{File}不存在", file); - continue; - } - var extension = (Path.GetExtension(file) ?? "").ToLower(); + "verbose" => LogEventLevel.Verbose, + "debug" => LogEventLevel.Debug, + "info" => LogEventLevel.Information, + _ => LogEventLevel.Debug + } + }; + Log.Logger = new LoggerConfiguration() + .MinimumLevel.ControlledBy(levelSwitch) + .WriteTo.Console() + .WriteTo.File(logFile, + rollingInterval: RollingInterval.Month, + rollOnFileSizeLimit: true) + .CreateLogger(); + IOHelper.NoReaction = noReaction; + if (torrentFile is null) + { + Log.Fatal("o.TorrentFile is null"); + return; + } + var addCookieCount = 0; + foreach (var file in torrentFile) + { + if (!File.Exists(file)) + { + Log.Error("文件{File}不存在", file); + continue; + } + var extension = (Path.GetExtension(file) ?? "").ToLower(); - if (extension == ".torrent") + if (extension == ".torrent") + { + Log.Information("正在发布 {File}", file); + SinglePublish(file, settingFile, cookies, allowSkip); + continue; + } + if (extension == ".txt") + { + if (cookies is null) + { + Log.Information("请输入Cookie文件名,不需要包含扩展名,相对目录默认为{DefaultPath}", + IOHelper.BasePath(Constants.DefaultCookiePath)); + IOHelper.HintText(Constants.DefaultCookieFile); + var filename = IOHelper.ReadLine(); + if (File.Exists(filename)) { - Log.Information("正在发布 {File}", file); - SinglePublish(file, settingFile, cookies, allowSkip); - continue; + cookies = filename; + Log.Error("你指定的Cookie文件{File}已经存在!继续添加可能会覆盖之前保存的Cookie!", cookies); + IOHelper.ReadLine(); + HttpHelper.GlobalCookieContainer.LoadFromTxt(cookies); } - if (extension == ".txt") + else { - if (cookies is null) + if (!Directory.Exists(IOHelper.BasePath(Constants.DefaultCookiePath))) { - Log.Information("请输入Cookie文件名,不需要包含扩展名,相对目录默认为{DefaultPath}", - IOHelper.BasePath(Constants.DefaultCookiePath)); - IOHelper.HintText(Constants.DefaultCookieFile); - var filename = IOHelper.ReadLine(); - if (File.Exists(filename)) - { - cookies = filename; - Log.Error("你指定的Cookie文件{File}已经存在!继续添加可能会覆盖之前保存的Cookie!", cookies); - IOHelper.ReadLine(); - HttpHelper.GlobalCookieContainer.LoadFromTxt(cookies); - } - else - { - if (!Directory.Exists(IOHelper.BasePath(Constants.DefaultCookiePath))) - { - Directory.CreateDirectory(IOHelper.BasePath(Constants.DefaultCookiePath)); - } - cookies = IOHelper.BasePath(Constants.DefaultCookiePath, - (filename?.Length == 0 ? Constants.DefaultCookieFile : filename) + ".txt"); - if (File.Exists(cookies)) - { - Log.Error("你指定的Cookie文件{File}已经存在!继续添加可能会覆盖之前保存的Cookie!", cookies); - IOHelper.ReadLine(); - HttpHelper.GlobalCookieContainer.LoadFromTxt(cookies); - } - } - Log.Information("请输入你使用的浏览器UserAgent:"); - var ua = IOHelper.ReadLine(); - while (ua is null || !HttpHelper.UaRegex.IsMatch(ua)) - { - Log.Information("你必须输入一个合法的UserAgent以确保你的cookie可以正常使用:"); - ua = IOHelper.ReadLine(); - } - HttpHelper.GlobalUserAgent = ua; + Directory.CreateDirectory(IOHelper.BasePath(Constants.DefaultCookiePath)); } - if (File.Exists(IOHelper.BasePath(Constants.DefaultCookiePath, - Constants.DefaultCookieFile + ".txt"))) + cookies = IOHelper.BasePath(Constants.DefaultCookiePath, + (filename?.Length == 0 ? Constants.DefaultCookieFile : filename) + ".txt"); + if (File.Exists(cookies)) { - HttpHelper.GlobalCookieContainer.LoadFromTxt(IOHelper.BasePath(Constants.DefaultCookiePath, - Constants.DefaultCookieFile + ".txt")); + Log.Error("你指定的Cookie文件{File}已经存在!继续添加可能会覆盖之前保存的Cookie!", cookies); + IOHelper.ReadLine(); + HttpHelper.GlobalCookieContainer.LoadFromTxt(cookies); } - Log.Information("正在添加Cookie文件{File}", file); - AddCookies(file); - addCookieCount++; - Log.Information("Cookie文件{File}添加完成,按回车键继续添加", file); - IOHelper.ReadLine(); } - else + Log.Information("请输入你使用的浏览器UserAgent:"); + var ua = IOHelper.ReadLine(); + while (ua is null || !HttpHelper.UaRegex.IsMatch(ua)) { - Log.Error("不受支持的文件格式{File}", file); - } - if (cookies is not null) - { - Log.Information("共输入了{Count}个Cookie文件", addCookieCount); - HttpHelper.GlobalCookieContainer.SaveToTxt(cookies, HttpHelper.GlobalUserAgent); - Log.Information("保存成功,Cookie文件保存在{Path}", cookies); + Log.Information("你必须输入一个合法的UserAgent以确保你的cookie可以正常使用:"); + ua = IOHelper.ReadLine(); } + HttpHelper.GlobalUserAgent = ua; } - }, - torrentArgument, - cookiesOption, - settingOption, - logLevelOption, - logFileOption, - noReactionOption, - allowSkipOption); - - var parser = new CommandLineBuilder(rootCommand) - .UseDefaults() - .UseHelp(ctx => + if (File.Exists(IOHelper.BasePath(Constants.DefaultCookiePath, + Constants.DefaultCookieFile + ".txt"))) + { + HttpHelper.GlobalCookieContainer.LoadFromTxt(IOHelper.BasePath(Constants.DefaultCookiePath, + Constants.DefaultCookieFile + ".txt")); + } + Log.Information("正在添加Cookie文件{File}", file); + AddCookies(file); + addCookieCount++; + Log.Information("Cookie文件{File}添加完成,按回车键继续添加", file); + IOHelper.ReadLine(); + } + else { - ctx.HelpBuilder.CustomizeLayout( - _ => - HelpBuilder.Default - .GetLayout() - .Skip(1) // Skip the default command description section. - .Prepend(hc => hc.Output.Write("Copyright (C) 2023 AmusementClub\nReleased under the GNU GPLv3+.\n")) - .Prepend( - _ => AnsiConsole.Write( - new FigletText(rootCommand.Description!)) - )); - }) - .Build(); - - parser.Invoke(args); - IOHelper.ReadLine(); + Log.Error("不受支持的文件格式{File}", file); + } + if (cookies is not null) + { + Log.Information("共输入了{Count}个Cookie文件", addCookieCount); + HttpHelper.GlobalCookieContainer.SaveToTxt(cookies, HttpHelper.GlobalUserAgent); + Log.Information("保存成功,Cookie文件保存在{Path}", cookies); + } + } } + private static void SinglePublish(string file, string settingFile, string? cookies,bool allowSkip) { if (!File.Exists(file)) diff --git a/nuget.config b/nuget.config new file mode 100644 index 0000000..63bc119 --- /dev/null +++ b/nuget.config @@ -0,0 +1,6 @@ + + + + + +