Program.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace TodoList
  5. {
  6. public class Program
  7. {
  8. private static CommandLineIterator ParseCommand(Tuple<CommandAttribute, Type> Command, CommandLineIterator Iterator, Dictionary<String, Object> PipedArguments)
  9. {
  10. var commandObject = Activator.CreateInstance(Command.Item2) as ICommand;
  11. try
  12. {
  13. Iterator = Iterator.Advance(); // Skip the command name.
  14. // Set the piped arguments.
  15. foreach (var pipedArgument in PipedArguments)
  16. foreach (var member in commandObject.GetType().GetFields())
  17. if (member.Name == pipedArgument.Key && member.FieldType == pipedArgument.Value.GetType())
  18. member.SetValue(Command, pipedArgument.Value);
  19. var defaultList = new List<Tuple<DefaultSwitchAttribute, System.Reflection.FieldInfo>>();
  20. System.Reflection.FieldInfo unknownSwitch = null;
  21. var unknownSwitchCaught = false;
  22. foreach (var member in commandObject.GetType().GetFields())
  23. {
  24. var defaultAttribute = member.GetCustomAttributes(true).FirstOrDefault(a => a is DefaultSwitchAttribute) as DefaultSwitchAttribute;
  25. if (defaultAttribute != null)
  26. defaultList.Add(Tuple.Create(defaultAttribute, member));
  27. if (member.GetCustomAttributes(true).Any(a => a is UnknownSwitchAttribute))
  28. unknownSwitch = member;
  29. }
  30. defaultList = defaultList.OrderBy(t => t.Item1.Order).ToList();
  31. while (!Iterator.AtEnd() && !Iterator.Peek().StartsWith("+"))
  32. {
  33. if (Iterator.Peek().StartsWith("-"))
  34. {
  35. var memberName = Iterator.Peek().Substring(1);
  36. Iterator = Iterator.Advance();
  37. bool matchingMemberFound = false;
  38. foreach (var member in commandObject.GetType().GetFields())
  39. if (member.Name == memberName)
  40. {
  41. if (member.FieldType == typeof(bool))
  42. member.SetValue(commandObject, true);
  43. else if (member.FieldType == typeof(String) && member.GetCustomAttributes(true).Any(a => a is GreedyArgumentAttribute))
  44. {
  45. var v = "";
  46. while (!Iterator.AtEnd())
  47. {
  48. v += Iterator.Peek();
  49. Iterator = Iterator.Advance();
  50. if (!Iterator.AtEnd())
  51. v += " ";
  52. }
  53. member.SetValue(commandObject, v);
  54. }
  55. else
  56. {
  57. member.SetValue(commandObject, ConvertArgument(Iterator.Peek(), member.FieldType));
  58. Iterator = Iterator.Advance();
  59. }
  60. matchingMemberFound = true;
  61. }
  62. if (!matchingMemberFound)
  63. {
  64. if (unknownSwitch == null)
  65. throw new InvalidOperationException("Unknown switch " + memberName);
  66. else
  67. {
  68. if (unknownSwitchCaught)
  69. throw new InvalidOperationException("Caught multiple unknown switches. " + memberName);
  70. unknownSwitchCaught = true;
  71. unknownSwitch.SetValue(commandObject, ConvertArgument(memberName, unknownSwitch.FieldType));
  72. }
  73. }
  74. }
  75. else
  76. {
  77. if (defaultList.Count == 0)
  78. break;
  79. if (defaultList[0].Item2.FieldType == typeof(String) && defaultList[0].Item2.GetCustomAttributes(true).Any(a => a is GreedyArgumentAttribute))
  80. {
  81. var v = "";
  82. while (!Iterator.AtEnd())
  83. {
  84. v += Iterator.Peek();
  85. Iterator = Iterator.Advance();
  86. if (!Iterator.AtEnd())
  87. v += " ";
  88. }
  89. defaultList[0].Item2.SetValue(commandObject, v);
  90. }
  91. else
  92. {
  93. defaultList[0].Item2.SetValue(commandObject, ConvertArgument(Iterator.Peek(), defaultList[0].Item2.FieldType));
  94. Iterator = Iterator.Advance();
  95. }
  96. defaultList.RemoveAt(0);
  97. }
  98. }
  99. commandObject.Invoke(PipedArguments);
  100. return Iterator;
  101. }
  102. catch (Exception e)
  103. {
  104. Console.WriteLine(e.Message);
  105. Console.WriteLine(e.StackTrace);
  106. Console.WriteLine("Try 'help -" + Command.Item1.Name + "'");
  107. if (!String.IsNullOrEmpty(Command.Item1.ErrorText))
  108. Console.WriteLine(Command.Item1.ErrorText);
  109. Environment.Exit(0);
  110. throw new Exception();
  111. }
  112. }
  113. private static Object ConvertArgument(String Argument, Type DestinationType)
  114. {
  115. if (DestinationType == typeof(String))
  116. return Argument;
  117. else if (DestinationType == typeof(UInt32))
  118. return Convert.ToUInt32(Argument, 16);
  119. else if (DestinationType.IsEnum)
  120. {
  121. var value = Enum.Parse(DestinationType, Argument.ToUpperInvariant());
  122. return Convert.ChangeType(value, DestinationType);
  123. }
  124. else
  125. return Convert.ChangeType(Argument, DestinationType);
  126. }
  127. public static void Main(string[] args)
  128. {
  129. try
  130. {
  131. var commands = new Dictionary<String, Tuple<CommandAttribute, Type>>();
  132. foreach (var type in System.Reflection.Assembly.GetExecutingAssembly().GetTypes())
  133. {
  134. var commandAttribute = type.GetCustomAttributes(true).FirstOrDefault(a => a is CommandAttribute) as CommandAttribute;
  135. if (commandAttribute != null)
  136. commands[commandAttribute.Name] = Tuple.Create(commandAttribute, type);
  137. }
  138. var pipedArguments = new Dictionary<String, Object>();
  139. var iterator = new CommandLineIterator(args, 0);
  140. while (!iterator.AtEnd())
  141. {
  142. var commandName = iterator.Peek().Replace("+", "");
  143. var command = commands.Values.FirstOrDefault(c => c.Item1.Name == commandName || c.Item1.Synonyms.Contains(commandName));
  144. if (command == null)
  145. throw new InvalidOperationException("Unknown command " + iterator.Peek());
  146. iterator = ParseCommand(command, iterator, pipedArguments);
  147. }
  148. }
  149. catch (Exception e)
  150. {
  151. Console.WriteLine(e.Message);
  152. Console.WriteLine(e.StackTrace);
  153. }
  154. finally
  155. {
  156. Console.ResetColor();
  157. }
  158. }
  159. }
  160. }