-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathCommandLineProcessor.cs
More file actions
77 lines (64 loc) · 2.3 KB
/
CommandLineProcessor.cs
File metadata and controls
77 lines (64 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
namespace Simplify.WindowsServices.CommandLine;
/// <summary>
/// Provides windows-service command line processor
/// </summary>
/// <seealso cref="Simplify.WindowsServices.CommandLine.ICommandLineProcessor" />
public class CommandLineProcessor : ICommandLineProcessor
{
private IInstallationController? _installationController;
/// <summary>
/// Gets or sets the current installation controller.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
public IInstallationController InstallationController
{
get => _installationController ??= new InstallationController();
set => _installationController = value ?? throw new ArgumentNullException(nameof(value));
}
/// <summary>
/// Processes the command line arguments.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns></returns>
public virtual ProcessCommandLineResult ProcessCommandLineArguments(string[]? args)
{
if (args == null || args.Length == 0)
return ProcessCommandLineResult.NoArguments;
var action = ParseCommandLineArguments(args);
switch (action)
{
case CommandLineAction.InstallService:
InstallationController.InstallService();
return ProcessCommandLineResult.CommandLineActionExecuted;
case CommandLineAction.UninstallService:
InstallationController.UninstallService();
return ProcessCommandLineResult.CommandLineActionExecuted;
case CommandLineAction.RunAsConsole:
return ProcessCommandLineResult.SkipServiceStart;
case CommandLineAction.UndefinedAction:
break;
default:
throw new ArgumentOutOfRangeException();
}
Console.WriteLine($"Undefined service parameters: '{string.Concat(args)}'");
Console.WriteLine("To install service use 'install' command");
Console.WriteLine("To uninstall service use 'uninstall' command");
return ProcessCommandLineResult.UndefinedParameters;
}
/// <summary>
/// Parses the command line arguments.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns></returns>
public virtual CommandLineAction ParseCommandLineArguments(string[] args)
{
return args[0] switch
{
"install" => CommandLineAction.InstallService,
"uninstall" => CommandLineAction.UninstallService,
"console" => CommandLineAction.RunAsConsole,
_ => CommandLineAction.UndefinedAction
};
}
}