-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 33ceaef
Showing
14 changed files
with
3,047 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace GodPotato | ||
{ | ||
internal class ArgsParse | ||
{ | ||
[System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)] | ||
public sealed class ArgsAttribute : Attribute | ||
{ | ||
public string FieldName { get; set; } | ||
public string DefaultValue { get; set; } | ||
public bool Required { get; set; } | ||
public string Description { get; set; } | ||
public ArgsAttribute(string FieldName, string DefaultValue) | ||
{ | ||
this.FieldName = FieldName; | ||
this.DefaultValue = DefaultValue; | ||
} | ||
} | ||
|
||
protected static void ParseArgsSetValue(object obj, PropertyInfo propertyInfo, string value) | ||
{ | ||
Type propertyType = propertyInfo.PropertyType; | ||
object valueObj = null; | ||
if (propertyType.IsPrimitive) | ||
{ | ||
MethodInfo methodInfo = propertyType.GetMethod("Parse", new Type[] { typeof(string) }); | ||
methodInfo.Invoke(null, new object[] { valueObj }); | ||
} | ||
else if (propertyType == typeof(string)) | ||
{ | ||
valueObj = value; | ||
} | ||
else if (propertyType == typeof(string[])) | ||
{ | ||
string[] values = value.Split(','); | ||
valueObj = values; | ||
} | ||
else if (propertyType == typeof(byte[])) | ||
{ | ||
valueObj = Convert.FromBase64String(value); | ||
} | ||
else if (propertyType.IsArray && propertyType.GetElementType().IsPrimitive) | ||
{ | ||
Type elementType = propertyType.GetElementType(); | ||
string[] strValues = value.Split(','); | ||
List<object> values = new List<object>(); | ||
MethodInfo methodInfo = elementType.GetMethod("Parse", new Type[] { typeof(string) }); | ||
|
||
foreach (var str in strValues) | ||
{ | ||
if (str.Contains("-")) | ||
{ | ||
string[] strRanges = str.Split('-'); | ||
long startRange = long.Parse(strRanges[0]); | ||
long stopRange = long.Parse(strRanges[1]); | ||
for (long i = startRange; i <= stopRange; i++) | ||
{ | ||
values.Add((methodInfo.Invoke(null, new object[] { i.ToString() }))); | ||
} | ||
} | ||
else | ||
{ | ||
values.Add(methodInfo.Invoke(null, new object[] { str })); | ||
} | ||
} | ||
Array array = Array.CreateInstance(elementType, values.Count); | ||
for (int i = 0; i < values.Count; i++) | ||
{ | ||
array.SetValue(values[i], i); | ||
} | ||
valueObj = array; | ||
} | ||
|
||
propertyInfo.SetValue(obj, valueObj, null); | ||
|
||
} | ||
public static T ParseArgs<T>(string[] args) | ||
{ | ||
Type type = typeof(T); | ||
Type argsAttributeType = typeof(ArgsAttribute); | ||
object value = type.GetConstructor(new Type[0]).Invoke(new object[0]); | ||
PropertyInfo[] propertyInfos = type.GetProperties(); | ||
Dictionary<string, PropertyInfo> propertyInfoDict = new Dictionary<string, PropertyInfo>(); | ||
List<string> requiredPropertyList = new List<string>(); | ||
foreach (PropertyInfo propertyInfo in propertyInfos) | ||
{ | ||
ArgsAttribute argsAttribute = (ArgsAttribute)Attribute.GetCustomAttribute(propertyInfo, argsAttributeType); | ||
if (argsAttribute != null) | ||
{ | ||
string attributeLower = argsAttribute.FieldName.ToLower(); | ||
if (argsAttribute.Required) | ||
{ | ||
requiredPropertyList.Add(attributeLower); | ||
} | ||
propertyInfoDict.Add(attributeLower, propertyInfo); | ||
ParseArgsSetValue(value, propertyInfo, argsAttribute.DefaultValue); | ||
} | ||
} | ||
|
||
for (int i = 0; i < args.Length; i++) | ||
{ | ||
string currentArg = args[i]; | ||
if (currentArg.StartsWith("-")) | ||
{ | ||
string currentArgName = currentArg.Substring(1).ToLower(); | ||
if ((i + 1 < args.Length)) | ||
{ | ||
i++; | ||
string currentArgValue = args[i]; | ||
|
||
PropertyInfo propertyInfo; | ||
if (propertyInfoDict.TryGetValue(currentArgName, out propertyInfo)) | ||
{ | ||
ParseArgsSetValue(value, propertyInfo, currentArgValue); | ||
requiredPropertyList.Remove(currentArgName); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (requiredPropertyList.Count > 0) | ||
{ | ||
throw new Exception($"Required Parameter {string.Join(",", requiredPropertyList.ToArray())}"); | ||
} | ||
|
||
return (T)value; | ||
} | ||
public static string PrintHelp(Type type,string head,string appName, string[] examples) {; | ||
Type argsAttributeType = typeof(ArgsAttribute); | ||
object value = type.GetConstructor(new Type[0]).Invoke(new object[0]); | ||
PropertyInfo[] propertyInfos = type.GetProperties(); | ||
List<ArgsAttribute> propertyInfoList = new List<ArgsAttribute>(); | ||
List<ArgsAttribute> requiredPropertyList = new List<ArgsAttribute>(); | ||
foreach (PropertyInfo propertyInfo in propertyInfos) | ||
{ | ||
ArgsAttribute argsAttribute = (ArgsAttribute)Attribute.GetCustomAttribute(propertyInfo, argsAttributeType); | ||
if (argsAttribute != null) | ||
{ | ||
propertyInfoList.Add(argsAttribute); | ||
if (argsAttribute.Required) | ||
{ | ||
requiredPropertyList.Add(argsAttribute); | ||
} | ||
} | ||
} | ||
|
||
StringWriter stringBuilder = new StringWriter(); | ||
stringBuilder.WriteLine(head); | ||
stringBuilder.WriteLine(); | ||
stringBuilder.WriteLine("Arguments:"); | ||
stringBuilder.WriteLine(); | ||
foreach (var argsAttribute in propertyInfoList) | ||
{ | ||
stringBuilder.WriteLine("\t-{0} Required:{1} {2} (default {3})", argsAttribute.FieldName, argsAttribute.Required, argsAttribute.Description, argsAttribute.DefaultValue); | ||
} | ||
stringBuilder.WriteLine(); | ||
stringBuilder.WriteLine("Example:"); | ||
stringBuilder.WriteLine(); | ||
foreach (string example in examples) | ||
{ | ||
stringBuilder.WriteLine(example); | ||
} | ||
|
||
|
||
|
||
if (requiredPropertyList.Count > 0) | ||
{ | ||
string requiredExample = ""; | ||
requiredExample = appName + " "; | ||
foreach (ArgsAttribute argsAttribute in requiredPropertyList) | ||
{ | ||
if (argsAttribute.DefaultValue.Contains(" ") || argsAttribute.DefaultValue.Contains("\t") || argsAttribute.DefaultValue.Contains("\r")) | ||
{ | ||
requiredExample += string.Format("-{0} \"{1}\" ", argsAttribute.FieldName, argsAttribute.DefaultValue); | ||
} | ||
else | ||
{ | ||
requiredExample += string.Format("-{0} {1} ", argsAttribute.FieldName, argsAttribute.DefaultValue); | ||
} | ||
} | ||
stringBuilder.WriteLine(requiredExample); ; | ||
} | ||
|
||
if (propertyInfoList.Count > 0) | ||
{ | ||
string allParameterExample = ""; | ||
allParameterExample = appName + " "; | ||
foreach (ArgsAttribute argsAttribute in propertyInfoList) | ||
{ | ||
if (argsAttribute.DefaultValue.Contains(" ") || argsAttribute.DefaultValue.Contains("\t") || argsAttribute.DefaultValue.Contains("\r")) | ||
{ | ||
allParameterExample += string.Format("-{0} \"{1}\" ", argsAttribute.FieldName, argsAttribute.DefaultValue); | ||
} | ||
else | ||
{ | ||
allParameterExample += string.Format("-{0} {1} ", argsAttribute.FieldName, argsAttribute.DefaultValue); | ||
} | ||
} | ||
stringBuilder.WriteLine(allParameterExample); ; | ||
} | ||
|
||
|
||
|
||
return stringBuilder.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{2AE886C3-3272-40BE-8D3C-EBAEDE9E61E1}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<RootNamespace>GodPotato</RootNamespace> | ||
<AssemblyName>GodPotato</AssemblyName> | ||
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<Deterministic>true</Deterministic> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="ArgsParse.cs" /> | ||
<Compile Include="NativeAPI\GodPotatoContext.cs" /> | ||
<Compile Include="NativeAPI\GodPotatoStorageTrigger.cs" /> | ||
<Compile Include="NativeAPI\IEnumSTATSTG.cs" /> | ||
<Compile Include="NativeAPI\ILockBytes.cs" /> | ||
<Compile Include="NativeAPI\IMarshal.cs" /> | ||
<Compile Include="NativeAPI\IStorage.cs" /> | ||
<Compile Include="NativeAPI\IStream.cs" /> | ||
<Compile Include="NativeAPI\NativeMethods.cs" /> | ||
<Compile Include="NativeAPI\ObjRef.cs" /> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="SharpToken.cs" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> |
Oops, something went wrong.