1
0
Fork 0
mirror of https://gitlab.com/Syping/mayu synced 2024-11-25 05:20:23 +01:00

argument parser improved

This commit is contained in:
Syping 2018-05-16 02:37:03 +02:00
parent e06f1288da
commit dd01afacf6

View file

@ -35,26 +35,43 @@ int main(int argc, char *argv[])
mayuMode a_mode = mayuMode::Ping; mayuMode a_mode = mayuMode::Ping;
for (int i = arguments.length(); i > 0; i--) { for (int i = arguments.length(); i > 0; i--) {
const QString &argument = arguments.at(i-1); const QString &argument = arguments.at(i-1);
if (argument == "-p" || argument == "--ping") { if (argument.left(2) == "--") {
a_mode = mayuMode::Ping; if (argument == "--ping") {
arguments.removeAt(i-1); a_mode = mayuMode::Ping;
arguments.removeAt(i-1);
}
else if (argument == "--resolve") {
a_mode = mayuMode::Resolve;
arguments.removeAt(i-1);
}
else if (argument == "--clean") {
a_clean = true;
arguments.removeAt(i-1);
}
else {
QTextStream(stderr) << "Unknown Option found: " << argument << endl;
return 4;
}
} }
else if (argument == "-r" || argument == "--resolve") { else if (argument.left(1) == "-" && argument.length() != 1) {
a_mode = mayuMode::Resolve; for (int ii = argument.length(); ii > 0; ii--) {
arguments.removeAt(i-1); switch (argument.at(ii-1).cell()) {
} case 'p':
else if (argument == "-c" || argument == "--clean") { a_mode = mayuMode::Ping;
a_clean = true; break;
arguments.removeAt(i-1); case 'r':
} a_mode = mayuMode::Resolve;
else if (argument == "-pc" || argument == "-cp") { break;
a_mode = mayuMode::Ping; case 'c':
a_clean = true; a_clean = true;
arguments.removeAt(i-1); break;
} case '-':
else if (argument == "-rc" || argument == "-cr") { break;
a_mode = mayuMode::Resolve; default:
a_clean = true; QTextStream(stderr) << "Unknown Option found: " << argument.at(ii-1) << endl;
return 4;
}
}
arguments.removeAt(i-1); arguments.removeAt(i-1);
} }
} }
@ -67,7 +84,7 @@ int main(int argc, char *argv[])
return a_mayu.getResult(); return a_mayu.getResult();
} }
else { else {
QTextStream(stdout) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " [-c clean]" << " input.txt" << " output.json" << endl; QTextStream(stderr) << "Usage: " << a.arguments().at(0) << " [-p ping]" << " [-r resolve]" << " [-c clean]" << " input.txt" << " output.json" << endl;
} }
return 0; return 0;