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*/
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"
3130using namespace std ;
3231using namespace dev ;
3332using 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
5036void version ()
5137{
@@ -61,7 +47,6 @@ to set locale to fail, so there are only two possible actions, the first is to
6147throw a runtime exception and cause the program to quit (default behaviour),
6248or the second is to modify the environment to something sensible (least
6349surprising behaviour).
64-
6550The follow code produces the least surprising behaviour. It will use the user
6651specified default locale if it is valid, and if not then it will modify the
6752environment 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