|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace libArgument |
| 7 | +{ |
| 8 | + public static class ArgumentParser |
| 9 | + { |
| 10 | + public static T Parse<T> (string[] args) where T : class, new() |
| 11 | + { |
| 12 | + T options = new T (); |
| 13 | + var list = new List<string> (args); |
| 14 | + var fields = typeof (T).GetFields (BindingFlags.Instance | BindingFlags.Public); |
| 15 | + foreach (var item in fields) |
| 16 | + ParseField<T> (options, list, item.Name); |
| 17 | + return options; |
| 18 | + } |
| 19 | + |
| 20 | + static void ParseField<T> (T options, List<string> args, string name) where T : class, new() |
| 21 | + { |
| 22 | + var field = typeof(T).GetField (name); |
| 23 | + var attributes = field.GetCustomAttributes (); |
| 24 | + var enumerable = attributes as object[] ?? attributes.ToArray (); |
| 25 | + var cast = enumerable.First (attrib => attrib as CastAs != null) as CastAs ?? new CastAs (CastingType.String); |
| 26 | + foreach (var attrib in enumerable) { |
| 27 | + if (attrib is Switch) { |
| 28 | + field.SetValue (options, true); |
| 29 | + return; |
| 30 | + } |
| 31 | + if (!(attrib is Argument && attrib as Argument != null)) |
| 32 | + continue; |
| 33 | + var attribute = attrib as Argument; |
| 34 | + if (!args.Contains (attribute.FriendlyShort) || args.Contains (attribute.FriendlyFull)) |
| 35 | + continue; |
| 36 | + var str = args.Contains (attribute.FriendlyShort) ? attribute.FriendlyShort : attribute.FriendlyFull; |
| 37 | + var index = 1 + args.IndexOf (str); |
| 38 | + var indexInRange = index <= args.Count - 1; |
| 39 | + if (cast.Type != CastingType.Boolean && !indexInRange) |
| 40 | + throw new ArgumentOutOfRangeException (string.Format ("Parameter of argument {0} out of range.", str)); |
| 41 | + switch (cast.Type) { |
| 42 | + case CastingType.Boolean: |
| 43 | + var bool_result = false; |
| 44 | + if (!Boolean.TryParse (args [index], out bool_result)) |
| 45 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to Boolean.", str)); |
| 46 | + field.SetValue (options, bool_result); |
| 47 | + return; |
| 48 | + case CastingType.Object: |
| 49 | + field.SetValue (options, args [index]); |
| 50 | + return; |
| 51 | + case CastingType.Int32: |
| 52 | + var int32_result = 0; |
| 53 | + if (!Int32.TryParse (args [index], out int32_result)) |
| 54 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to Int32.", str)); |
| 55 | + field.SetValue (options, int32_result); |
| 56 | + return; |
| 57 | + case CastingType.Int64: |
| 58 | + var int64_result = 0L; |
| 59 | + if (!Int64.TryParse (args [index], out int64_result)) |
| 60 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to Int64.", str)); |
| 61 | + field.SetValue (options, int64_result); |
| 62 | + return; |
| 63 | + case CastingType.UInt32: |
| 64 | + var uint32_result = 0u; |
| 65 | + if (!UInt32.TryParse (args [index], out uint32_result)) |
| 66 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to UInt32.", str)); |
| 67 | + field.SetValue (options, uint32_result); |
| 68 | + return; |
| 69 | + case CastingType.UInt64: |
| 70 | + var uint64_result = 0uL; |
| 71 | + if (!UInt64.TryParse (args [index], out uint64_result)) |
| 72 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to UInt64.", str)); |
| 73 | + field.SetValue (options, uint64_result); |
| 74 | + return; |
| 75 | + case CastingType.Long: |
| 76 | + var long_result = 0L; |
| 77 | + if (!long.TryParse (args [index], out long_result)) |
| 78 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to long.", str)); |
| 79 | + field.SetValue (options, long_result); |
| 80 | + return; |
| 81 | + case CastingType.ULong: |
| 82 | + var ulong_result = 0uL; |
| 83 | + if (!ulong.TryParse (args [index], out ulong_result)) |
| 84 | + throw new InvalidCastException (string.Format ("Can't cast parameter of argument {0} to ulong.", str)); |
| 85 | + field.SetValue (options, ulong_result); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
0 commit comments