c++ - Extract parameters from string, included quoted regions, in Qt -
I have a Qt5 / C ++ program which receives a program name and possibly a parameter with a parameter. I need to split this QString into multiple stars, for example, string
/ tmp / myprog - at least 1 - prefix 2 = 2 - prefix 3 = "1 2 3 "- p 4
[2] = - param2 = 2 list [3] = - the ultimate 3 =" 1 2 3 "list [4] = - P4 < / Pre>
My first thought was to use the "partition" method on space, but it will be broken which will be broken up to the 3 which is cited. As the parameter 4 does not have: or between = -p and 4
Is there an easy way to do this? I suspect that it is impossible to split P4 easily because there is no way to know that 2 items get together at the same time.
But is there a division / reggae / other way for the above partition? (If we ignore the -p 4 then is it possible?)
Update:
Is the same function string split-without split or -p is? Therefore a string of
abc "aa" "bbb"
will be created
list [0] = [ 2] = "BBB" list [3] = c
text "> Your task has two sides:
-
Command line in arguments It is necessary to implement it with scratch.
-
To process the arguments and the values of their arguments from Qt 5.2, you can do this to
QCommandLineParser
.
#include & lt; QCoreApplication & gt; # Include & lt; QCommandLineParser & gt; #include & lt; QDebug & gt; Fixed QStringList splitCommandLine (ConstestString & CMDline) {QStringList list; QString arg; Bool escape = false; nim {idol, erg, quatated erg state} = Inactive; Forchha (QChar const C, CMD line) {If (! Escape & amp; C == '\\') {escape = true; Continue;} Switch (state) {Case Idol: If (! Escape Amp; C == '' '') state = quotedRG; otherwise (saved ||! C.isSpace ()) {arg + = c; State = Arg;} break; Case erg: if (! Escape and c == '' '') state = coated AR; And if (saved ||! CisSpace ()) arg + = c; Other {list & lt; & Lt; Arg; Arg.clear (); State = inactive; } break; Case QuotedArg: (! Escape & amp; C == '' '') if state = arg.isEmpty () is disabled:?! Arg; Any other arg = c; Break;} escape = false;} if (arg.isEmpty ()) list is & lt; & Lt; Arg; Return list;} integer main (int argc, char * argv []) {QCoreApplication application (argc, argv); QCommandLineParser Parser; Parser.addHelpOption (); QCommandLineOption param1 ("param1"); QCommandLineOption param2 ("param2", "", "val2"); QCommandLineOption param3 ("param3", "", "val3"); QCommandLineOption param4 ("p", "", "val4"); Parser.addOption (param1); Parser.addOption (param2); Parser.addOption (param3); Parser.addOption (param4); If (true) {// a string QString cmdline ("parse / tmp / myprog --param1 --param2 = 2 \\ 2 --param3 = \" 1 2 3 \ "-p 4"); Parser.parse (splitCommandLine (cmdline)); else else // a command line this application parser.process (pars passed for the application);} if (parser.iset (param 1)) qDebug () & lt; & Lt; "Param1"; If (parser.iset (ultimate 2)) qDebug () & lt; & Lt; "Ultimate 2:" & lt; & Lt; Parser.value (param2); If (parser.iset (para3)) {Custostelist value = parser.value (Ultimate 3). Split ('', Caststring :: Public Displayparts); QDebug () & lt; & Lt; "Ultimate 3:" & lt; & Lt; Values; } If (parser.iset (parameter4)) qDebug () & lt; & Lt; "Param 4:" & lt; & Lt; Parser.value (param4); Return 0; }
Output:
param1 param2: "2 2" param 3: ("1", "2", "3") param4: 4 "
QDebug
string outputs that quote string has no quotes.
Comments
Post a Comment