source: xmlTools/trunk/posUpdate/ParametersParser.cs@ 800

Last change on this file since 800 was 750, checked in by s10k, 12 years ago
  • now also return errors when the arguments are incorrect
  • added parameter to display xmlTools errors

0.8d

File size: 10.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Text;
5using CLAP;
6using CLAP.Interception;
7using CLAP.Validation;
8
9namespace xmlTools
10{
11 // Define a class to receive parsed values
12 class ParametersParser
13 {
14 private static string globalFileName = "";
15 private static string globalElement = "";
16 private static string globalParentElement = "";
17 private static bool globalNoBackups = false;
18
19 [Verb]
20 public static void addValue(
21 [Parameter(Required = true, Description = "Values to add to Element. Separated multiple by spaces.")] string value
22 )
23 {
24 initialChecks();
25 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups);
26 List<string> filesToProcess = getFilesToProcess(globalFileName);
27 foreach (string currentFile in filesToProcess)
28 {
29 myTools.addValues(currentFile, value);
30 }
31 printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
32 }
33
34 [Verb]
35 public static void removeValue(
36 [Parameter(Required = true, Description = "Values to remove of Element. Separated multiple by spaces.")] string value
37 )
38 {
39 initialChecks();
40 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups);
41 List<string> filesToProcess = getFilesToProcess(globalFileName);
42 foreach (string currentFile in filesToProcess)
43 {
44 myTools.removeValues(currentFile, value);
45 }
46 printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
47 }
48
49 [Verb]
50 public static void updateChainValues(
51 [Parameter(Required = true, Description = "The new first value of the chain. All the chain will be updated based on this value")] string newValue,
52 [Parameter(Description = "Value which have some kind of relation with -newVal \n Together with -newVal updates all the values based on the" +
53 "-newVal and another position specified on -valRelation parameter (basically starts with (newVal-valRelation) ) Is especially useful when" +
54 "updating multiple related chains (on different files), like multiple objects from one position to another. Don't use with -filename, because" +
55 "it will only update one file. \nExample: xmlTools.exe -newVal \"1 1 1\" -valRelation \"4 4 4\" -valElement Translation -valParentElement" +
56 "OBANKeyFrame")] string valRelation,
57 [Parameter(Description = "Only update specific positions. Positions starts with 0, separted multiple positions with space. Example: valPositions=0 1 4")] [MoreThan(-1)] string valPositions
58 )
59 {
60 initialChecks();
61 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups);
62 List<string> filesToProcess = getFilesToProcess(globalFileName);
63 foreach (string currentFile in filesToProcess)
64 {
65 myTools.changeValue(currentFile, newValue, valRelation, valPositions);
66 }
67 printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
68 }
69
70 [Verb(Description = "Inverts a chain (like an OBAN animation). Example: xmlTools.exe -filename OBANheli_rotorblades08.xml -invert -valElement Translation -valParentElement OBANKeyFrame (inverts translation chain)")]
71 public static void Invert()
72 {
73 initialChecks();
74 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups);
75 List<string> filesToProcess = getFilesToProcess(globalFileName);
76 foreach (string currentFile in filesToProcess)
77 {
78 myTools.invert(currentFile); //Inverting the element order
79 }
80 printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
81 }
82
83 [Verb]
84 public static void replaceValue(
85 [Parameter(Required = true, Description = "Old value to replace in Element.")] string oldValue,
86 [Parameter(Required = true, Description = "New value to replace in Element.")] string newValue
87 )
88 {
89 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups);
90 List<string> filesToProcess = getFilesToProcess(globalFileName);
91 foreach (string currentFile in filesToProcess)
92 {
93 myTools.replaceValue(currentFile, oldValue, newValue);
94 }
95 printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
96 }
97
98 [Verb]
99 public static void replaceAll(
100 [Parameter(Required = true, Description = "Value to replace in Element. Replace all values of a element by another value.")] string value,
101 [Parameter(Description = "Only replace specific positions. Positions starts with 0, separted multiple positions with space. Example: valPositions=0 1 4")] [MoreThan(-1)] string valPositions
102 )
103 {
104 initialChecks();
105 XmlTools myTools = new XmlTools(globalElement, globalParentElement, globalNoBackups);
106 List<string> filesToProcess = getFilesToProcess(globalFileName);
107 foreach (string currentFile in filesToProcess)
108 {
109 if (valPositions != null)
110 {
111 myTools.replaceAll(currentFile, value, valPositions);
112 }
113 else
114 {
115 myTools.replaceAll(currentFile, value);
116 }
117 }
118 printProcessedMessage(filesToProcess.Count, MethodBase.GetCurrentMethod().Name);
119 }
120
121 /// <summary>
122 /// Patch in files can be used with wildcard or empty filename instead
123 /// </summary>
124 /// <param name="filename"></param>
125 [Verb]
126 public static void patchFile(
127 [Parameter(Description = "Force the specified patch to run in specified files")] string forceInFiles
128 )
129 {
130 XmlPatch myPatch;
131
132 if (forceInFiles != "")
133 {
134 myPatch = new XmlPatch(globalFileName, forceInFiles, globalNoBackups);
135 }
136 else
137 {
138 myPatch = new XmlPatch(globalFileName, globalNoBackups);
139 }
140
141 myPatch.startPatch();
142 }
143
144 [Verb]
145 public static void version()
146 {
147 Console.WriteLine("xmlTools v" + Program.toolsVersion);
148 Console.WriteLine("\nWritten by s10k");
149 }
150
151 [Verb]
152 public static void showErrTypes()
153 {
154 Array values = Enum.GetValues(typeof(Program.appErrors));
155
156 foreach (Program.appErrors val in values)
157 {
158 Console.WriteLine(Enum.GetName(typeof(Program.appErrors),val) + " : " + (int)val);
159 }
160 }
161
162 // Global Parameters
163 [Global(Description = "Filename to apply the operations (with patchFile specifies the patch filename). Wildcards accepted for multiple files. No filename = search all .xml files in current path.")]
164 public static void fileName(
165 [Parameter(Required = true)] string filename // xml filename. Wildcards accepted.
166 )
167 {
168 globalFileName = filename;
169 }
170
171 [Global(Description = "Element to apply the operation.")]
172 public static void element(
173 [Parameter(Required = true)] string element
174 )
175 {
176 globalElement = element;
177 }
178
179 [Global(Description = "Parent of the Element to apply the operation.")]
180 public static void parElement(
181 [Parameter(Required = true)] string parentElement
182 )
183 {
184 globalParentElement = parentElement;
185 }
186
187 [Global(Description = "Don't make backup of the files modified. Improves the overall program processing performance.")]
188 public static void noBackups()
189 {
190 globalNoBackups = true;
191 }
192
193 // Private functions
194 private static List<String> getFilesToProcess(String filename)
195 {
196 List<String> filesToProccess = new List<String>();
197
198 if (filename == "") // No filename? Process everything xml file found.
199 {
200 List<string> allXmlFiles = Util.getAllXmlFiles();
201 foreach (String file in allXmlFiles)
202 {
203 filesToProccess.Add(file);
204 }
205 }
206 else if (Util.containsWildcard(filename)) // Contains wildcards? Get all files that match it.
207 {
208 List<string> matchingWildcardFiles = Util.getXmlFilesWildcard(filename);
209 foreach (String file in matchingWildcardFiles)
210 {
211 filesToProccess.Add(file);
212 }
213 }
214 else // Add the file specified
215 {
216 if (System.IO.File.Exists(filename))
217 {
218 filesToProccess.Add(filename);
219 }
220 else
221 {
222 Program.printAppError(Program.appErrors.FILE_NOT_FOUND, "The file specified: " + filename + " doesn't exists.", true);
223 }
224 }
225 return filesToProccess;
226 }
227
228 private static void printProcessedMessage(int count, string methodName)
229 {
230 Console.WriteLine(count + " files processed with " + methodName + " command.");
231 }
232
233 private static void initialChecks()
234 {
235 if (String.IsNullOrEmpty(globalElement.Trim()))
236 {
237 Program.printAppError(Program.appErrors.ELEMENT_NOT_FOUND, "You must specify the element parameter where the operations will be processed.", true);
238 }
239
240 }
241 }
242}
Note: See TracBrowser for help on using the repository browser.