Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit 48da1c8

Browse files
committed
Catch exceptions from boost.program_options; user return from main instead of exit()
1 parent d645a78 commit 48da1c8

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

ethkey/main.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
This file is part of cpp-ethereum.
3+
34
cpp-ethereum is free software: you can redistribute it and/or modify
45
it under the terms of the GNU General Public License as published by
56
the Free Software Foundation, either version 3 of the License, or
@@ -8,6 +9,7 @@
89
but WITHOUT ANY WARRANTY; without even the implied warranty of
910
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1011
GNU General Public License for more details.
12+
1113
You should have received a copy of the GNU General Public License
1214
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
1315
*/
@@ -69,33 +71,43 @@ int main(int argc, char** argv)
6971
g_logVerbosity = 0;
7072
po::options_description generalOptions("General Options");
7173
generalOptions.add_options()
72-
("verbosity,v", po::value<int>(), "<0 - 9> Set the log verbosity from 0 to 9 (default: 8).")
73-
("version,V", "Show the version and exit.")
74-
("help,h", "Show this help message and exit.");
75-
po::parsed_options parsed = po::command_line_parser(argc, argv).options(generalOptions).allow_unregistered().run();
76-
vector<string> unrecognisedOptions = collect_unrecognized(parsed.options, po::include_positional);
74+
("verbosity,v", po::value<int>(), "<0 - 9> Set the log verbosity from 0 to 9 (default: 8).")
75+
("version,V", "Show the version and exit.")
76+
("help,h", "Show this help message and exit.");
77+
78+
po::variables_map vm;
79+
vector<string> unrecognisedOptions;
80+
try
81+
{
82+
po::parsed_options parsed = po::command_line_parser(argc, argv).options(generalOptions).allow_unregistered().run();
83+
unrecognisedOptions = collect_unrecognized(parsed.options, po::include_positional);
84+
po::store(parsed, vm);
85+
po::notify(vm);
86+
}
87+
catch (po::error const& e)
88+
{
89+
cerr << e.what();
90+
return -1;
91+
}
92+
7793
for (size_t i = 0; i < unrecognisedOptions.size(); ++i)
7894
{
7995
string arg = unrecognisedOptions[i];
80-
//cout << arg << " ";
81-
if (m.interpretOption(i, unrecognisedOptions)) {}
82-
else
96+
if (!m.interpretOption(i, unrecognisedOptions))
8397
{
8498
cerr << "Invalid argument: " << arg << endl;
85-
exit(-1);
99+
return -1;
86100
}
87101
}
88-
po::variables_map vm;
89-
po::store(parsed, vm);
90-
po::notify(vm);
102+
91103
if (vm.count("help"))
92104
{
93105
cout
94-
<< "Usage ethkey [OPTIONS]" << endl
95-
<< "Options:" << endl << endl;
106+
<< "Usage ethkey [OPTIONS]" << endl
107+
<< "Options:" << endl << endl;
96108
KeyCLI::streamHelp(cout);
97109
cout << generalOptions;
98-
exit(0);
110+
return 0;
99111
}
100112
if (vm.count("version"))
101113
version();

0 commit comments

Comments
 (0)