[927] | 1 | #include "optionsparser.h"
|
---|
| 2 |
|
---|
| 3 | OptionsParser::OptionsParser(QStringList args)
|
---|
| 4 | {
|
---|
| 5 | this->args=args;
|
---|
| 6 | }
|
---|
| 7 |
|
---|
| 8 | void OptionsParser::parse(){
|
---|
| 9 |
|
---|
| 10 | QCommandLineParser parser;
|
---|
| 11 | parser.setApplicationDescription("Additional documentation can be found at: http://wiki.oni2.net/XmlTools");
|
---|
| 12 |
|
---|
| 13 | XmlTools *myXmlTools;
|
---|
| 14 | QString filesWildCard, patchFilesWildCard, forceTargetFilesWildcard;
|
---|
| 15 | QString currentVal, newVal, diffOldNewVal, positions;
|
---|
| 16 | QString xPathExpression;
|
---|
| 17 | XmlFilter filters; // Filters
|
---|
| 18 |
|
---|
| 19 | bool noBackups=false;
|
---|
| 20 | bool noVerbose=false;
|
---|
| 21 |
|
---|
| 22 | QCommandLineOption addValuesOption(QStringList() << "a" << "add-values", "Add values to a set of XML elements.");
|
---|
| 23 | QCommandLineOption removeValuesOption(QStringList() << "remove-values", "Removes values from a set of XML elements.");
|
---|
| 24 | QCommandLineOption replaceValueOption(QStringList() << "replace-value", "Replaces 1 value in a set of XML elements.");
|
---|
| 25 | QCommandLineOption replaceAllValuesOption(QStringList() << "replace-all-values", "Replaces all values in a set of XML elements.");
|
---|
| 26 | QCommandLineOption updateElementsOption(QStringList() << "u" << "update-elements", "Updates all values in a set of XML elements.");
|
---|
| 27 | QCommandLineOption invertElementsOption(QStringList() << "i" << "invert-elements", "Inverts a set of XML elements.");
|
---|
| 28 |
|
---|
| 29 | QCommandLineOption currentValOption(QStringList() << "c" << "current-val", "Current value(s) [use space as separator].","current-val");
|
---|
| 30 | QCommandLineOption newValOption(QStringList() << "n" << "new-val", "New value(s) [use space as separator].","new-val");
|
---|
| 31 | QCommandLineOption diffOldNewValueOption(QStringList() << "d" << "diff-old-new-val", "Difference between old and new value.","diff-old-new-val");
|
---|
| 32 | QCommandLineOption positionsValueOption(QStringList() << "positions", "Positions [use space as separator] [0-index based].","positions");
|
---|
| 33 |
|
---|
| 34 | QCommandLineOption filesOption(QStringList() << "f" << "files", "XML files to process [wildcards supported].", "files");
|
---|
| 35 | QCommandLineOption patchFilesOption(QStringList() << "p" << "patch-files" , "Patch files to process [wildcards supported].", "patch-files");
|
---|
| 36 | QCommandLineOption forceTargetFilesOption(QStringList() << "force-target-files" , "Force the patch-files operation in the specified XML files. [wildcards supported].", "force-target-files");
|
---|
| 37 | QCommandLineOption elementNameOption(QStringList() << "e" << "element-name", "Name of the XML element(s) where processing will occur.", "element-name");
|
---|
| 38 | QCommandLineOption parentElementNameOption(QStringList() << "parent-element-name", "Name of the XML parent element of <element-name> [used as filter].", "parent-element-name");
|
---|
| 39 | QCommandLineOption attributeNameOption("attribute-name", "Attribute name of <element-name> [used as filter].", "attribute-name");
|
---|
| 40 | QCommandLineOption attributeValueOption("attribute-value", "Attribute value of <attribute-name> [used as filter].", "attribute-value");
|
---|
| 41 | QCommandLineOption xPathExpressionOption(QStringList() << "x" << "xpath-expression", "XPath 1.0 expression to select elements where processing will occur.", "xpath-expression");
|
---|
| 42 | QCommandLineOption noBackupsOption(QStringList() << "no-backups", "No backups [faster processing].");
|
---|
| 43 | QCommandLineOption noVerboseOption(QStringList() << "no-verbose", "Reduce the number of text messages output [faster processing].");
|
---|
| 44 | QCommandLineOption aeiPatchFilesListOption(QStringList() << "aei-patch-files-list" , "Exclusive option for AEI. Provide a list of patches to process.", "aei-patch-files-list");
|
---|
| 45 |
|
---|
| 46 | parser.addOption(addValuesOption);
|
---|
| 47 | parser.addOption(removeValuesOption);
|
---|
| 48 | parser.addOption(replaceValueOption);
|
---|
| 49 | parser.addOption(replaceAllValuesOption);
|
---|
| 50 | parser.addOption(updateElementsOption);
|
---|
| 51 | parser.addOption(invertElementsOption);
|
---|
| 52 |
|
---|
| 53 | parser.addOption(currentValOption);
|
---|
| 54 | parser.addOption(newValOption);
|
---|
| 55 | parser.addOption(diffOldNewValueOption);
|
---|
| 56 | parser.addOption(positionsValueOption);
|
---|
| 57 |
|
---|
| 58 | parser.addOption(filesOption);
|
---|
| 59 | parser.addOption(patchFilesOption);
|
---|
| 60 | parser.addOption(forceTargetFilesOption);
|
---|
| 61 | parser.addOption(elementNameOption);
|
---|
| 62 | parser.addOption(parentElementNameOption);
|
---|
| 63 | parser.addOption(attributeNameOption);
|
---|
| 64 | parser.addOption(attributeValueOption);
|
---|
| 65 | parser.addOption(xPathExpressionOption);
|
---|
| 66 | parser.addOption(noBackupsOption);
|
---|
| 67 | parser.addOption(noVerboseOption);
|
---|
| 68 | parser.addOption(aeiPatchFilesListOption);
|
---|
| 69 |
|
---|
| 70 | parser.addVersionOption();
|
---|
| 71 | parser.addHelpOption();
|
---|
| 72 |
|
---|
| 73 | // Process the actual command line arguments given by the user
|
---|
| 74 | parser.process(this->args);
|
---|
| 75 |
|
---|
| 76 | // If no arguments show help option
|
---|
| 77 | if(this->args.size()==1){
|
---|
| 78 | parser.showHelp();
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | // Begin by processing AEI special parameter
|
---|
| 82 | if(parser.isSet(aeiPatchFilesListOption)){
|
---|
| 83 |
|
---|
| 84 | QFile inputFile(parser.value(aeiPatchFilesListOption));
|
---|
| 85 | QStringList temp; // contains the complete line. first in
|
---|
| 86 | QStringList patchFiles, targetXmlFiles;
|
---|
| 87 | QString line;
|
---|
| 88 |
|
---|
| 89 | if (inputFile.open(QIODevice::ReadOnly))
|
---|
| 90 | {
|
---|
| 91 |
|
---|
| 92 | QTextStream fileStream(&inputFile);
|
---|
| 93 |
|
---|
| 94 | // Process all the patches until the end of file
|
---|
| 95 | while ( !fileStream.atEnd() ){
|
---|
| 96 | line = fileStream.readLine();
|
---|
| 97 |
|
---|
| 98 | if(line.startsWith('"')){ // Only read when starting with quotes
|
---|
[942] | 99 | temp=line.split(" \""); // We need to use more than space, because if paths contains spaces...
|
---|
[927] | 100 | patchFiles << temp[0].remove('"'); // remove the quotes, they are now not needed
|
---|
| 101 | targetXmlFiles << temp[1].remove('"');
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | inputFile.close();
|
---|
| 106 |
|
---|
| 107 | // Now let's process each patch file and target file
|
---|
[944] | 108 | //#pragma omp parallel for
|
---|
[927] | 109 | for(int i=0; i<patchFiles.size(); i++){
|
---|
| 110 | XmlPatch myXmlPatch(patchFiles[i],targetXmlFiles[i],true,true); // use --no-backups and --no-verbose for AEI
|
---|
| 111 | myXmlPatch.readAndProcessPatchFile(); // process current file
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | UtilXmlTools::displaySuccessMessage(patchFiles.size(),"AEI patches");
|
---|
| 115 |
|
---|
| 116 | }
|
---|
| 117 | else{
|
---|
| 118 | UtilXmlTools::displayErrorMessage("Read file", "Error opening AEI-patch-files-list file: '" + parser.value(aeiPatchFilesListOption) + "'.\n" + inputFile.errorString());
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | // Check if the user doesn't want backups (it boosts XmlTools peformance, lower disk output)
|
---|
| 126 | if(parser.isSet(noBackupsOption)){
|
---|
| 127 | noBackups=true;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // Check if the user doesn't want verbose mode (it boosts XmlTools peformance, lower std output)
|
---|
| 131 | if(parser.isSet(noVerboseOption)){
|
---|
| 132 | noVerbose=true;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // Get patch files wildcard if available
|
---|
| 136 | if(parser.isSet(patchFilesOption)){
|
---|
| 137 | patchFilesWildCard=parser.value(patchFilesOption);
|
---|
| 138 |
|
---|
| 139 | // Never reached
|
---|
| 140 | // if(patchFilesWildCard==""){
|
---|
| 141 | // UtilXmlTools::displayErrorMessage("Parameter Parsing", "patch-files option requires 1 value: <patch-files> the patch files to process (wildcards supported).");
|
---|
| 142 | // }
|
---|
| 143 |
|
---|
| 144 | forceTargetFilesWildcard=parser.value(forceTargetFilesOption);
|
---|
| 145 |
|
---|
| 146 | XmlPatch myXmlPatch(patchFilesWildCard,forceTargetFilesWildcard,noBackups,noVerbose);
|
---|
| 147 | myXmlPatch.readAndProcessPatchFile(); // beging file patch processing
|
---|
| 148 |
|
---|
| 149 | return;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | if(!parser.isSet(elementNameOption) && !parser.isSet(xPathExpressionOption)){
|
---|
| 153 | UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option or xpath-expression option is required if not using patch-files option.");
|
---|
| 154 | }
|
---|
| 155 | else if(parser.isSet(elementNameOption) && parser.isSet(xPathExpressionOption)){
|
---|
| 156 | UtilXmlTools::displayErrorMessage("Parameter Parsing","element-name option and xpath-expression options cannot be used simultaneously.");
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // Get element name if available
|
---|
| 160 | if(parser.isSet(elementNameOption)){
|
---|
| 161 | filters.setElementName(parser.value(elementNameOption));
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | // Get xpath expression if available
|
---|
| 165 | if(parser.isSet(xPathExpressionOption)){
|
---|
| 166 | xPathExpression=parser.value(xPathExpressionOption);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // Get current value(s) if avaialabe
|
---|
| 170 | if(parser.isSet(currentValOption)){
|
---|
| 171 | currentVal=parser.value(currentValOption);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | // Get new value(s) if avaialabe
|
---|
| 175 | if(parser.isSet(newValOption)){
|
---|
| 176 | newVal=parser.value(newValOption);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | // Get difference between old and new value if avaialabe
|
---|
| 180 | if(parser.isSet(diffOldNewValueOption)){
|
---|
| 181 | diffOldNewVal=parser.value(diffOldNewValueOption);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | // Get positions if avaialabe
|
---|
| 185 | if(parser.isSet(positionsValueOption)){
|
---|
| 186 | positions=parser.value(positionsValueOption);
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | // Get parent element name if available
|
---|
| 190 | if(parser.isSet(parentElementNameOption)){
|
---|
| 191 | filters.setParentElementName(parser.value(parentElementNameOption));
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | // Get attribute name if available
|
---|
| 195 | if(parser.isSet(attributeNameOption)){
|
---|
| 196 | filters.setAttributeName(parser.value(attributeNameOption));
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | // Get attribute value if available
|
---|
| 200 | if(parser.isSet(attributeValueOption)){
|
---|
| 201 | filters.setAttributeValue(parser.value(attributeValueOption));
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // Check attribute filters
|
---|
| 205 | if(filters.getAttributeName()!="" && filters.getAttributeValue()==""){
|
---|
| 206 | UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-value option is required if using attribute-name option.");
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | if(filters.getAttributeValue()!="" && filters.getAttributeName()==""){
|
---|
| 210 | UtilXmlTools::displayErrorMessage("Parameter Parsing","attribute-name option is required if using attribute-value option.");
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | // Get files wildcard if available
|
---|
| 214 | if(parser.isSet(filesOption)){
|
---|
| 215 | filesWildCard=parser.value(filesOption);
|
---|
| 216 | }
|
---|
| 217 | else{
|
---|
| 218 | UtilXmlTools::displayErrorMessage("Parameter Parsing", "files option requires 1 value: <files> the XML files to process (wildcards supported).");
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | if(parser.isSet(elementNameOption)){
|
---|
| 222 | myXmlTools=new XmlTools(filesWildCard,filters,noBackups,noVerbose);
|
---|
| 223 | }
|
---|
| 224 | else{
|
---|
| 225 | myXmlTools=new XmlTools(filesWildCard,xPathExpression,noBackups,noVerbose);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 |
|
---|
| 229 | // Users wants an add-option?
|
---|
| 230 | if(parser.isSet(addValuesOption)){
|
---|
| 231 |
|
---|
| 232 | if(newVal==""){
|
---|
| 233 | UtilXmlTools::displayErrorMessage("Parameter Parsing", "add-value option requires 1 value: <new-val> the new values to add (space separated).");
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | myXmlTools->addValues(newVal);
|
---|
| 237 | }
|
---|
| 238 | else if(parser.isSet(removeValuesOption)){ // Or remove-values option?
|
---|
| 239 |
|
---|
| 240 | if(currentVal==""){
|
---|
| 241 | UtilXmlTools::displayErrorMessage("Parameter Parsing","remove-values option requires 1 value: <current-val> the current values to remove (space separated).");
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | myXmlTools->removeValues(currentVal);
|
---|
| 245 | }
|
---|
| 246 | else if(parser.isSet(replaceValueOption)){ // Or replace-value option?
|
---|
| 247 |
|
---|
| 248 | if(currentVal=="" || newVal==""){
|
---|
| 249 | UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-value option requires 2 values: <current-val> the current value and <new-val> the new value.");
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | myXmlTools->replaceValue(currentVal,newVal);
|
---|
| 253 | }
|
---|
| 254 | else if(parser.isSet(replaceAllValuesOption)){ // Or replace-all-values option?
|
---|
| 255 |
|
---|
| 256 | if(newVal=="" && positions==""){
|
---|
| 257 | UtilXmlTools::displayErrorMessage("Parameter Parsing","replace-all-values option requires 1 value: <new-val> the new value.\n" +
|
---|
| 258 | Util::toQString("It has also 1 optional value: <positions> the positions to replace (space separated and 0-index based)."));
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | myXmlTools->replaceAll(newVal,positions);
|
---|
| 262 | }
|
---|
| 263 | else if(parser.isSet(updateElementsOption)){ // Or update-elements option?
|
---|
| 264 |
|
---|
| 265 | if(diffOldNewVal==""){
|
---|
| 266 | UtilXmlTools::displayErrorMessage("Parameter Parsing","update-elements option requires 1 value: <diff-old-new-val> the difference between one current element "+
|
---|
| 267 | Util::toQString("value and one new element value (current value and new value must have the same position)."));
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | myXmlTools->updateElements(diffOldNewVal);
|
---|
| 271 | }
|
---|
| 272 | else if(parser.isSet(invertElementsOption)){ // Or invert-elements option?
|
---|
| 273 | myXmlTools->invertElements();
|
---|
| 274 | }
|
---|
| 275 | else{
|
---|
| 276 | UtilXmlTools::displayErrorMessage("Parameter Parsing","One operation is required to XmlTools continue. Possible operations are:\n"+
|
---|
| 277 | Util::toQString("--patch-files\n--add-values\n--remove-values\n--replace-value\n--replace-all-values\n--update-elements\n--invert-elements"));
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | delete myXmlTools;
|
---|
| 281 | }
|
---|