main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file main.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. * Ethereum client.
  18. */
  19. #include <thread>
  20. #include <chrono>
  21. #include <clocale>
  22. #include <fstream>
  23. #include <iostream>
  24. #include <libdevcore/FileSystem.h>
  25. #include <libdevcore/Log.h>
  26. #include <libethcore/KeyManager.h>
  27. #include "cpp-ethereum/BuildInfo.h"
  28. #include "KeyAux.h"
  29. using namespace std;
  30. using namespace dev;
  31. using namespace dev::eth;
  32. void help()
  33. {
  34. cout
  35. << "Usage ethkey [OPTIONS]" << endl
  36. << "Options:" << endl << endl;
  37. KeyCLI::streamHelp(cout);
  38. cout
  39. << "General Options:" << endl
  40. << " -v,--verbosity <0 - 9> Set the log verbosity from 0 to 9 (default: 8)." << endl
  41. << " -V,--version Show the version and exit." << endl
  42. << " -h,--help Show this help message and exit." << endl
  43. ;
  44. exit(0);
  45. }
  46. void version()
  47. {
  48. cout << "ethkey version " << dev::Version << endl;
  49. cout << "Build: " << DEV_QUOTED(ETH_BUILD_PLATFORM) << "/" << DEV_QUOTED(ETH_BUILD_TYPE) << endl;
  50. exit(0);
  51. }
  52. /*
  53. The equivalent of setlocale(LC_ALL, “C”) is called before any user code is run.
  54. If the user has an invalid environment setting then it is possible for the call
  55. to set locale to fail, so there are only two possible actions, the first is to
  56. throw a runtime exception and cause the program to quit (default behaviour),
  57. or the second is to modify the environment to something sensible (least
  58. surprising behaviour).
  59. The follow code produces the least surprising behaviour. It will use the user
  60. specified default locale if it is valid, and if not then it will modify the
  61. environment the process is running in to use a sensible default. This also means
  62. that users do not need to install language packs for their OS.
  63. */
  64. void setDefaultOrCLocale()
  65. {
  66. #if __unix__
  67. if (!std::setlocale(LC_ALL, ""))
  68. {
  69. setenv("LC_ALL", "C", 1);
  70. }
  71. #endif
  72. }
  73. int main(int argc, char** argv)
  74. {
  75. setDefaultOrCLocale();
  76. KeyCLI m(KeyCLI::OperationMode::ListBare);
  77. g_logVerbosity = 0;
  78. for (int i = 1; i < argc; ++i)
  79. {
  80. string arg = argv[i];
  81. if (m.interpretOption(i, argc, argv)) {}
  82. else if ((arg == "-v" || arg == "--verbosity") && i + 1 < argc)
  83. g_logVerbosity = atoi(argv[++i]);
  84. else if (arg == "-h" || arg == "--help")
  85. help();
  86. else if (arg == "-V" || arg == "--version")
  87. version();
  88. else
  89. {
  90. cerr << "Invalid argument: " << arg << endl;
  91. exit(-1);
  92. }
  93. }
  94. m.execute();
  95. return 0;
  96. }