// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using AutomationTool;
namespace Gauntlet
{
public interface IAutoParamNotifiable
{
void ParametersWereApplied(string[] Params);
};
///
/// An attribute that can be used to apply commandline options to fields or properties.
///
/// Simply tag properties or fields with the CommandLineOption, a name, and a default value, then
/// call CommandLineOption.Apply(obj, args) where args is a list of -switches or -key=value pairs
///
/// The main constraint is that your object type must be convertable from a string
///
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class AutoParam : System.Attribute
{
///
/// Default value
///
protected object Default;
///
/// Names that can refer to this param
///
public string[] OptionNames { get; protected set; }
///
/// Constructor that takes nothing. Param option should be -MemberName or -MemberName=value.
/// Members with no matching param will be left as-is.
///
public AutoParam()
{
this.OptionNames = null;
this.Default = null;
}
///
/// Constructor that takes an array of of potential argument names, e.g. {"build","builds"}
/// Members with no matching param will be left as-is.
///
///
protected AutoParam(params string[] OptionNames)
{
this.OptionNames = OptionNames;
this.Default = null;
}
///
///
/// Constructor that takes a default argument to use if no param is specified. Param option should be -MemberName or -MemberName=value.
/// Members with no matching param will be set to 'Default'
///
///
public AutoParam(object Default)
{
this.OptionNames = null;
this.Default = Default;
}
///
/// Constructor that takes an array of of potential argument names, e.g. {"build","builds"}
/// Members with no matching param will be set to 'Default'
///
///
///
protected AutoParam(object Default, params string[] OptionNames)
{
this.OptionNames = OptionNames;
this.Default = Default;
}
///
/// Checks whether Args contains a -Param statement, if so returns true else
/// returns Default
///
///
///
///
static protected bool SwitchExists(string Param, string[] Args)
{
foreach (string Arg in Args)
{
string StringArg = Arg;
if (StringArg.StartsWith("-"))
{
StringArg = Arg.Substring(1);
}
if (StringArg.ToString().Equals(Param, StringComparison.InvariantCultureIgnoreCase))
{
return true;
}
}
return false;
}
///
/// Checks Args for a -param=value statement and either returns value or the
/// provided default
///
///
///
///
///
static protected object ParaseAndCoerceParam(string Param, Type ParamType, string[] Args)
{
if (!Param.EndsWith("="))
{
Param += "=";
}
foreach (string Arg in Args)
{
string StringArg = Arg;
if (StringArg.StartsWith("-"))
{
StringArg = Arg.Substring(1);
}
if (StringArg.StartsWith(Param, StringComparison.InvariantCultureIgnoreCase))
{
string StringVal = StringArg.Substring(Param.Length);
if (ParamType.IsEnum)
{
var AllValues = Enum.GetValues(ParamType).Cast