CommandLineArguments.cpp

00001 /*
00002 
00003 Copyright (c) 2005-2014, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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     // Make doubly sure there's only one instance
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     // Convert to lowercase
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 

Generated by  doxygen 1.6.2