commandlinearguments.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file commandlinearguments.cpp
  3. /// \brief Takes care of the command line arguments to LPJ-GUESS
  4. ///
  5. /// $Date: 2013-07-17 09:22:52 +0200 (Wed, 17 Jul 2013) $
  6. ///
  7. ///////////////////////////////////////////////////////////////////////////////////////
  8. #include "config.h"
  9. #include "commandlinearguments.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. #include <algorithm>
  14. namespace {
  15. std::string tolower(const char* str) {
  16. std::string result(str);
  17. std::transform(result.begin(), result.end(), result.begin(), ::tolower);
  18. return result;
  19. }
  20. }
  21. CommandLineArguments::CommandLineArguments(int argc, char** argv)
  22. : help(false),
  23. parallel(false),
  24. input_module("ece"), // run_ecev3_not_cruncep
  25. //input_module("cru_ncep"), // run_cruncep_not_ecev3
  26. islpjgspinup(false){ // ecev3 - "cru" to "ece", and added islpjgspinup
  27. driver_file = "";
  28. if (!parse_arguments(argc, argv)) {
  29. print_usage(argv[0]);
  30. }
  31. }
  32. bool CommandLineArguments::parse_arguments(int argc, char** argv) {
  33. if (argc < 2) {
  34. return false;
  35. }
  36. // For now, just consider anything starting with '-' to be an option,
  37. // and anything else to be the ins file
  38. for (int i = 1; i < argc; ++i) {
  39. if (argv[i][0] == '-') {
  40. std::string option = tolower(argv[i]);
  41. if (option == "-help") {
  42. help = true;
  43. }
  44. else if (option == "-parallel") {
  45. parallel = true;
  46. }
  47. else if (option == "-islpjgspinup") { // ecev3 - assumed false unless indicated in the command line
  48. islpjgspinup = true;
  49. }
  50. else if (option == "-input") {
  51. if (i+1 < argc) {
  52. std::string module = tolower(argv[i + 1]);
  53. if (module == "getclim") {
  54. // If GetClim input module requested, next argument should be path to driver file
  55. if (i + 2 < argc){
  56. input_module = argv[i + 1];
  57. driver_file = argv[i + 2];
  58. i += 2; // skip two arguments
  59. }
  60. else {
  61. fprintf(stderr, "Missing pathname after -input getclim");
  62. return false;
  63. }
  64. }
  65. else {
  66. input_module = argv[i + 1];
  67. ++i; // skip the next argument
  68. }
  69. }
  70. else {
  71. fprintf(stderr, "Missing argument after -input\n");
  72. return false;
  73. }
  74. }
  75. else {
  76. fprintf(stderr, "Unknown option: \"%s\"\n", argv[i]);
  77. return false;
  78. }
  79. }
  80. else {
  81. if (insfile.empty()) {
  82. insfile = argv[i];
  83. }
  84. else {
  85. fprintf(stderr, "Two arguments parsed as ins file: %s and %s\n",
  86. insfile.c_str(), argv[i]);
  87. return false;
  88. }
  89. }
  90. }
  91. // The only time it's ok not to specify an insfile is if -help is used
  92. if (insfile.empty() && !help) {
  93. fprintf(stderr, "No instruction file specified\n");
  94. return false;
  95. }
  96. return true;
  97. }
  98. void CommandLineArguments::print_usage(const char* command_name) const {
  99. fprintf(stderr, "\nUsage: %s [-parallel] [-input <module_name> [<GetClim-driver-file-path>] ] <instruction-script-filename> | -help\n",
  100. command_name);
  101. exit(EXIT_FAILURE);
  102. }
  103. bool CommandLineArguments::get_help() const {
  104. return help;
  105. }
  106. bool CommandLineArguments::get_parallel() const {
  107. return parallel;
  108. }
  109. const char* CommandLineArguments::get_instruction_file() const {
  110. return insfile.c_str();
  111. }
  112. const char* CommandLineArguments::get_input_module() const {
  113. return input_module.c_str();
  114. }
  115. const char* CommandLineArguments::get_driver_file() const {
  116. return driver_file.c_str();
  117. }
  118. // ecev3
  119. bool CommandLineArguments::get_islpjgspinup() const {
  120. return islpjgspinup;
  121. }