1
0
mirror of https://github.com/pdf2htmlEX/pdf2htmlEX.git synced 2024-12-22 04:50:09 +00:00

working on arg parsing

This commit is contained in:
Lu Wang 2012-09-10 22:44:19 +08:00
parent 03779406b8
commit d49dbc3deb
2 changed files with 22 additions and 6 deletions

View File

@ -76,9 +76,9 @@ void ArgParser::parse(int argc, char ** argv) const
longopts.push_back({0,0,0,0}); longopts.push_back({0,0,0,0});
{ {
opterr = 1;
int r; int r;
int idx; int idx;
opterr = 0;
while(true) while(true)
{ {
r = getopt_long(argc, argv, &optstring.front(), &longopts.front(), &idx); r = getopt_long(argc, argv, &optstring.front(), &longopts.front(), &idx);
@ -87,9 +87,7 @@ void ArgParser::parse(int argc, char ** argv) const
assert(r != ':'); assert(r != ':');
if(r == '?') if(r == '?')
{ {
ostringstream sout; throw "";
assert(optopt < 256);
throw string() + ((opt_map.find(optopt) == opt_map.end()) ? "Unknown option: -" : "Missing argument for option -") + (char)optopt;
} }
auto iter = opt_map.find(r); auto iter = opt_map.find(r);

View File

@ -89,10 +89,24 @@ void parse_options (int argc, char **argv)
{ {
argparser.parse(argc, argv); argparser.parse(argc, argv);
} }
catch(const char * s)
{
// if s == "", getopt_long would have printed the error message
if(s && s[0])
{
cerr << "Error when parsing the arguments:" << endl;
cerr << s << endl;
}
exit(EXIT_FAILURE);
}
catch(const std::string & s) catch(const std::string & s)
{ {
cerr << "Error when parsing the arguments:" << endl; // if s == "", getopt_long would have printed the error message
cerr << s << endl; if(s != "")
{
cerr << "Error when parsing the arguments:" << endl;
cerr << s << endl;
}
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -181,6 +195,10 @@ int main(int argc, char **argv)
finished = true; finished = true;
} }
catch (const char * s)
{
cerr << "Error: " << s << endl;
}
catch (const string & s) catch (const string & s)
{ {
cerr << "Error: " << s << endl; cerr << "Error: " << s << endl;