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 TestOptionFormat(rOption);
00064 try
00065 {
00066 return(GetIndexForArgument(rOption));
00067 }
00068 catch (const Exception&)
00069 {
00070 return false;
00071 }
00072 }
00073
00074 char* CommandLineArguments::GetValueCorrespondingToOption(const std::string& rOption, int valueNumber)
00075 {
00076 EXCEPT_IF_NOT(valueNumber>0);
00077 TestOptionFormat(rOption);
00078
00079 int index = GetIndexForArgument(rOption);
00080 int num_args = GetNumberOfArgumentsForOption(rOption, true);
00081 if ( num_args<valueNumber )
00082 {
00083 std::stringstream ss;
00084 ss<<"Index="<<valueNumber<<" requested for '"<<rOption<<"', but only "<<num_args<<" given.";
00085 EXCEPTION(ss.str());
00086 }
00087
00088 return (*p_argv)[index+valueNumber];
00089 }
00090
00091 double CommandLineArguments::GetDoubleCorrespondingToOption(const std::string& rOption, int valueNumber)
00092 {
00093 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00094 return atof(val);
00095 }
00096
00097 int CommandLineArguments::GetIntCorrespondingToOption(const std::string& rOption, int valueNumber)
00098 {
00099 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00100 return atoi(val);
00101 }
00102
00103 unsigned CommandLineArguments::GetUnsignedCorrespondingToOption(const std::string& rOption, int valueNumber)
00104 {
00105 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00106 int i = atoi(val);
00107 if (i < 0)
00108 {
00109 EXCEPTION("Option is a negative number and cannot be converted to unsigned.");
00110 }
00111 return (unsigned)(i);
00112 }
00113
00114 int CommandLineArguments::GetIndexForArgument(std::string rOption)
00115 {
00116 TestOptionFormat(rOption);
00117
00118 for (int i=1; i<*p_argc; i++)
00119 {
00120 if (rOption==std::string((*p_argv)[i]))
00121 {
00122 return i;
00123 }
00124 }
00125 EXCEPTION("Command line option '"+rOption+"' does not exist");
00126 }
00127
00128 int CommandLineArguments::GetNumberOfArgumentsForOption(const std::string& rOption, bool throwIfNone)
00129 {
00130 int start_idx = GetIndexForArgument(rOption);
00131
00132 int end_idx = start_idx;
00133 for (int i=start_idx+1; i<*p_argc; i++)
00134 {
00135 std::string argument = std::string((*p_argv)[i]);
00136 if (argument.substr(0,1)=="-" && argument.substr(1,1).find_first_of("0123456789")==std::string::npos)
00137 {
00138 break;
00139 }
00140 end_idx = i;
00141 }
00142
00143 if (end_idx == start_idx)
00144 {
00145 if ( throwIfNone )
00146 {
00147 EXCEPTION("No value(s) given after command line option '" + rOption + "'");
00148 }
00149 else
00150 {
00151 return 0;
00152 }
00153
00154 }
00155
00156 return end_idx - start_idx;
00157 }
00158
00159 std::string CommandLineArguments::GetStringCorrespondingToOption(const std::string& rOption, int valueNumber)
00160 {
00161 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
00162 std::string string_arg(val);
00163 return string_arg;
00164 }
00165
00166 std::vector<std::string> CommandLineArguments::GetStringsCorrespondingToOption(const std::string& rOption)
00167 {
00168 std::vector<std::string> strings;
00169 int num_args = GetNumberOfArgumentsForOption(rOption, true);
00170 for(int i=1; i<=num_args; ++i)
00171 {
00172 strings.push_back(GetStringCorrespondingToOption(rOption, i));
00173 }
00174 return strings;
00175 }
00176
00177 std::vector<double> CommandLineArguments::GetDoublesCorrespondingToOption(const std::string& rOption)
00178 {
00179 std::vector<double> doubles;
00180 int num_args = GetNumberOfArgumentsForOption(rOption, true);
00181 for(int i=1; i<=num_args; ++i)
00182 {
00183 doubles.push_back(GetDoubleCorrespondingToOption(rOption, i));
00184 }
00185 return doubles;
00186 }
00187
00188 std::vector<unsigned> CommandLineArguments::GetUnsignedsCorrespondingToOption(const std::string& rOption)
00189 {
00190 std::vector<unsigned> unsigneds;
00191 int num_args = GetNumberOfArgumentsForOption(rOption, true);
00192 for(int i=1; i<=num_args; ++i)
00193 {
00194 unsigneds.push_back(GetUnsignedCorrespondingToOption(rOption, i));
00195 }
00196 return unsigneds;
00197 }
00198
00199 std::vector<int> CommandLineArguments::GetIntsCorrespondingToOption(const std::string& rOption)
00200 {
00201 std::vector<int> ints;
00202 int num_args = GetNumberOfArgumentsForOption(rOption, true);
00203 for(int i=1; i<=num_args; ++i)
00204 {
00205 ints.push_back(GetIntCorrespondingToOption(rOption, i));
00206 }
00207 return ints;
00208 }
00209
00210 bool CommandLineArguments::GetBoolCorrespondingToOption(const std::string& rOption)
00211 {
00212 bool result;
00213 std::string string_result = GetStringCorrespondingToOption(rOption);
00214
00215
00216 std::transform(string_result.begin(), string_result.end(), string_result.begin(), ::tolower);
00217
00218 if (string_result=="0" || string_result=="false")
00219 {
00220 result = false;
00221 }
00222 else if (string_result=="1" || string_result=="true")
00223 {
00224 result = true;
00225 }
00226 else
00227 {
00228 EXCEPTION("The option '" << rOption << "' argument '" <<
00229 GetStringCorrespondingToOption(rOption) << "' cannot be converted to a bool.");
00230 }
00231
00232 return result;
00233 }
00234
00235 void CommandLineArguments::TestOptionFormat(const std::string& rOption)
00236 {
00237 if ( !( rOption.substr(0,1) == "-" && rOption.substr(1,1).find_first_of("0123456789")==std::string::npos ) )
00238 {
00239 EXCEPTION("A command line option must begin with '-' followed by a non-numeric character.");
00240 }
00241 }
00242