|
|
@@ -45,6 +45,14 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
|
|
|
protected AbstractCommand(Engine engine)
|
|
|
{
|
|
|
+ // ReSharper disable once VirtualMemberCallInConstructor
|
|
|
+ Notes = string.Empty;
|
|
|
+ // ReSharper disable once VirtualMemberCallInConstructor
|
|
|
+ Examples = string.Empty;
|
|
|
+ // ReSharper disable once VirtualMemberCallInConstructor
|
|
|
+ CustomUsage = string.Empty;
|
|
|
+
|
|
|
+
|
|
|
_engine = engine ?? throw new ArgumentNullException(nameof(engine));
|
|
|
_log = engine.Configuration.DefaultLoggerFactory?.GetLogger(GetType());
|
|
|
_arguments = new Dictionary<string, AbstractArgument>();
|
|
|
@@ -63,7 +71,7 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
#region *** Public operations ***
|
|
|
public Result Execute(string input)
|
|
|
{
|
|
|
- ConsoleWriter writer = null;
|
|
|
+ ConsoleWriter? writer = null;
|
|
|
try
|
|
|
{
|
|
|
writer = new ConsoleWriter(ConsoleColor.Green);
|
|
|
@@ -118,7 +126,7 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
var arg = args[i];
|
|
|
var index = i - 1;
|
|
|
WriteDebugInfo($"Parsing argument [index={index}] by value string '{arg}' in progress..");
|
|
|
- AbstractArgument argument = null;
|
|
|
+ AbstractArgument? argument = null;
|
|
|
if (arg.StartsWith(Engine.Configuration.CharacterNamedArgumentSeparator))
|
|
|
{
|
|
|
//Named
|
|
|
@@ -281,11 +289,11 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
using (var writerYellow = new ConsoleWriter(ConsoleColor.Magenta))
|
|
|
{
|
|
|
writer.WriteLine("Engine context:");
|
|
|
- writerBlue.Write(_engine.Context.ToString()).Write(" : ");
|
|
|
- writerYellow.WriteLine(_engine.Context.Status);
|
|
|
+ writerBlue.Write((_engine?.Context?.ToString() ?? String.Empty)).Write(" : ");
|
|
|
+ writerYellow.WriteLine((_engine?.Context?.Status ?? string.Empty));
|
|
|
writer.WriteLine("Command context:");
|
|
|
- writerBlue.Write(_context.ToString()).Write(" : ");
|
|
|
- writerYellow.WriteLine(_context.Status);
|
|
|
+ writerBlue.Write((_context?.ToString() ?? string.Empty)).Write(" : ");
|
|
|
+ writerYellow.WriteLine((_context?.Status ?? string.Empty));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -333,12 +341,11 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
{
|
|
|
return incommingResult;
|
|
|
}
|
|
|
-
|
|
|
- protected virtual void Log(LogSeverityEnum type, int code, string message, Exception e = null)
|
|
|
+ protected virtual void Log(LogSeverityEnum type, int code, string message, Exception? e = null)
|
|
|
{
|
|
|
_log?.Log(type, code, message,e);
|
|
|
}
|
|
|
- protected void Log(LogSeverityEnum type, string message, Exception e = null)
|
|
|
+ protected void Log(LogSeverityEnum type, string message, Exception? e = null)
|
|
|
{
|
|
|
Log(type,0, message,e);
|
|
|
}
|
|
|
@@ -358,19 +365,19 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
return _arguments.Values.Where(x => x.IsPositional()).Skip(ordinalPosition).FirstOrDefault();
|
|
|
}
|
|
|
|
|
|
- protected TValue GetArgumentValueOrDefault<TValue>(string code, TValue defalutValue)
|
|
|
+ protected TValue? GetArgumentValueOrDefault<TValue>(string code, TValue defalutValue)
|
|
|
{
|
|
|
if (!ContainsArgument(code)) return defalutValue;
|
|
|
- return (TValue) GetArgumentValueOrDefault<TValue>(code);
|
|
|
+ return (TValue?) GetArgumentValueOrDefault<TValue>(code);
|
|
|
}
|
|
|
- protected TValue GetArgumentValueOrDefault<TValue>(string code)
|
|
|
+ protected TValue? GetArgumentValueOrDefault<TValue>(string code)
|
|
|
{
|
|
|
if (!ContainsArgument(code)) throw new ArgumentOutOfRangeException(code, $"Argument '{code}' not defined.");
|
|
|
var arg = GetArgument(code);
|
|
|
var value = arg.Value.Get() ?? arg.DefaultValue.Get();
|
|
|
if (typeof(TValue).IsEnum)
|
|
|
- return (TValue)Enum.Parse(typeof(TValue), value?.ToString(),true);
|
|
|
- return (TValue) value;
|
|
|
+ return (TValue)Enum.Parse(typeof(TValue), value?.ToString() ?? string.Empty,true);
|
|
|
+ return (TValue?) value;
|
|
|
}
|
|
|
|
|
|
protected bool WasArgumentSpecified(string code)
|
|
|
@@ -383,6 +390,7 @@ namespace Quadarax.Foundation.Core.QConsole.Command.Base
|
|
|
protected void ValidateArgumentValueEnum(string argumentName, Type enumType)
|
|
|
{
|
|
|
var argVal = GetArgumentValueOrDefault<string>(argumentName);
|
|
|
+ if (argVal == null) throw new ArgumentException($"Cannot obtain enumeration value from argument '{argumentName}'. Value is empty.");
|
|
|
if (!Enum.GetNames(enumType).Select(x => x.ToLower()).Contains(argVal.ToLower()))
|
|
|
throw new ArgumentOutOfRangeException(
|
|
|
$"Argument '{argumentName}' value must be set as following: {string.Join(",", Enum.GetNames(enumType))}");
|