source: XmlTools2/trunk/main.cpp@ 910

Last change on this file since 910 was 906, checked in by s10k, 11 years ago
File size: 9.7 KB
RevLine 
[906]1#include "main.h"
2
3int main(int argc, char *argv[])
4{
5
6 QCoreApplication app(argc, argv);
7
8 QCoreApplication::setApplicationName(GlobalVars::AppName);
9 QCoreApplication::setApplicationVersion(GlobalVars::AppVersion);
10
11 QCommandLineParser parser;
12 parser.setApplicationDescription("Additional documentation can be found at: http://wiki.oni2.net/XmlTools");
13
14 QString filesWildCard, patchFilesWildCard, forceTargetFilesWildcard;
15 QString currentVal, newVal, diffOldNewVal, positions;
16 XmlFilter filters; // Filters
17 bool noBackups=false;
18
19
20 QCommandLineOption addValuesOption(QStringList() << "a" << "add-values", "Add values to a set of XML elements.");
21 QCommandLineOption removeValuesOption(QStringList() << "remove-values", "Removes values from a set of XML elements.");
22 QCommandLineOption replaceValueOption(QStringList() << "replace-value", "Replaces 1 value in a set of XML elements.");
23 QCommandLineOption replaceAllValuesOption(QStringList() << "replace-all-values", "Replaces all values in a set of XML elements.");
24 QCommandLineOption updateElementsOption(QStringList() << "u" << "update-elements", "Updates all values in a set of XML elements.");
25 QCommandLineOption invertElementsOption(QStringList() << "i" << "invert-elements", "Inverts a set of XML elements.");
26
27 QCommandLineOption currentValOption(QStringList() << "c" << "current-val", "Current value(s) [use space as separator].","current-val");
28 QCommandLineOption newValOption(QStringList() << "n" << "new-val", "New value(s) [use space as separator]","new-val.");
29 QCommandLineOption diffOldNewValueOption(QStringList() << "d" << "diff-old-new-val", "Difference between old and new value.","diff-old-new-val");
30 QCommandLineOption positionsValueOption(QStringList() << "positions", "Positions [use space as separator] [0-index based].","positions");
31
32 QCommandLineOption filesOption(QStringList() << "f" << "files", "XML files to process [wildcards supported].", "files");
33 QCommandLineOption patchFilesOption(QStringList() << "p" << "patch-files" , "Patch files to process [wildcards supported].", "patch-files");
34 QCommandLineOption forceTargetFilesOption(QStringList() << "force-target-files" , "Force the patch-files operation in the specified XML files. [wildcards supported].", "force-target-files");
35 QCommandLineOption elementNameOption(QStringList() << "e" << "element-name", "Name of the XML element(s) where processing will occur.", "element-name");
36 QCommandLineOption parentElementNameOption(QStringList() << "parent-element-name", "Name of the XML parent element of <element-name> [used as filter].", "parent-element-name");
37 QCommandLineOption attributeNameOption("attribute-name", "Attribute name of <element-name> [used as filter].", "attribute-name");
38 QCommandLineOption attributeValueOption("attribute-value", "Attribute value of <attribute-name> [used as filter].", "attribute-value");
39 QCommandLineOption noBackupsOption(QStringList() << "no-backups", "No backups [faster processing].");
40
41 parser.addOption(addValuesOption);
42 parser.addOption(removeValuesOption);
43 parser.addOption(replaceValueOption);
44 parser.addOption(replaceAllValuesOption);
45 parser.addOption(updateElementsOption);
46 parser.addOption(invertElementsOption);
47
48 parser.addOption(currentValOption);
49 parser.addOption(newValOption);
50 parser.addOption(diffOldNewValueOption);
51 parser.addOption(positionsValueOption);
52
53 parser.addOption(filesOption);
54 parser.addOption(patchFilesOption);
55 parser.addOption(forceTargetFilesOption);
56 parser.addOption(elementNameOption);
57 parser.addOption(parentElementNameOption);
58 parser.addOption(attributeNameOption);
59 parser.addOption(attributeValueOption);
60 parser.addOption(noBackupsOption);
61
62 parser.addVersionOption();
63 parser.addHelpOption();
64
65 // Process the actual command line arguments given by the user
66 parser.process(app);
67
68 // If no arguments show help option
69 if(app.arguments().size()==1){
70 parser.showHelp();
71 }
72
73 // Check if the user doesn't want backups (it boost XmlTools peformance, lower disk output)
74 if(parser.isSet(noBackupsOption)){
75 noBackups=true;
76 }
77
78 // Get patch files wildcard if available
79 if(parser.isSet(patchFilesOption)){
80 patchFilesWildCard=parser.value(patchFilesOption);
81
82 // Never reached
83 // if(patchFilesWildCard==""){
84 // UtilXmlTools::displayErrorMessage("Parameter Parsing", "patch-files option requires 1 value: <patch-files> the patch files to process (wildcards supported).");
85 // }
86
87 forceTargetFilesWildcard=parser.value(forceTargetFilesOption);
88
89
90 XmlPatch myXmlPatch(patchFilesWildCard,forceTargetFilesWildcard,noBackups);
91 myXmlPatch.readAndProcessPatchFile(); // beging file patch processing
92
93 return 0;
94 }
95
96 // Get element name if available
97 if(parser.isSet(elementNameOption)){
98 filters.setElementName(parser.value(elementNameOption));
99 }
100
101 if(filters.getElementName()==""){
102 UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option is required if not using patch-files option.");
103 }
104
105 // Get current value(s) if avaialabe
106 if(parser.isSet(currentValOption)){
107 currentVal=parser.value(currentValOption);
108 }
109
110 // Get new value(s) if avaialabe
111 if(parser.isSet(newValOption)){
112 newVal=parser.value(newValOption);
113 }
114
115 // Get difference between old and new value if avaialabe
116 if(parser.isSet(diffOldNewValueOption)){
117 diffOldNewVal=parser.value(diffOldNewValueOption);
118 }
119
120 // Get positions if avaialabe
121 if(parser.isSet(positionsValueOption)){
122 positions=parser.value(positionsValueOption);
123 }
124
125 // Get parent element name if available
126 if(parser.isSet(parentElementNameOption)){
127 filters.setParentElementName(parser.value(parentElementNameOption));
128 }
129
130 // Get attribute name if available
131 if(parser.isSet(attributeNameOption)){
132 filters.setAttributeName(parser.value(attributeNameOption));
133 }
134
135 // Get attribute value if available
136 if(parser.isSet(attributeValueOption)){
137 filters.setAttributeValue(parser.value(attributeValueOption));
138 }
139
140 // Check attribute filters
141 if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
142 UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-value option is required if using attribute-name option.");
143 }
144
145 if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
146 UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-name option is required if using attribute-value option.");
147 }
148
149 // Get files wildcard if available
150 if(parser.isSet(filesOption)){
151 filesWildCard=parser.value(filesOption);
152 }
153 else{
154 UtilXmlTools::displayErrorMessage("Parameter Parsing", "files option requires 1 value: <files> the XML files to process (wildcards supported).");
155 }
156
157 XmlTools myXmlTools(filesWildCard,filters,noBackups);
158
159
160 // Users wants an add-option?
161 if(parser.isSet(addValuesOption)){
162
163 if(newVal==""){
164 UtilXmlTools::displayErrorMessage("Parameter Parsing", "add-value option requires 1 value: <new-val> the new values to add (space separated).");
165 }
166
167 myXmlTools.addValues(newVal);
168 }
169 else if(parser.isSet(removeValuesOption)){ // Or remove-values option?
170
171 if(currentVal==""){
172 UtilXmlTools::displayErrorMessage("Parameter Parsing","remove-values option requires 1 value: <current-val> the current values to remove (space separated).");
173 }
174
175 myXmlTools.removeValues(currentVal);
176 }
177 else if(parser.isSet(replaceValueOption)){ // Or replace-value option?
178
179 if(currentVal=="" || newVal==""){
180 UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-value option requires 2 values: <current-val> the current value and <new-val> the new value.");
181 }
182
183 myXmlTools.replaceValue(currentVal,newVal);
184 }
185 else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option?
186
187 if(newVal=="" && positions==""){
188 UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-all-values option requires 1 value: <new-val> the new value.\n" +
189 Util::toQString("It has also 1 optional value: <positions> the positions to replace (space separated and 0-index based)."));
190 }
191
192 myXmlTools.replaceAll(newVal,positions);
193 }
194 else if(parser.isSet(updateElementsOption)){ // Or update-elements option?
195
196 if(diffOldNewVal==""){
197 UtilXmlTools::displayErrorMessage("Parameter Parsing","update-elements option requires 1 value: <diff-old-new-val> the difference between one current element "+
198 Util::toQString("value and one new element value (current value and new value must have the same position)."));
199 }
200
201 myXmlTools.updateElements(diffOldNewVal);
202 }
203 else if(parser.isSet(invertElementsOption)){ // Or invert-elements option?
204 myXmlTools.invertElements();
205 }
206 else{
207 UtilXmlTools::displayErrorMessage("Parameter Parsing","One operation is required to XmlTools continue. Possible operations are:\n"+
208 Util::toQString("--patch-files\n--add-values\n--remove-values\n--replace-value\n--replace-all-values\n--update-elements\n--invert-elements"));
209 }
210
211 //std::cout << "Started" << std::endl;
212
213 //app.exec();
214 return 0;
215}
Note: See TracBrowser for help on using the repository browser.