Add customize command

This commit is contained in:
NextTurn
2020-07-30 00:00:00 +08:00
committed by Next Turn
parent 7e644f7944
commit 2a576e102e
7 changed files with 352 additions and 14 deletions

View File

@@ -34,6 +34,21 @@ namespace WinSW
private static readonly ILog Log = LogManager.GetLogger(typeof(Program));
internal static Action<Exception, InvocationContext>? TestExceptionHandler;
internal static string? TestExecutablePath;
private static string ExecutablePath
{
get
{
if (TestExecutablePath != null)
{
return TestExecutablePath;
}
using Process current = Process.GetCurrentProcess();
return current.MainModule.FileName;
}
}
internal static int Main(string[] args)
{
@@ -205,12 +220,12 @@ namespace WinSW
test.Add(config);
test.Add(noElevate);
const int minTimeout = -1;
const int maxTimeout = int.MaxValue / 1000;
var timeout = new Option<int>("--timeout", "Specifies the number of seconds to wait before the service is stopped.");
timeout.Argument.AddValidator(argument =>
{
const int minTimeout = -1;
const int maxTimeout = int.MaxValue / 1000;
string token = argument.Tokens.Single().Value;
return !int.TryParse(token, out int value) ? null :
value < minTimeout ? $"Argument '{token}' must be greater than or equal to {minTimeout}." :
@@ -236,6 +251,39 @@ namespace WinSW
root.Add(refresh);
}
{
var customize = new Command("customize")
{
Handler = CommandHandler.Create<string, string>(Customize),
};
customize.Add(new Option<string>(new[] { "--output", "-o" })
{
Required = true,
});
var manufacturer = new Option<string>("--manufacturer")
{
Required = true,
};
manufacturer.Argument.AddValidator(argument =>
{
const int minLength = 12;
const int maxLength = 15;
string token = argument.Tokens.Single().Value;
int length = token.Length;
return
length < minLength ? $"The length of argument '{token}' must be greater than or equal to {minLength}." :
length > maxLength ? $"The length of argument '{token}' must be less than or equal to {maxLength}." :
null;
});
customize.Add(manufacturer);
root.Add(customize);
}
{
var dev = new Command("dev", "Experimental commands.")
{
@@ -878,6 +926,18 @@ namespace WinSW
}
}
static void Customize(string output, string manufacturer)
{
if (Resources.UpdateCompanyName(ExecutablePath, output, manufacturer))
{
Console.WriteLine("The operation succeeded.");
}
else
{
Console.Error.WriteLine("The operation failed.");
}
}
// [DoesNotReturn]
static void Elevate(bool noElevate)
{