Changeset 1155
- Timestamp:
- 7/3/2009 7:29:06 AM (4 years ago)
- File:
-
- 1 edited
-
trunk/eraser6/Eraser/Program.cs (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/eraser6/Eraser/Program.cs
r1153 r1155 618 618 /// Manages a command line. 619 619 /// </summary> 620 public class CommandLine 621 { 620 public abstract class CommandLine 621 { 622 /// <summary> 623 /// Gets the CommandLine-derived object for the given command line. 624 /// </summary> 625 /// <param name="cmdParams">The raw arguments passed to the program.</param> 626 /// <returns>A processed CommandLine Object.</returns> 627 public static CommandLine Get(string[] cmdParams) 628 { 629 //Get the action. 630 if (cmdParams.Length < 1) 631 throw new ArgumentException("An action must be specified."); 632 string action = cmdParams[0]; 633 634 CommandLine result = null; 635 switch (action) 636 { 637 case "help": 638 result = new HelpCommandLine(); 639 break; 640 case "querymethods": 641 result = new QueryMethodsCommandLine(); 642 break; 643 case "importtasklist": 644 result = new ImportTaskListCommandLine(); 645 break; 646 case "addtask": 647 result = new AddTaskCommandLine(); 648 break; 649 } 650 651 if (result != null) 652 { 653 result.Parse(cmdParams); 654 return result; 655 } 656 else 657 throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, 658 "Unknown action: {0}", action)); 659 } 660 622 661 /// <summary> 623 662 /// Constructor. 624 663 /// </summary> 664 protected CommandLine() 665 { 666 } 667 668 /// <summary> 669 /// Parses the given command line arguments to the respective properties of 670 /// the class. 671 /// </summary> 625 672 /// <param name="cmdParams">The raw arguments passed to the program.</param> 626 public CommandLine(string[] cmdParams) 627 { 628 //Get the action. 629 if (cmdParams.Length < 1) 630 throw new ArgumentException("An action must be specified."); 631 Action = cmdParams[0]; 632 673 /// <returns></returns> 674 public bool Parse(string[] cmdParams) 675 { 633 676 //Iterate over each argument, resolving them ourselves and letting 634 677 //subclasses resolve them if we don't know how to. … … 640 683 throw new ArgumentException("Unknown argument: " + cmdParams[i]); 641 684 } 685 686 return true; 642 687 } 643 688 … … 648 693 /// <param name="param">The parameter to resolve.</param> 649 694 /// <returns>Return true if the parameter was resolved and accepted.</returns> 650 virtual protectedbool ResolveParameter(string param)695 protected virtual bool ResolveParameter(string param) 651 696 { 652 697 return false; … … 780 825 781 826 /// <summary> 782 /// The action that the command line specifies.783 /// </summary>784 public string Action785 {786 get787 {788 return action;789 }790 private set791 {792 action = value;793 }794 }795 796 /// <summary>797 827 /// True if no console window should be created. 798 828 /// </summary> 799 public bool Quiet 800 { 801 get 802 { 803 return quiet; 804 } 805 private set 806 { 807 quiet = value; 808 } 809 } 810 811 private string action; 812 private bool quiet; 829 public bool Quiet { get; private set; } 830 } 831 832 /// <summary> 833 /// Manages a help query command line. 834 /// </summary> 835 class HelpCommandLine : CommandLine 836 { 837 public HelpCommandLine() 838 { 839 } 840 } 841 842 class QueryMethodsCommandLine : CommandLine 843 { 844 public QueryMethodsCommandLine() 845 { 846 } 813 847 } 814 848 … … 821 855 /// Constructor. 822 856 /// </summary> 823 /// <param name="cmdParams">The raw command line arguments passed to the program.</param>824 public AddTaskCommandLine(string[] cmdParams)825 : base(cmdParams)826 {857 public AddTaskCommandLine() 858 { 859 Schedule = Schedule.RunNow; 860 Targets = new List<ErasureTarget>(); 827 861 } 828 862 … … 838 872 List<KeyValuePair<string, string>> subParams = 839 873 GetSubParameters(param.Substring(equalPos + 1)); 840 erasureMethod = new Guid(subParams[0].Key);874 ErasureMethod = new Guid(subParams[0].Key); 841 875 } 842 876 else if (IsParam(param, "schedule", "s")) … … 851 885 { 852 886 case "now": 853 schedule = Schedule.RunNow;887 Schedule = Schedule.RunNow; 854 888 break; 855 889 case "restart": 856 schedule = Schedule.RunOnRestart;890 Schedule = Schedule.RunOnRestart; 857 891 break; 858 892 default: … … 862 896 else if (IsParam(param, "recycled", "r")) 863 897 { 864 targets.Add(new RecycleBinTarget());898 Targets.Add(new RecycleBinTarget()); 865 899 } 866 900 else if (IsParam(param, "unused", "u")) … … 884 918 else 885 919 throw new ArgumentException("Unknown subparameter: " + kvp.Key); 886 targets.Add(target);920 Targets.Add(target); 887 921 } 888 922 else if (IsParam(param, "dir", "d") || IsParam(param, "directory", null)) … … 921 955 922 956 //Add the target to the list of targets 923 targets.Add(target);924 } 925 else 957 Targets.Add(target); 958 } 959 else if (IsParam(param, "file", "f")) 926 960 { 927 961 //It's just a file! 928 962 FileTarget target = new FileTarget(); 929 963 target.Path = Path.GetFullPath(param); 930 targets.Add(target); 931 } 964 Targets.Add(target); 965 } 966 else 967 return false; 932 968 933 969 return true; … … 937 973 /// The erasure method which the user specified on the command line. 938 974 /// </summary> 939 public Guid ErasureMethod 940 { 941 get 942 { 943 return erasureMethod; 944 } 945 } 975 public Guid ErasureMethod { get; private set; } 946 976 947 977 /// <summary> 948 978 /// The schedule for the current set of targets. 949 979 /// </summary> 950 public Schedule Schedule 951 { 952 get 953 { 954 return schedule; 955 } 956 } 980 public Schedule Schedule { get; private set; } 957 981 958 982 /// <summary> 959 983 /// The list of targets which was specified on the command line. 960 984 /// </summary> 961 public List<ErasureTarget> Targets 962 { 963 get 964 { 965 return new List<ErasureTarget>(targets.ToArray()); 966 } 967 } 968 969 private Guid erasureMethod; 970 private Schedule schedule = Schedule.RunNow; 971 private List<ErasureTarget> targets = new List<ErasureTarget>(); 985 public List<ErasureTarget> Targets { get; private set; } 972 986 } 973 987 … … 981 995 /// Constructor. 982 996 /// </summary> 983 /// <param name="cmdParams">The raw command line arguments passed to the program.</param> 984 public ImportTaskListCommandLine(string[] cmdParams) 985 : base(cmdParams) 997 public ImportTaskListCommandLine() 986 998 { 987 999 } … … 1017 1029 try 1018 1030 { 1019 //Parse the command line arguments. 1020 if (cmdParams.Length < 1) 1021 throw new ArgumentException("An action must be specified."); 1022 1023 switch (cmdParams[0]) 1024 { 1025 case "addtask": 1026 Arguments = new AddTaskCommandLine(cmdParams); 1027 break; 1028 case "importtasklist": 1029 Arguments = new ImportTaskListCommandLine(cmdParams); 1030 break; 1031 case "querymethods": 1032 case "help": 1033 default: 1034 Arguments = new CommandLine(cmdParams); 1035 break; 1036 } 1031 Arguments = CommandLine.Get(cmdParams); 1037 1032 1038 1033 //If the user did not specify the quiet command line, then create the console. … … 1041 1036 1042 1037 //Map actions to their handlers 1043 actionHandlers.Add( "addtask", AddTask);1044 actionHandlers.Add( "importtasklist", ImportTaskList);1045 actionHandlers.Add( "querymethods", QueryMethods);1046 actionHandlers.Add( "help", Help);1038 actionHandlers.Add(typeof(AddTaskCommandLine), AddTask); 1039 actionHandlers.Add(typeof(ImportTaskListCommandLine), ImportTaskList); 1040 actionHandlers.Add(typeof(QueryMethodsCommandLine), QueryMethods); 1041 actionHandlers.Add(typeof(HelpCommandLine), Help); 1047 1042 } 1048 1043 finally … … 1059 1054 { 1060 1055 //Call the function handling the current command line. 1061 actionHandlers[Arguments. Action]();1056 actionHandlers[Arguments.GetType()](); 1062 1057 } 1063 1058 … … 1096 1091 parameters for addtask: 1097 1092 eraser addtask [--method=<methodGUID>] [--schedule=(now|restart)] (--recycled " + 1098 @"| --unused=<volume> | --dir=<directory> | [file1 [file2 [...]]])1093 @"| --unused=<volume> | --dir=<directory> | --file=<file>)[...] 1099 1094 1100 1095 --method, -m The Erasure method to use. … … 1119 1114 delete Deletes the folder at the end of the erasure if 1120 1115 specified. 1121 file1 ... fileN The list of files to erase.1116 --file, -f Erases the specified file 1122 1117 1123 1118 parameters for querymethods: … … 1134 1129 /// The command line arguments passed to the program. 1135 1130 /// </summary> 1136 public CommandLine Arguments 1137 { 1138 get 1139 { 1140 return arguments; 1141 } 1142 private set 1143 { 1144 arguments = value; 1145 } 1146 } 1131 public CommandLine Arguments { get; private set; } 1147 1132 1148 1133 /// <summary> … … 1194 1179 ErasureMethodManager.Default : 1195 1180 ErasureMethodManager.GetInstance(taskArgs.ErasureMethod); 1181 1196 1182 foreach (ErasureTarget target in taskArgs.Targets) 1197 1183 { … … 1289 1275 #endregion 1290 1276 1291 /// <see cref="Arguments"/>1292 private CommandLine arguments;1293 1294 1277 /// <summary> 1295 1278 /// The prototype of an action handler in the class which executes an … … 1301 1284 /// Matches an action handler to a function in the class. 1302 1285 /// </summary> 1303 private Dictionary< string, ActionHandler> actionHandlers =1304 new Dictionary< string, ActionHandler>();1286 private Dictionary<Type, ActionHandler> actionHandlers = 1287 new Dictionary<Type, ActionHandler>(); 1305 1288 } 1306 1289
Note: See TracChangeset
for help on using the changeset viewer.
