CommandLineArguments.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "CommandLineArguments.hpp"
00037
00038 #include <cassert>
00039 #include <cstddef>
00040 #include <algorithm>
00041
00042 CommandLineArguments::CommandLineArguments()
00043 : p_argc(NULL),
00044 p_argv(NULL)
00045 {
00046
00047 assert(mpInstance == NULL);
00048 }
00049
00050 CommandLineArguments* CommandLineArguments::Instance()
00051 {
00052 if (mpInstance == NULL)
00053 {
00054 mpInstance = new CommandLineArguments;
00055 }
00056 return mpInstance;
00057 }
00058
00059 CommandLineArguments* CommandLineArguments::mpInstance = NULL;
00060
00061 bool CommandLineArguments::OptionExists(const std::string& rOption)
00062 {
00063 int index = GetIndexForArgument(rOption);
00064 assert(index!=0);
00065
00066 return (index > 0);
00067 }
00068
00069 char* CommandLineArguments::GetValueCorrespondingToOption(const std::string& rOption, int valueNumber)
00070 {
00071 EXCEPT_IF_NOT(valueNumber>0);
00072 TestOptionFormat(rOption);
00073
00074 int num_args = GetNumberOfArgumentsForOption(rOption);
00075 int index = GetIndexForArgument(rOption);
00076 EXCEPT_IF_NOT(index>0);
00077 EXCEPT_IF_NOT(num_args>=valueNumber);
00078 return (*p_argv)[index+valueNumber];
00079 }
00080
00081 double CommandLineArguments::GetDoubleCorrespondingToOption(const std::string& rOption, int valueNumber)
00082 {
00083 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00084 return atof(val);
00085 }
00086
00087 int CommandLineArguments::GetIntCorrespondingToOption(const std::string& rOption, int valueNumber)
00088 {
00089 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00090 return atoi(val);
00091 }
00092
00093 unsigned CommandLineArguments::GetUnsignedCorrespondingToOption(const std::string& rOption, int valueNumber)
00094 {
00095 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00096 int i = atoi(val);
00097 if (i < 0)
00098 {
00099 EXCEPTION("Option is a negative number and cannot be converted to unsigned.");
00100 }
00101 return (unsigned)(i);
00102 }
00103
00104 int CommandLineArguments::GetIndexForArgument(std::string argument)
00105 {
00106 TestOptionFormat(argument);
00107
00108 for (int i=1; i<*p_argc; i++)
00109 {
00110 if (argument==std::string((*p_argv)[i]))
00111 {
00112 return i;
00113 }
00114 }
00115 return -1;
00116 }
00117
00118 int CommandLineArguments::GetNumberOfArgumentsForOption(const std::string& rOption)
00119 {
00120 int start_idx = GetIndexForArgument(rOption);
00121 if (start_idx < 0)
00122 {
00123 EXCEPTION("Command line option '" + rOption + "' does not exist");
00124 }
00125
00126 int end_idx = start_idx;
00127 for (int i=start_idx+1; i<*p_argc; i++)
00128 {
00129 std::string argument = std::string((*p_argv)[i]);
00130 if (argument.substr(0,1)=="-" && argument.substr(1,1).find_first_of("0123456789")==std::string::npos)
00131 {
00132 break;
00133 }
00134 end_idx = i;
00135 }
00136
00137 if (end_idx == start_idx)
00138 {
00139 EXCEPTION("No value(s) given after command line option '" + rOption + "'");
00140 }
00141
00142 return end_idx - start_idx;
00143 }
00144
00145 std::string CommandLineArguments::GetStringCorrespondingToOption(const std::string& rOption, int valueNumber)
00146 {
00147 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00148 std::string string_arg(val);
00149 return string_arg;
00150 }
00151
00152 std::vector<std::string> CommandLineArguments::GetStringsCorrespondingToOption(const std::string& rOption)
00153 {
00154 std::vector<std::string> strings;
00155 int num_args = GetNumberOfArgumentsForOption(rOption);
00156 for(int i=1; i<=num_args; ++i)
00157 {
00158 strings.push_back(GetStringCorrespondingToOption(rOption, i));
00159 }
00160 return strings;
00161 }
00162
00163 std::vector<double> CommandLineArguments::GetDoublesCorrespondingToOption(const std::string& rOption)
00164 {
00165 std::vector<double> doubles;
00166 int num_args = GetNumberOfArgumentsForOption(rOption);
00167 for(int i=1; i<=num_args; ++i)
00168 {
00169 doubles.push_back(GetDoubleCorrespondingToOption(rOption, i));
00170 }
00171 return doubles;
00172 }
00173
00174 std::vector<unsigned> CommandLineArguments::GetUnsignedsCorrespondingToOption(const std::string& rOption)
00175 {
00176 std::vector<unsigned> unsigneds;
00177 int num_args = GetNumberOfArgumentsForOption(rOption);
00178 for(int i=1; i<=num_args; ++i)
00179 {
00180 unsigneds.push_back(GetUnsignedCorrespondingToOption(rOption, i));
00181 }
00182 return unsigneds;
00183 }
00184
00185 std::vector<int> CommandLineArguments::GetIntsCorrespondingToOption(const std::string& rOption)
00186 {
00187 std::vector<int> ints;
00188 int num_args = GetNumberOfArgumentsForOption(rOption);
00189 for(int i=1; i<=num_args; ++i)
00190 {
00191 ints.push_back(GetIntCorrespondingToOption(rOption, i));
00192 }
00193 return ints;
00194 }
00195
00196 bool CommandLineArguments::GetBoolCorrespondingToOption(const std::string& rOption)
00197 {
00198 bool result;
00199 std::string string_result = GetStringCorrespondingToOption(rOption);
00200
00201
00202 std::transform(string_result.begin(), string_result.end(), string_result.begin(), ::tolower);
00203
00204 if (string_result=="0" || string_result=="false")
00205 {
00206 result = false;
00207 }
00208 else if (string_result=="1" || string_result=="true")
00209 {
00210 result = true;
00211 }
00212 else
00213 {
00214 EXCEPTION("The option '" << rOption << "' argument '" <<
00215 GetStringCorrespondingToOption(rOption) << "' cannot be converted to a bool.");
00216 }
00217
00218 return result;
00219 }
00220
00221 void CommandLineArguments::TestOptionFormat(const std::string& rOption)
00222 {
00223 if ( !( rOption.substr(0,1) == "-" && rOption.substr(1,1).find_first_of("0123456789")==std::string::npos ) )
00224 {
00225 EXCEPTION("A command line option must begin with '-' followed by a non-numeric character.");
00226 }
00227 }
00228