Created HasValidArguments function for checking paths

This commit is contained in:
erri120 2020-04-06 14:22:07 +02:00
parent 9a3a3448d0
commit b8be153c99
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135

View File

@ -1,5 +1,10 @@
using System;
using System.Linq;
using System.Reflection;
using Alphaleonis.Win32.Filesystem;
using CommandLine;
using Wabbajack.CLI.Verbs;
using Wabbajack.Common;
namespace Wabbajack.CLI
{
@ -11,9 +16,58 @@ namespace Wabbajack.CLI
internal static class CLIUtils
{
internal static bool VerifyArguments(AVerb verb)
internal static bool HasValidArguments(AVerb verb)
{
return true;
var props = verb.GetType().GetProperties().Where(p =>
{
var hasAttr = p.HasAttribute(typeof(OptionAttribute));
if (!hasAttr)
return false;
if (p.PropertyType != typeof(string))
return false;
var value = p.GetValue(verb);
if (value == null)
return false;
var stringValue = (string)value;
return string.IsNullOrWhiteSpace(stringValue);
});
var valid = true;
props.Do(p =>
{
if (!valid)
return;
var valueObject = p.GetValue(verb);
// not really possible since we filtered them out but whatever
if (valueObject == null)
return;
var value = (string)valueObject;
if (p.HasAttribute(typeof(FileAttribute)))
{
valid = File.Exists(value);
}
if (p.HasAttribute(typeof(DirectoryAttribute)))
{
valid = Directory.Exists(value);
}
});
return valid;
}
internal static bool HasAttribute(this MemberInfo member, Type attribute)
{
var attributes = member.GetCustomAttributes(attribute);
return attributes.Count() == 1;
}
internal static void Log(string msg, bool newLine = true)