Skip to content

Commit

Permalink
main.cpp: detect and bail out on unknown options (danmar#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Oct 29, 2022
1 parent fa8fd1a commit 3df6160
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,60 @@

int main(int argc, char **argv)
{
const char *filename = nullptr;
bool error = false;

// Settings..
const char *filename = nullptr;
simplecpp::DUI dui;
bool quiet = false;
for (int i = 1; i < argc; i++) {
const char * const arg = argv[i];
if (*arg == '-') {
bool found = false;
const char c = arg[1];
if (c != 'D' && c != 'U' && c != 'I' && c != 'i' && c != 's' && c != 'q')
continue; // Ignored
const char * const value = arg[2] ? (argv[i] + 2) : argv[++i];
switch (c) {
case 'D': // define symbol
dui.defines.push_back(value);
found = true;
break;
case 'U': // undefine symbol
dui.undefined.insert(value);
found = true;
break;
case 'I': // include path
dui.includePaths.push_back(value);
found = true;
break;
case 'i':
if (std::strncmp(arg, "-include=",9)==0)
if (std::strncmp(arg, "-include=",9)==0) {
dui.includes.push_back(arg+9);
found = true;
}
break;
case 's':
if (std::strncmp(arg, "-std=",5)==0)
if (std::strncmp(arg, "-std=",5)==0) {
dui.std = arg + 5;
found = true;
}
break;
case 'q':
quiet = true;
found = true;
break;
}
if (!found) {
std::cout << "Option '" << arg << "' is unknown." << std::endl;
error = true;
}
} else {
filename = arg;
}
}

if (error)
std::exit(1);

if (!filename) {
std::cout << "Syntax:" << std::endl;
std::cout << "simplecpp [options] filename" << std::endl;
Expand Down

0 comments on commit 3df6160

Please sign in to comment.