Skip to content

Commit 70493cb

Browse files
committed
Minor adjustments.
1 parent 96e8b03 commit 70493cb

File tree

7 files changed

+30
-20
lines changed

7 files changed

+30
-20
lines changed

Templates/PowerCommands.zip

44 Bytes
Binary file not shown.

Templates/src/Core/PainKiller.PowerCommands.Core/EXTENSIONS/CommandLineInputInterpreterExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static string GetOptionValue(this ICommandLineInput input, string[] optio
6464
private static string GetOptionValue(string[] options, string optionName, string raw)
6565
{
6666
var quotes = Regex.Matches(raw, "\\\"(.*?)\\\"").ToStringArray();
67-
var option = options.FirstOrDefault(f => f == $"--{optionName.ToLower()}");
67+
var option = options.FirstOrDefault(f => f.ToLower() == $"--{optionName.ToLower()}");
6868
if (IsNullOrEmpty(option)) return "";
6969

7070
var firstQuotedOptionValueIfAny = FindFirstQuotedOptionValueIfAny(raw, quotes, optionName);

Templates/src/Core/PainKiller.PowerCommands.Core/MANAGERS/InputValidationManager.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace $safeprojectname$.Managers;
1+
using System.Text;
2+
3+
namespace $safeprojectname$.Managers;
24
public class InputValidationManager
35
{
46
private readonly ICommandLineInput _input;
@@ -13,17 +15,18 @@ public InputValidationManager(IConsoleCommand command, ICommandLineInput input,
1315
public InputValidationResult ValidateAndInitialize()
1416
{
1517
var retVal = new InputValidationResult();
18+
var messages = new StringBuilder();
1619
var attribute = _command.GetPowerCommandAttribute();
1720
var requiredArguments = attribute.Arguments.Split('|').Where(a => a.StartsWith('!')).ToArray();
1821
if (_input.Arguments.Length < requiredArguments.Length)
1922
{
20-
_logger.Invoke($"Missing argument(s), required arguments is {requiredArguments.Length}");
23+
messages.AppendLine($"Missing argument(s), required arguments is {requiredArguments.Length}");
2124
retVal.HasValidationError = true;
2225
}
2326
var requiredQuotes = attribute.Quotes.Split('|').Where(q => q.StartsWith('!')).ToArray();
2427
if (_input.Quotes.Length < requiredQuotes.Length)
2528
{
26-
_logger.Invoke($"Missing quote(s), required quotes is {requiredQuotes.Length}");
29+
messages.AppendLine($"Missing quote(s), required quotes is {requiredQuotes.Length}");
2730
retVal.HasValidationError = true;
2831
}
2932

@@ -35,23 +38,25 @@ public InputValidationResult ValidateAndInitialize()
3538
if (optionInfo.IsMandatory && !_input.HasOption(optionInfo.Name))
3639
{
3740
retVal.HasValidationError = true;
38-
_logger.Invoke($"Option [{optionInfo.Name}] is mandatory");
41+
messages.AppendLine($"Option [{optionInfo.Name}] is mandatory.");
3942
}
4043
optionInfo.Value = _input.GetOptionValue(optionInfo.Name);
41-
if (!string.IsNullOrEmpty(optionInfo.Value) || !_input.HasOption(optionInfo.Name) || !optionInfo.ValueIsRequired) continue;
42-
_logger.Invoke($"Option [{optionInfo.Name}] is required to have a value or not used at all.");
44+
if (!string.IsNullOrEmpty(optionInfo.Value) || (!_input.HasOption(optionInfo.Name) && !optionInfo.IsMandatory)) continue;
45+
messages.AppendLine($"Option [{optionInfo.Name}] is required to have a value.");
4346
retVal.HasValidationError = true;
4447
}
45-
48+
_logger.Invoke(messages.ToString());
4649
if (string.IsNullOrEmpty(attribute.Secrets)) return retVal;
50+
4751
var requiredSecrets = attribute.Secrets.Split('|').Where(s => s.StartsWith('!')).ToArray();
4852
foreach (var secretName in requiredSecrets)
4953
{
5054
var secret = IPowerCommandServices.DefaultInstance!.Configuration.Secret.Secrets.FirstOrDefault(s => s.Name.ToLower() == secretName.Replace("!","").ToLower());
5155
if (secret != null) continue;
52-
_logger.Invoke($"Secret [{secretName.Replace("!","")}] is required");
56+
messages.AppendLine($"Secret [{secretName.Replace("!","")}] is required");
5357
retVal.HasValidationError = true;
5458
}
59+
_logger.Invoke(messages.ToString());
5560
return retVal;
5661
}
5762
}

Templates/src/PainKiller.PowerCommands.MyExampleCommands/COMMANDS/DemoCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace $safeprojectname$.Commands;
22

33
[PowerCommandTest(tests: "! |!--pause 3")]
44
[PowerCommandDesign( description: "Demo command just to try out how you could use the input, do not forget the MANDATORY option, will trigger a validation error otherwise! ;-)\n That is because the option name is typed with UPPERCASE letters, useful when you want a mandatory option\n The pause option on the other hand starts with a ! symbol meaning that if you add the --pause option you must also give it a value, an integer in this case.",
5-
options: "MANDATORY|!pause",
5+
options: "!MANDATORY|!pause",
66
example: "//Must provide the MANDATORY option will trigger a validation error otherwise|demo MANDATORY|//Test the pause service|demo --pause 5 MANDATORY")]
77
public class DemoCommand : CommandBase<PowerCommandsConfiguration>
88
{

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Core/Extensions/CommandLineInputInterpreterExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static string GetOptionValue(this ICommandLineInput input, string[] optio
6464
private static string GetOptionValue(string[] options, string optionName, string raw)
6565
{
6666
var quotes = Regex.Matches(raw, "\\\"(.*?)\\\"").ToStringArray();
67-
var option = options.FirstOrDefault(f => f == $"--{optionName.ToLower()}");
67+
var option = options.FirstOrDefault(f => f.ToLower() == $"--{optionName.ToLower()}");
6868
if (IsNullOrEmpty(option)) return "";
6969

7070
var firstQuotedOptionValueIfAny = FindFirstQuotedOptionValueIfAny(raw, quotes, optionName);

src/PainKiller.PowerCommands/Core/PainKiller.PowerCommands.Core/Managers/InputValidationManager.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace PainKiller.PowerCommands.Core.Managers;
1+
using System.Text;
2+
3+
namespace PainKiller.PowerCommands.Core.Managers;
24
public class InputValidationManager
35
{
46
private readonly ICommandLineInput _input;
@@ -13,17 +15,18 @@ public InputValidationManager(IConsoleCommand command, ICommandLineInput input,
1315
public InputValidationResult ValidateAndInitialize()
1416
{
1517
var retVal = new InputValidationResult();
18+
var messages = new StringBuilder();
1619
var attribute = _command.GetPowerCommandAttribute();
1720
var requiredArguments = attribute.Arguments.Split('|').Where(a => a.StartsWith('!')).ToArray();
1821
if (_input.Arguments.Length < requiredArguments.Length)
1922
{
20-
_logger.Invoke($"Missing argument(s), required arguments is {requiredArguments.Length}");
23+
messages.AppendLine($"Missing argument(s), required arguments is {requiredArguments.Length}");
2124
retVal.HasValidationError = true;
2225
}
2326
var requiredQuotes = attribute.Quotes.Split('|').Where(q => q.StartsWith('!')).ToArray();
2427
if (_input.Quotes.Length < requiredQuotes.Length)
2528
{
26-
_logger.Invoke($"Missing quote(s), required quotes is {requiredQuotes.Length}");
29+
messages.AppendLine($"Missing quote(s), required quotes is {requiredQuotes.Length}");
2730
retVal.HasValidationError = true;
2831
}
2932

@@ -35,23 +38,25 @@ public InputValidationResult ValidateAndInitialize()
3538
if (optionInfo.IsMandatory && !_input.HasOption(optionInfo.Name))
3639
{
3740
retVal.HasValidationError = true;
38-
_logger.Invoke($"Option [{optionInfo.Name}] is mandatory");
41+
messages.AppendLine($"Option [{optionInfo.Name}] is mandatory.");
3942
}
4043
optionInfo.Value = _input.GetOptionValue(optionInfo.Name);
41-
if (!string.IsNullOrEmpty(optionInfo.Value) || !_input.HasOption(optionInfo.Name) || !optionInfo.ValueIsRequired) continue;
42-
_logger.Invoke($"Option [{optionInfo.Name}] is required to have a value or not used at all.");
44+
if (!string.IsNullOrEmpty(optionInfo.Value) || (!_input.HasOption(optionInfo.Name) && !optionInfo.IsMandatory)) continue;
45+
messages.AppendLine($"Option [{optionInfo.Name}] is required to have a value.");
4346
retVal.HasValidationError = true;
4447
}
45-
48+
_logger.Invoke(messages.ToString());
4649
if (string.IsNullOrEmpty(attribute.Secrets)) return retVal;
50+
4751
var requiredSecrets = attribute.Secrets.Split('|').Where(s => s.StartsWith('!')).ToArray();
4852
foreach (var secretName in requiredSecrets)
4953
{
5054
var secret = IPowerCommandServices.DefaultInstance!.Configuration.Secret.Secrets.FirstOrDefault(s => s.Name.ToLower() == secretName.Replace("!","").ToLower());
5155
if (secret != null) continue;
52-
_logger.Invoke($"Secret [{secretName.Replace("!","")}] is required");
56+
messages.AppendLine($"Secret [{secretName.Replace("!","")}] is required");
5357
retVal.HasValidationError = true;
5458
}
59+
_logger.Invoke(messages.ToString());
5560
return retVal;
5661
}
5762
}

src/PainKiller.PowerCommands/PainKiller.PowerCommands.MyExampleCommands/Commands/DemoCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace PainKiller.PowerCommands.MyExampleCommands.Commands;
22

33
[PowerCommandTest(tests: "! |!--pause 3")]
44
[PowerCommandDesign( description: "Demo command just to try out how you could use the input, do not forget the MANDATORY option, will trigger a validation error otherwise! ;-)\n That is because the option name is typed with UPPERCASE letters, useful when you want a mandatory option\n The pause option on the other hand starts with a ! symbol meaning that if you add the --pause option you must also give it a value, an integer in this case.",
5-
options: "MANDATORY|!pause",
5+
options: "!MANDATORY|!pause",
66
example: "//Must provide the MANDATORY option will trigger a validation error otherwise|demo MANDATORY|//Test the pause service|demo --pause 5 MANDATORY")]
77
public class DemoCommand : CommandBase<PowerCommandsConfiguration>
88
{

0 commit comments

Comments
 (0)