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

Commit 12e0928

Browse files
demon1999gumb0
authored andcommitted
Use boost.program_options to handle command line arguments in eth and ethkey
1 parent c1cf074 commit 12e0928

File tree

7 files changed

+581
-503
lines changed

7 files changed

+581
-503
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ include(EthExecutableHelper)
9898
include(EthDependencies)
9999
include(EthUtils)
100100

101-
hunter_add_package(Boost COMPONENTS filesystem system thread)
102-
find_package(Boost CONFIG REQUIRED filesystem system thread)
101+
hunter_add_package(Boost COMPONENTS program_options filesystem system thread)
102+
find_package(Boost CONFIG REQUIRED program_options filesystem system thread)
103103

104104
hunter_add_package(jsoncpp)
105105
find_package(jsoncpp CONFIG REQUIRED)

eth/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set(
88
add_executable(eth ${sources})
99
target_link_libraries(
1010
eth
11-
PRIVATE ethereum ethashseal evm web3jsonrpc webthree devcore
11+
PRIVATE ethereum ethashseal evm web3jsonrpc webthree devcore Boost::program_options
1212
)
1313

1414
target_include_directories(eth PRIVATE ../utils)

eth/MinerAux.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ class MinerCLI
101101
BasicAuthority::init();
102102
}
103103

104-
bool interpretOption(int& i, int argc, char** argv)
104+
bool interpretOption(size_t& i, vector<string> const& argv)
105105
{
106+
size_t argc = argv.size();
106107
string arg = argv[i];
107108
if (arg == "--benchmark-warmup" && i + 1 < argc)
108109
try {

eth/main.cpp

Lines changed: 542 additions & 468 deletions
Large diffs are not rendered by default.

ethkey/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
add_executable(ethkey KeyAux.h main.cpp)
2-
target_link_libraries(ethkey PRIVATE ethcore devcore)
2+
target_link_libraries(ethkey PRIVATE ethcore devcore Boost::program_options)

ethkey/KeyAux.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ class KeyCLI
107107

108108
KeyCLI(OperationMode _mode = OperationMode::None): m_mode(_mode) {}
109109

110-
bool interpretOption(int& i, int argc, char** argv)
110+
bool interpretOption(size_t& i, vector<string> const& argv)
111111
{
112+
size_t argc = argv.size();
112113
string arg = argv[i];
113114
if (arg == "--wallet-path" && i + 1 < argc)
114115
m_walletPath = argv[++i];

ethkey/main.cpp

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
/*
22
This file is part of cpp-ethereum.
3-
43
cpp-ethereum is free software: you can redistribute it and/or modify
54
it under the terms of the GNU General Public License as published by
65
the Free Software Foundation, either version 3 of the License, or
76
(at your option) any later version.
8-
97
cpp-ethereum is distributed in the hope that it will be useful,
108
but WITHOUT ANY WARRANTY; without even the implied warranty of
119
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1210
GNU General Public License for more details.
13-
1411
You should have received a copy of the GNU General Public License
1512
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
1613
*/
@@ -26,26 +23,15 @@
2623
#include <libdevcore/FileSystem.h>
2724
#include <libdevcore/Log.h>
2825
#include <libethcore/KeyManager.h>
26+
#include <boost/program_options.hpp>
27+
#include <boost/program_options/options_description.hpp>
2928
#include "BuildInfo.h"
3029
#include "KeyAux.h"
3130
using namespace std;
3231
using namespace dev;
3332
using namespace dev::eth;
3433

35-
void help()
36-
{
37-
cout
38-
<< "Usage ethkey [OPTIONS]" << endl
39-
<< "Options:" << endl << endl;
40-
KeyCLI::streamHelp(cout);
41-
cout
42-
<< "General Options:" << endl
43-
<< " -v,--verbosity <0 - 9> Set the log verbosity from 0 to 9 (default: 8)." << endl
44-
<< " -V,--version Show the version and exit." << endl
45-
<< " -h,--help Show this help message and exit." << endl
46-
;
47-
exit(0);
48-
}
34+
namespace po = boost::program_options;
4935

5036
void version()
5137
{
@@ -61,7 +47,6 @@ to set locale to fail, so there are only two possible actions, the first is to
6147
throw a runtime exception and cause the program to quit (default behaviour),
6248
or the second is to modify the environment to something sensible (least
6349
surprising behaviour).
64-
6550
The follow code produces the least surprising behaviour. It will use the user
6651
specified default locale if it is valid, and if not then it will modify the
6752
environment the process is running in to use a sensible default. This also means
@@ -82,25 +67,42 @@ int main(int argc, char** argv)
8267
setDefaultOrCLocale();
8368
KeyCLI m(KeyCLI::OperationMode::ListBare);
8469
g_logVerbosity = 0;
85-
86-
for (int i = 1; i < argc; ++i)
70+
po::options_description generalOptions("General Options");
71+
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);
77+
for (size_t i = 0; i < unrecognisedOptions.size(); ++i)
8778
{
88-
string arg = argv[i];
89-
if (m.interpretOption(i, argc, argv)) {}
90-
else if ((arg == "-v" || arg == "--verbosity") && i + 1 < argc)
91-
g_logVerbosity = atoi(argv[++i]);
92-
else if (arg == "-h" || arg == "--help")
93-
help();
94-
else if (arg == "-V" || arg == "--version")
95-
version();
79+
string arg = unrecognisedOptions[i];
80+
//cout << arg << " ";
81+
if (m.interpretOption(i, unrecognisedOptions)) {}
9682
else
9783
{
9884
cerr << "Invalid argument: " << arg << endl;
9985
exit(-1);
10086
}
10187
}
88+
po::variables_map vm;
89+
po::store(parsed, vm);
90+
po::notify(vm);
91+
if (vm.count("help"))
92+
{
93+
cout
94+
<< "Usage ethkey [OPTIONS]" << endl
95+
<< "Options:" << endl << endl;
96+
KeyCLI::streamHelp(cout);
97+
cout << generalOptions;
98+
exit(0);
99+
}
100+
if (vm.count("version"))
101+
version();
102+
if (vm.count("verbosity"))
103+
g_logVerbosity = vm["verbosity"].as<int>();
102104

103105
m.execute();
104106

105107
return 0;
106-
}
108+
}

0 commit comments

Comments
 (0)