-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Better command line argument handling #3628 #4597
Changes from 1 commit
d7721df
e5917bd
a4cfdec
f20ff93
5f23851
0febf63
eb4f786
eb1cd28
7ce58ad
af0d89a
691d0e6
bf58fff
c1f13cb
5612846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| add_executable(ethkey KeyAux.h main.cpp) | ||
| target_link_libraries(ethkey PRIVATE ethcore devcore) | ||
| target_link_libraries(ethkey PRIVATE ethcore devcore Boost::program_options) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,12 +26,16 @@ | |
| #include <libdevcore/FileSystem.h> | ||
| #include <libdevcore/Log.h> | ||
| #include <libethcore/KeyManager.h> | ||
| #include <boost/program_options.hpp> | ||
| #include <boost/program_options/options_description.hpp> | ||
| #include "BuildInfo.h" | ||
| #include "KeyAux.h" | ||
| using namespace std; | ||
| using namespace dev; | ||
| using namespace dev::eth; | ||
|
|
||
| namespace po = boost::program_options; | ||
|
|
||
| void help() | ||
| { | ||
| cout | ||
|
|
@@ -82,23 +86,48 @@ int main(int argc, char** argv) | |
| setDefaultOrCLocale(); | ||
| KeyCLI m(KeyCLI::OperationMode::ListBare); | ||
| g_logVerbosity = 0; | ||
|
|
||
| po::options_description generalOptions("General Options"); | ||
| generalOptions.add_options() | ||
| ("verbosity,v", po::value<string>(), "<0 - 9> Set the log verbosity from 0 to 9 (default: 8).") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't we make it |
||
| ("version,V", "Show the version and exit.") | ||
| ("help,h", "Show this help message and exit.") | ||
| ; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This formatting looks weird in our style, please leave semicolon on the last line of the statement. |
||
| int ac = 1; | ||
| for (int i = 1; i < argc; ++i) | ||
| { | ||
| string arg = argv[i]; | ||
| if (m.interpretOption(i, argc, argv)) {} | ||
| else if ((arg == "-v" || arg == "--verbosity") && i + 1 < argc) | ||
| g_logVerbosity = atoi(argv[++i]); | ||
| else if (arg == "-h" || arg == "--help") | ||
| help(); | ||
| else if (arg == "-V" || arg == "--version") | ||
| version(); | ||
| else if (((arg == "-v" || arg == "--verbosity") && i + 1 < argc) || arg == "-h" || arg == "--help" || arg == "-V" || arg == "--version") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opening brace should be on a separate line |
||
| argv[ac] = argv[i]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain what are you trying to achieve in this part? I think you're trying to get rid of all the options that are handled in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ac++; | ||
| if (arg == "-v" || arg == "--verbosity") { | ||
| i++; | ||
| argv[ac] = argv[i]; | ||
| ac++; | ||
| } | ||
| continue; | ||
| } | ||
| else | ||
| { | ||
| cerr << "Invalid argument: " << arg << endl; | ||
| exit(-1); | ||
| } | ||
| } | ||
| po::variables_map vm; | ||
| po::store(po::parse_command_line(ac, argv, generalOptions), vm); | ||
| po::notify(vm); | ||
| if (vm.count("help")) { | ||
| cout | ||
| << "Usage ethkey [OPTIONS]" << endl | ||
| << "Options:" << endl << endl; | ||
| KeyCLI::streamHelp(cout); | ||
| cout << generalOptions; | ||
| exit(0); | ||
| } | ||
| if (vm.count("version")) | ||
| version(); | ||
| if (vm.count("verbosity")) | ||
| g_logVerbosity = atoi(vm["verbosity"].as<string>().c_str()); | ||
|
|
||
| m.execute(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this function?