#include "commandlineparser.h" CommandLineParseResult parseCommandLine2(QCommandLineParser &parser, TcQuery *query, QString *errorMessage) { //添加命令 parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); QCommandLineOption helpOption = parser.addHelpOption();//Adds the help option (-h, --help and -? on Windows) This option is handled automatically by QCommandLineParser. QCommandLineOption versionOption = parser.addVersionOption();//This option is handled automatically by QCommandLineParser. QCommandLineOption serialPort("serialPort", "Serial port.", "serialPort"); parser.addOption(serialPort); QCommandLineOption connectBaudrate("connectBaudrate", "ConnectBaudrate of wit motion.", "connectBaudrate"); parser.addOption(connectBaudrate); QCommandLineOption algorithm("algorithm", "Algorithm of wit motion.", "algorithm"); parser.addOption(algorithm); QCommandLineOption installationOrientation("installationOrientation", "InstallationOrientation of wit motion.", "installationOrientation"); parser.addOption(installationOrientation); QCommandLineOption timeZone("timeZone", "TimeZone of wit motion.", "timeZone"); parser.addOption(timeZone); // A boolean option with multiple names (-r, --record) QCommandLineOption isSetDefaultReturnContent(QStringList() << "d" << "isSetDefaultReturnContent", "isSetDefaultReturnContent"); parser.addOption(isSetDefaultReturnContent); QCommandLineOption baudrate("baudrate", "Baudrate of wit motion.", "baudrate"); parser.addOption(baudrate); QCommandLineOption returnRate("returnRate", "ReturnRate of wit motion.", "returnRate"); parser.addOption(returnRate); QCommandLineOption deviceAddress("deviceAddress", "DeviceAddress of wit motion.", "deviceAddress"); parser.addOption(deviceAddress); QCommandLineOption modelForInterface_D0("modelForInterface_D0", "modelForInterface_D0 of interface number.", "modelForInterface_D0"); parser.addOption(modelForInterface_D0); QCommandLineOption pulseWidth_D0("pulseWidth_D0", "PulseWidth_D0 of interface number.", "pulseWidth_D0"); pulseWidth_D0.setDefaultValue("0");//设置默认参数 parser.addOption(pulseWidth_D0); QCommandLineOption period_D0("period_D0", "Period_D0 of wit interface number.", "period_D0"); period_D0.setDefaultValue("0");//设置默认参数 parser.addOption(period_D0); QCommandLineOption modelForInterface_D1("modelForInterface_D1", "modelForInterface_D1 of interface number.", "modelForInterface_D1"); parser.addOption(modelForInterface_D1); QCommandLineOption pulseWidth_D1("pulseWidth_D1", "PulseWidth_D1 of interface number.", "pulseWidth_D1"); pulseWidth_D1.setDefaultValue("0");//设置默认参数 parser.addOption(pulseWidth_D1); QCommandLineOption period_D1("period_D1", "Period_D1 of wit interface number.", "period_D1"); period_D1.setDefaultValue("0");//设置默认参数 parser.addOption(period_D1); QCommandLineOption modelForInterface_D2("modelForInterface_D2", "modelForInterface_D2 of interface number.", "modelForInterface_D2"); parser.addOption(modelForInterface_D2); QCommandLineOption pulseWidth_D2("pulseWidth_D2", "PulseWidth_D2 of interface number.", "pulseWidth_D2"); pulseWidth_D2.setDefaultValue("0");//设置默认参数 parser.addOption(pulseWidth_D2); QCommandLineOption period_D2("period_D2", "Period_D2 of wit interface number.", "period_D2"); period_D2.setDefaultValue("0");//设置默认参数 parser.addOption(period_D2); QCommandLineOption modelForInterface_D3("modelForInterface_D3", "modelForInterface_D3 of interface number.", "modelForInterface_D3"); parser.addOption(modelForInterface_D3); QCommandLineOption pulseWidth_D3("pulseWidth_D3", "PulseWidth_D3 of interface number.", "pulseWidth_D3"); pulseWidth_D3.setDefaultValue("0");//设置默认参数 parser.addOption(pulseWidth_D3); QCommandLineOption period_D3("period_D3", "Period_D3 of wit interface number.", "period_D3"); period_D3.setDefaultValue("0");//设置默认参数 parser.addOption(period_D3); // QCommandLineOption rlx1("rlx1", "rlx1 parm of wit motion.", "rlx1"); // parser.addOption(rlx1); QCommandLineOption rlx1(QStringList() << "rlx1" << "rlx1 long", "rlx1 parm of wit motion."); parser.addOption(rlx1); //解析命令 if (!parser.parse(QCoreApplication::arguments()))//Process the actual command line arguments given by the user { *errorMessage = parser.errorText(); return CommandLineError; } if (parser.isSet(versionOption)) return CommandLineVersionRequested; if (parser.isSet(helpOption)) return CommandLineHelpRequested; if (parser.isSet(serialPort)) { const QString serialPortTmp = parser.value(serialPort); query->serialPort = serialPortTmp; } else//默认参数 { *errorMessage = "No serialPort set."; return CommandLineError; } if (parser.isSet(connectBaudrate)) { const QString connectBaudrateTmp = parser.value(connectBaudrate); query->connectBaudrate = connectBaudrateTmp.toInt(); } else//默认参数 { *errorMessage = "No connectBaudrate set."; return CommandLineError; } if (parser.isSet(algorithm)) { const QString algorithmTmp1 = parser.value(algorithm); int algorithmTmp2 = algorithmTmp1.toInt(); switch (algorithmTmp2) { case 9: query->algorithm = ALGROITHM9; query->isSetAlgorithm = true; break; case 6: query->algorithm = ALGROITHM6; query->isSetAlgorithm = true; break; default: *errorMessage = "Algorithm set error."; return CommandLineError; break; } } else//默认参数 { query->isSetAlgorithm = false; } if (parser.isSet(installationOrientation)) { const QString installationOrientationTmp1 = parser.value(installationOrientation); int installationOrientationTmp2 = installationOrientationTmp1.toInt(); switch (installationOrientationTmp2) { case 0: query->installationOrientation = ORIENT_HORIZONTAL; query->isSetInstallationOrientation = true; break; case 1: query->installationOrientation = ORIENT_VERTICAL; query->isSetInstallationOrientation = true; break; default: *errorMessage = "InstallationOrientation set error."; return CommandLineError; break; } } else//默认参数 { query->isSetInstallationOrientation = false; } if (parser.isSet(timeZone)) { const QString timeZoneTmp1 = parser.value(timeZone); int timeZoneTmp2 = timeZoneTmp1.toInt(); switch (timeZoneTmp2) { case 0: query->timeZone = UTC_N12; query->isSetTimeZone = true; break; case 1: query->timeZone = UTC_N11; query->isSetTimeZone = true; break; case 2: query->timeZone = UTC_N10; query->isSetTimeZone = true; break; case 3: query->timeZone = UTC_N9; query->isSetTimeZone = true; break; case 4: query->timeZone = UTC_N8; query->isSetTimeZone = true; break; case 5: query->timeZone = UTC_N7; query->isSetTimeZone = true; break; case 6: query->timeZone = UTC_N6; query->isSetTimeZone = true; break; case 7: query->timeZone = UTC_N5; query->isSetTimeZone = true; break; case 8: query->timeZone = UTC_N4; query->isSetTimeZone = true; break; case 9: query->timeZone = UTC_N3; query->isSetTimeZone = true; break; case 10: query->timeZone = UTC_N2; query->isSetTimeZone = true; break; case 11: query->timeZone = UTC_N1; query->isSetTimeZone = true; break; case 12: query->timeZone = UTC; query->isSetTimeZone = true; break; case 13: query->timeZone = UTC_N1; query->isSetTimeZone = true; break; case 14: query->timeZone = UTC_N2; query->isSetTimeZone = true; break; case 15: query->timeZone = UTC_N3; query->isSetTimeZone = true; break; case 16: query->timeZone = UTC_N4; query->isSetTimeZone = true; break; case 17: query->timeZone = UTC_N5; query->isSetTimeZone = true; break; case 18: query->timeZone = UTC_N6; query->isSetTimeZone = true; break; case 19: query->timeZone = UTC_N7; query->isSetTimeZone = true; break; case 20: query->timeZone = UTC_N8; query->isSetTimeZone = true; break; case 21: query->timeZone = UTC_N9; query->isSetTimeZone = true; break; case 22: query->timeZone = UTC_N10; query->isSetTimeZone = true; break; case 23: query->timeZone = UTC_N11; query->isSetTimeZone = true; break; case 24: query->timeZone = UTC_N12; query->isSetTimeZone = true; break; default: *errorMessage = "timeZone set error."; return CommandLineError; break; } } else//默认参数 { query->isSetTimeZone = false; } query->isSetDefaultReturnContent = parser.isSet(isSetDefaultReturnContent); if (parser.isSet(baudrate)) { const QString baudrateTmp1 = parser.value(baudrate); int baudrateTmp2 = baudrateTmp1.toInt(); switch (baudrateTmp2) { case 2400: query->baudrate = WIT_BAUD_2400; query->isSetBaudrate = true; break; case 4800: query->baudrate = WIT_BAUD_4800; query->isSetBaudrate = true; break; case 9600: query->baudrate = WIT_BAUD_9600; query->isSetBaudrate = true; break; case 19200: query->baudrate = WIT_BAUD_19200; query->isSetBaudrate = true; break; case 38400: query->baudrate = WIT_BAUD_38400; query->isSetBaudrate = true; break; case 57600: query->baudrate = WIT_BAUD_57600; query->isSetBaudrate = true; break; case 115200: query->baudrate = WIT_BAUD_115200; query->isSetBaudrate = true; break; case 230400: query->baudrate = WIT_BAUD_230400; query->isSetBaudrate = true; break; case 460800: query->baudrate = WIT_BAUD_460800; query->isSetBaudrate = true; break; case 921600: query->baudrate = WIT_BAUD_921600; query->isSetBaudrate = true; break; default: *errorMessage = "baudrate set error."; return CommandLineError; break; } } else//默认参数 { query->isSetBaudrate = false; } if (parser.isSet(returnRate)) { const QString returnRateTmp1 = parser.value(returnRate); int returnRateTmp2 = returnRateTmp1.toInt(); switch (returnRateTmp2) { // case 0.1: // query->returnRate = RRATE_01HZ; // query->isSetReturnRate = true; // break; // case 0.5: // query->returnRate = RRATE_05HZ; // query->isSetReturnRate = true; // break; case 1: query->returnRate = RRATE_1HZ; query->isSetReturnRate = true; break; case 2: query->returnRate = RRATE_2HZ; query->isSetReturnRate = true; break; case 5: query->returnRate = RRATE_5HZ; query->isSetReturnRate = true; break; case 10: query->returnRate = RRATE_10HZ; query->isSetReturnRate = true; break; case 20: query->returnRate = RRATE_20HZ; query->isSetReturnRate = true; break; case 50: query->returnRate = RRATE_50HZ; query->isSetReturnRate = true; break; case 100: query->returnRate = RRATE_100HZ; query->isSetReturnRate = true; break; // case "none": // query->returnRate = RRATE_NONE; // query->isSetReturnRate = true; // break; case 200: query->returnRate = RRATE_200HZ; query->isSetReturnRate = true; break; // case "once": // query->returnRate = RRATE_ONCE; // query->isSetReturnRate = true; // break; default: *errorMessage = "timeZone set error."; return CommandLineError; break; } } else//默认参数 { query->isSetReturnRate = false; } if (parser.isSet(deviceAddress)) { const QString deviceAddressTmp = parser.value(deviceAddress); query->deviceAddress = deviceAddressTmp.toInt(); query->isSetDeviceAddress = true; } else//默认参数 { query->isSetDeviceAddress = false; } bool ret; ret=processModelParm(parser,modelForInterface_D0,query->modelForInterface_D0,query->isSetInterface_D0); if(!ret) { *errorMessage = "modelForInterface_D0 set error."; return CommandLineError; } ret=processPulseWidthParm(parser,pulseWidth_D0, query->pulseWidth_D0, query->isSetInterface_D0); ret=processPeriodParm(parser,period_D0, query->period_D0, query->isSetInterface_D0); ret=processModelParm_D1(parser,modelForInterface_D1,query->modelForInterface_D1,query->isSetInterface_D1); if(!ret) { *errorMessage = "modelForInterface_D1 set error."; return CommandLineError; } ret=processPulseWidthParm(parser,pulseWidth_D1, query->pulseWidth_D1, query->isSetInterface_D1); ret=processPeriodParm(parser,period_D1, query->period_D1, query->isSetInterface_D1); ret=processModelParm(parser,modelForInterface_D2,query->modelForInterface_D2,query->isSetInterface_D2); if(!ret) { *errorMessage = "modelForInterface_D2 set error."; return CommandLineError; } ret=processPulseWidthParm(parser,pulseWidth_D2, query->pulseWidth_D2, query->isSetInterface_D2); ret=processPeriodParm(parser,period_D2, query->period_D2, query->isSetInterface_D2); ret=processModelParm(parser,modelForInterface_D3,query->modelForInterface_D3,query->isSetInterface_D3); if(!ret) { *errorMessage = "modelForInterface_D3 set error."; return CommandLineError; } ret=processPulseWidthParm(parser,pulseWidth_D3, query->pulseWidth_D3, query->isSetInterface_D3); ret=processPeriodParm(parser,period_D3, query->period_D3, query->isSetInterface_D3); if (parser.isSet(rlx1)) { query->isSetAlgorithm = true; query->algorithm = ALGROITHM9; query->isSetInstallationOrientation = true; query->installationOrientation = ORIENT_HORIZONTAL; query->isSetDefaultReturnContent = true; query->isSetBaudrate = true; query->baudrate = WIT_BAUD_115200; query->isSetReturnRate = true; query->returnRate = RRATE_10HZ; query->isSetInterface_D1 = true; query->modelForInterface_D1 = AIN_D1; query->pulseWidth_D1 = 0; query->period_D1 = 0; query->isSetInterface_D2 = true; query->modelForInterface_D2 = PWM; query->pulseWidth_D2 = 1500; query->period_D2 = 20000; query->isSetInterface_D3 = true; query->modelForInterface_D3 = PWM; query->pulseWidth_D3 = 1500; query->period_D3 = 20000; query->isSetReturnRate= true; query->returnRate=RRATE_20HZ; } return CommandLineOk; } bool getModel(int modelIndex, MODEL_D0_D2_D3_ENUM & model) { switch (modelIndex) { case 0: model = AIN; return true; case 1: model = DIN; return true; case 2: model = DOH; return true; case 3: model = DOL; return true; case 4: model = PWM; return true; default: return false; } } bool getModel_D1(int modelIndex, MODEL_D1_ENUM & model) { switch (modelIndex) { case 0: model = AIN_D1; return true; case 1: model = DIN_D1; return true; case 2: model = DOH_D1; return true; case 3: model = DOL_D1; return true; case 4: model = PWM_D1; return true; case 5: model = CLR_D1; return true; default: return false; } } bool processModelParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, MODEL_D0_D2_D3_ENUM & model, bool &isSetInterface) { if(parser.isSet(commandLineOption)) { const QString modelForInterface_D0Tmp1 = parser.value(commandLineOption); int modelForInterface_D0Tmp2 = modelForInterface_D0Tmp1.toInt(); bool ret = getModel(modelForInterface_D0Tmp2, model); if(ret) { isSetInterface = true; } else { return false; } } else { isSetInterface = false; } return true; } bool processModelParm_D1(QCommandLineParser &parser, QCommandLineOption commandLineOption, MODEL_D1_ENUM & model, bool &isSetInterface) { if(parser.isSet(commandLineOption)) { const QString modelForInterface_D0Tmp1 = parser.value(commandLineOption); int modelForInterface_D0Tmp2 = modelForInterface_D0Tmp1.toInt(); bool ret = getModel_D1(modelForInterface_D0Tmp2, model); if(ret) { isSetInterface = true; } else { return false; } } else { isSetInterface = false; } return true; } bool processPulseWidthParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, int & pulseWidth, bool &isSetInterface) { if (isSetInterface && parser.isSet(commandLineOption)) { const QString pulseWidth_D0Tmp = parser.value(commandLineOption); pulseWidth = pulseWidth_D0Tmp.toInt(); isSetInterface = true; } else//默认参数 { QStringList tmp = commandLineOption.defaultValues(); pulseWidth = tmp[0].toInt(); isSetInterface = false; } return true; } bool processPeriodParm(QCommandLineParser &parser, QCommandLineOption commandLineOption, int & period, bool &isSetInterface) { if (isSetInterface && parser.isSet(commandLineOption)) { const QString period_Tmp = parser.value(commandLineOption); period = period_Tmp.toInt(); } else//默认参数 { QStringList tmp = commandLineOption.defaultValues(); period = tmp[0].toInt(); isSetInterface = false; } return true; }