Last active
December 22, 2015 02:49
-
-
Save mrmoje/6406236 to your computer and use it in GitHub Desktop.
"Quick & dirty" command line parser for QT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QString QArgByKey(QString key, QChar sep = QChar('\0') ); //prototype usually in separate header | |
QString QArgByKey(QString key, QChar sep ) | |
{ | |
bool sepd=sep!=QChar('\0'); | |
int pos=sepd?qApp->arguments().indexOf(QRegExp('^'+key+sep+"\\S*")):qApp->arguments().indexOf(QRegExp(key)); | |
return pos==-1?QString::null: | |
(sepd?qApp->arguments().at(pos).split(sep).at(1):(++pos<qApp->arguments().size()?qApp->arguments().at(pos):QString::null)); | |
} | |
/* | |
USAGE | |
===== | |
command line args:- | |
user@box:~$ ./myApp firstKey=Value1 --secondKey Value2 thirdKey=val3.1,val3.2,val3.3 --enable-foo | |
Code:- | |
QString param1 = QArgByKey("firstkey",'='); // Returns `Value1` from first pair | |
QString param2 = QArgByKey("--secondkey"); // Returns `Value2` from second pair | |
QString param3-1 = QArgByKey("--thirdkey",'=').split(',').at(0); // Returns `val3.1` | |
bool fooEnabled = qApp->arguments().contains("--enable-foo"); //To check for `--enable-foo` | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment