Chaste Commit::357af77f3d8b47aa179dbebbdde04c89ce3f2aca
CommandLineArguments.cpp
1/*
2
3Copyright (c) 2005-2026, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "CommandLineArguments.hpp"
37
38#include <cassert>
39#include <cstddef>
40#include <algorithm>
41#include <petsc.h>
42
43#include "PetscException.hpp"
44#include "PetscTools.hpp"
45
47 : p_argc(nullptr),
48 p_argv(nullptr)
49{
50 // Make doubly sure there's only one instance
51 assert(mpInstance == nullptr);
52}
53
62
64
65bool CommandLineArguments::OptionExists(const std::string& rOption)
66{
67 TestOptionFormat(rOption);
68 try
69 {
70 return(GetIndexForArgument(rOption));
71 }
72 catch (const Exception&)
73 {
74 return false;
75 }
76}
77
78char* CommandLineArguments::GetValueCorrespondingToOption(const std::string& rOption, int valueNumber)
79{
80 EXCEPT_IF_NOT(valueNumber>0);
81 TestOptionFormat(rOption);
82
83 int index = GetIndexForArgument(rOption);
84 int num_args = GetNumberOfArgumentsForOption(rOption, true);
85 if (num_args < valueNumber)
86 {
87 std::stringstream ss;
88 ss<<"Index="<<valueNumber<<" requested for '"<<rOption<<"', but only "<<num_args<<" given.";
89 EXCEPTION(ss.str());
90 }
91
92 return (*p_argv)[index+valueNumber];
93}
94
95double CommandLineArguments::GetDoubleCorrespondingToOption(const std::string& rOption, int valueNumber)
96{
97 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
98 return atof(val);
99}
100
101int CommandLineArguments::GetIntCorrespondingToOption(const std::string& rOption, int valueNumber)
102{
103 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
104 return atoi(val);
105}
106
107unsigned CommandLineArguments::GetUnsignedCorrespondingToOption(const std::string& rOption, int valueNumber)
108{
109 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
110 int i = atoi(val);
111 if (i < 0)
112 {
113 EXCEPTION("Option is a negative number and cannot be converted to unsigned.");
114 }
115 return (unsigned)(i);
116}
117
119{
120 TestOptionFormat(rOption);
121
122 for (int i=1; i<*p_argc; i++)
123 {
124 if (rOption==std::string((*p_argv)[i]))
125 {
126 MarkOptionUsed(rOption);
127 return i;
128 }
129 }
130 EXCEPTION("Command line option '"+rOption+"' does not exist");
131}
132
133void CommandLineArguments::MarkOptionUsed(const std::string& rOption)
134{
135 // Register this option
136 mUsedOptions.insert(rOption);
137
139 {
140 // Register this option with PETSc, so PETSc doesn't complain about an unused option.
141 // Looking an option up in PETSc's database is what marks it as used there.
142 PetscBool found;
143 PETSCEXCEPT(PetscOptionsHasName(nullptr, nullptr, rOption.c_str(), &found));
144
145 }
146}
147
149{
150 std::vector<std::string> unused;
151 for (int i=1; i<*p_argc; i++)
152 {
153 std::string argument = std::string((*p_argv)[i]);
154 bool is_option = (argument.substr(0,1)=="-" && argument.substr(1,1).find_first_of("0123456789")==std::string::npos);
155 if (!is_option
156 || mUsedOptions.count(argument) > 0
157 || std::find(unused.begin(), unused.end(), argument) != unused.end())
158 {
159 continue;
160 }
161
162 PetscBool used_by_petsc = PETSC_FALSE;
164 {
165 // Unlike most PETSc options functions, PetscOptionsUsed expects the name without its first '-'
166 std::string petsc_name = argument.substr(1);
167 PETSCEXCEPT(PetscOptionsUsed(nullptr, petsc_name.c_str(), &used_by_petsc));
168 }
169 if (!used_by_petsc)
170 {
171 unused.push_back(argument);
172 }
173 }
174 return unused;
175}
176
177int CommandLineArguments::GetNumberOfArgumentsForOption(const std::string& rOption, bool throwIfNone)
178{
179 int start_idx = GetIndexForArgument(rOption);
180
181 int end_idx = start_idx;
182 for (int i=start_idx+1; i<*p_argc; i++)
183 {
184 std::string argument = std::string((*p_argv)[i]);
185 if (argument.substr(0,1)=="-" && argument.substr(1,1).find_first_of("0123456789")==std::string::npos)
186 {
187 break;
188 }
189 end_idx = i;
190 }
191
192 if (end_idx == start_idx)
193 {
194 if (throwIfNone)
195 {
196 EXCEPTION("No value(s) given after command line option '" + rOption + "'");
197 }
198 else
199 {
200 return 0;
201 }
202 }
203
204 return end_idx - start_idx;
205}
206
207std::string CommandLineArguments::GetStringCorrespondingToOption(const std::string& rOption, int valueNumber)
208{
209 char* val = GetValueCorrespondingToOption(rOption, valueNumber);
210 std::string string_arg(val);
211 return string_arg;
212}
213
214std::vector<std::string> CommandLineArguments::GetStringsCorrespondingToOption(const std::string& rOption)
215{
216 std::vector<std::string> strings;
217 int num_args = GetNumberOfArgumentsForOption(rOption, true);
218 for (int i=1; i<=num_args; ++i)
219 {
220 strings.push_back(GetStringCorrespondingToOption(rOption, i));
221 }
222 return strings;
223}
224
225std::vector<double> CommandLineArguments::GetDoublesCorrespondingToOption(const std::string& rOption)
226{
227 std::vector<double> doubles;
228 int num_args = GetNumberOfArgumentsForOption(rOption, true);
229 for (int i=1; i<=num_args; ++i)
230 {
231 doubles.push_back(GetDoubleCorrespondingToOption(rOption, i));
232 }
233 return doubles;
234}
235
236std::vector<unsigned> CommandLineArguments::GetUnsignedsCorrespondingToOption(const std::string& rOption)
237{
238 std::vector<unsigned> unsigneds;
239 int num_args = GetNumberOfArgumentsForOption(rOption, true);
240 for (int i=1; i<=num_args; ++i)
241 {
242 unsigneds.push_back(GetUnsignedCorrespondingToOption(rOption, i));
243 }
244 return unsigneds;
245}
246
247std::vector<int> CommandLineArguments::GetIntsCorrespondingToOption(const std::string& rOption)
248{
249 std::vector<int> ints;
250 int num_args = GetNumberOfArgumentsForOption(rOption, true);
251 for (int i=1; i<=num_args; ++i)
252 {
253 ints.push_back(GetIntCorrespondingToOption(rOption, i));
254 }
255 return ints;
256}
257
259{
260 bool result;
261 std::string string_result = GetStringCorrespondingToOption(rOption);
262
263 // Convert to lowercase
264 std::transform(string_result.begin(), string_result.end(), string_result.begin(), ::tolower);
265
266 if (string_result=="0" || string_result=="false")
267 {
268 result = false;
269 }
270 else if (string_result=="1" || string_result=="true")
271 {
272 result = true;
273 }
274 else
275 {
276 EXCEPTION("The option '" << rOption << "' argument '" <<
277 GetStringCorrespondingToOption(rOption) << "' cannot be converted to a bool.");
278 }
279
280 return result;
281}
282
283void CommandLineArguments::TestOptionFormat(const std::string& rOption)
284{
285 if (!( rOption.substr(0,1) == "-" && rOption.substr(1,1).find_first_of("0123456789")==std::string::npos))
286 {
287 EXCEPTION("A command line option must begin with '-' followed by a non-numeric character.");
288 }
289}
#define EXCEPTION(message)
#define EXCEPT_IF_NOT(test)
bool OptionExists(const std::string &rOption)
static CommandLineArguments * Instance()
static CommandLineArguments * mpInstance
std::vector< double > GetDoublesCorrespondingToOption(const std::string &rOption)
std::string GetStringCorrespondingToOption(const std::string &rOption, int valueNumber=1)
std::set< std::string > mUsedOptions
int GetIndexForArgument(std::string rOption)
std::vector< std::string > GetStringsCorrespondingToOption(const std::string &rOption)
int GetIntCorrespondingToOption(const std::string &rOption, int valueNumber=1)
std::vector< unsigned > GetUnsignedsCorrespondingToOption(const std::string &rOption)
double GetDoubleCorrespondingToOption(const std::string &rOption, int valueNumber=1)
unsigned GetUnsignedCorrespondingToOption(const std::string &rOption, int valueNumber=1)
void MarkOptionUsed(const std::string &rOption)
bool GetBoolCorrespondingToOption(const std::string &rOption)
char * GetValueCorrespondingToOption(const std::string &rOption, int valueNumber=1)
int GetNumberOfArgumentsForOption(const std::string &rOption, bool throwIfNone=false)
std::vector< int > GetIntsCorrespondingToOption(const std::string &rOption)
void TestOptionFormat(const std::string &rOption)
std::vector< std::string > GetUnusedOptions()
static bool IsInitialised()