Browse Source

Add support for option argument

QGC4.4
Don Gagne 11 years ago
parent
commit
3441496030
  1. 17
      src/CmdLineOptParser.cc
  2. 6
      src/CmdLineOptParser.h

17
src/CmdLineOptParser.cc

@ -39,13 +39,24 @@ void ParseCmdLineOptions(int& argc, ///< count of ar
{ {
// Start with all options off // Start with all options off
for (size_t iOption=0; iOption<cOpts; iOption++) { for (size_t iOption=0; iOption<cOpts; iOption++) {
*prgOpts[iOption].flag = false; *prgOpts[iOption].optionFound = false;
} }
for (int iArg=1; iArg<argc; iArg++) { for (int iArg=1; iArg<argc; iArg++) {
for (size_t iOption=0; iOption<cOpts; iOption++) { for (size_t iOption=0; iOption<cOpts; iOption++) {
if (QString(argv[iArg]).compare(prgOpts[iOption].optionStr, Qt::CaseInsensitive) == 0) { bool found = false;
*prgOpts[iOption].flag = true;
QString arg(argv[iArg]);
QString optionStr(prgOpts[iOption].optionStr);
if (arg.startsWith(QString("%1:").arg(optionStr), Qt::CaseInsensitive)) {
found = true;
prgOpts[iOption].optionArg = arg.right(arg.length() - (optionStr.length() + 1));
} else if (arg.compare(optionStr, Qt::CaseInsensitive) == 0) {
found = true;
}
if (found) {
*prgOpts[iOption].optionFound = true;
if (removeParsedOptions) { if (removeParsedOptions) {
for (int iShift=iArg; iShift<argc-1; iShift++) { for (int iShift=iArg; iShift<argc-1; iShift++) {
argv[iShift] = argv[iShift+1]; argv[iShift] = argv[iShift+1];

6
src/CmdLineOptParser.h

@ -29,12 +29,14 @@
#ifndef CMDLINEOPTPARSER_H #ifndef CMDLINEOPTPARSER_H
#define CMDLINEOPTPARSER_H #define CMDLINEOPTPARSER_H
#include <QString>
#include <cstring> #include <cstring>
/// @brief Structure used to pass command line options to the ParseCmdLineOptions function. /// @brief Structure used to pass command line options to the ParseCmdLineOptions function.
typedef struct { typedef struct {
const char* optionStr; ///< command line option, for example "--foo" const char* optionStr; ///< command line option, for example "--foo"
bool* flag; ///< if option is found this variable will be set to true bool* optionFound; ///< if option is found this variable will be set to true
QString optionArg; ///< Option has additional argument, form is option:arg
} CmdLineOpt_t; } CmdLineOpt_t;
void ParseCmdLineOptions(int& argc, void ParseCmdLineOptions(int& argc,

Loading…
Cancel
Save