Files
SabreTools/SabreTools.Help/TopLevel.cs

153 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging;
2020-12-07 14:29:45 -08:00
2020-12-07 13:57:26 -08:00
namespace SabreTools.Help
{
/// <summary>
/// Represents an actionable top-level feature
/// </summary>
public abstract class TopLevel : Feature
{
#region Fields
/// <summary>
/// List of files, directories, and potential wildcard paths
/// </summary>
2024-07-18 00:32:41 -04:00
public readonly List<string> Inputs = [];
#endregion
#region Logging
/// <summary>
/// Logging object
/// </summary>
2025-01-08 16:59:44 -05:00
private readonly Logger _logger;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public TopLevel()
{
2025-01-08 16:59:44 -05:00
_logger = new Logger(this);
}
#endregion
#region Processing
/// <summary>
/// Process args list based on current feature
/// </summary>
2020-12-08 17:05:08 -08:00
public virtual bool ProcessArgs(string[] args, FeatureSet help)
{
for (int i = 1; i < args.Length; i++)
{
// Verify that the current flag is proper for the feature
2024-07-18 00:32:41 -04:00
if (ValidateInput(args[i]))
continue;
// Special precautions for files and directories
if (File.Exists(args[i]) || Directory.Exists(args[i]))
{
2024-07-18 00:32:41 -04:00
Inputs.Add(item: args[i]);
}
2024-07-18 00:32:41 -04:00
// Special precautions for wildcarded inputs (potential paths)
2024-02-28 23:14:21 -05:00
#if NETFRAMEWORK
2024-07-18 00:32:41 -04:00
else if (args[i].Contains("*") || args[i].Contains("?"))
2024-02-28 23:14:21 -05:00
#else
2024-07-18 00:32:41 -04:00
else if (args[i].Contains('*') || args[i].Contains('?'))
2024-02-28 23:14:21 -05:00
#endif
2024-07-18 00:32:41 -04:00
{
Inputs.Add(args[i]);
}
2020-08-08 21:53:34 -07:00
2024-07-18 00:32:41 -04:00
// Everything else isn't a file
else
{
2025-01-08 16:59:44 -05:00
_logger.Error($"Invalid input detected: {args[i]}");
2024-07-18 00:32:41 -04:00
help.OutputIndividualFeature(Name);
LoggerImpl.Close();
return false;
2020-08-17 14:37:37 -07:00
}
}
return true;
}
/// <summary>
/// Process and extract variables based on current feature
/// </summary>
2021-03-19 20:52:11 -07:00
/// <returns>True if execution was successful, false otherwise</returns>
public virtual bool ProcessFeatures(Dictionary<string, Feature?> features) => true;
#endregion
#region Generic Extraction
/// <summary>
/// Get boolean value from nullable feature
/// </summary>
protected static bool GetBoolean(Dictionary<string, Feature?> features, string key)
{
if (!features.ContainsKey(key))
return false;
return true;
}
/// <summary>
/// Get int value from nullable feature
/// </summary>
protected static int GetInt32(Dictionary<string, Feature?> features, string key)
{
if (!features.ContainsKey(key))
return Int32.MinValue;
return features[key]!.GetInt32Value();
}
/// <summary>
/// Get long value from nullable feature
/// </summary>
protected static long GetInt64(Dictionary<string, Feature?> features, string key)
{
if (!features.ContainsKey(key))
return Int64.MinValue;
return features[key]!.GetInt64Value();
}
/// <summary>
/// Get list value from nullable feature
/// </summary>
protected static List<string> GetList(Dictionary<string, Feature?> features, string key)
{
if (!features.ContainsKey(key))
2024-02-28 19:19:50 -05:00
return [];
return features[key]!.GetListValue() ?? [];
}
/// <summary>
/// Get string value from nullable feature
/// </summary>
protected static string? GetString(Dictionary<string, Feature?> features, string key)
{
if (!features.ContainsKey(key))
return null;
2024-03-11 16:26:28 -04:00
return features[key]!.GetStringFieldValue();
}
#endregion
}
}