Files

94 lines
3.0 KiB
C#

using DbusSmsForward.Helper;
using DbusSmsForward.Models;
namespace DbusSmsForward.Services;
public static class CommandLineParser
{
public static AppCommand Parse(string[] args)
{
if (args.Length == 0)
{
return new AppCommand { Name = "run" };
}
List<string> enabledChannels = [];
string commandName = "run";
string phoneNumber = string.Empty;
string message = string.Empty;
bool validateConfig = false;
bool printSampleConfig = false;
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg)
{
case "run":
case "send":
case "validate-config":
case "print-sample-config":
commandName = arg;
break;
case "--to":
if (i + 1 < args.Length) phoneNumber = args[++i];
break;
case "--text":
if (i + 1 < args.Length) message = args[++i];
break;
case "-fE":
commandName = "run";
enabledChannels.Add("email");
break;
case "-fP":
commandName = "run";
enabledChannels.Add("pushplus");
break;
case "-fW":
commandName = "run";
enabledChannels.Add("wecom");
break;
case "-fT":
commandName = "run";
enabledChannels.Add("telegram");
break;
case "-fD":
commandName = "run";
enabledChannels.Add("dingtalk");
break;
case "-fB":
commandName = "run";
enabledChannels.Add("bark");
break;
case "-fS":
commandName = "run";
enabledChannels.Add("shell");
break;
case "-fA":
commandName = "run";
enabledChannels.Add("autmanpush");
break;
case "-sS":
commandName = "send";
break;
case "--validate-config":
validateConfig = true;
break;
case "--print-sample-config":
printSampleConfig = true;
break;
}
}
return new AppCommand
{
Name = commandName,
PhoneNumber = phoneNumber,
Message = message,
EnabledChannels = enabledChannels.Distinct(StringComparer.OrdinalIgnoreCase).ToArray(),
ValidateConfig = validateConfig || commandName == "validate-config",
PrintSampleConfig = printSampleConfig || commandName == "print-sample-config"
};
}
}