Skip to content

Commit 7d194ca

Browse files
authored
Merge pull request #69 from rest-for-physics/lobis-cli
CLI can now parse scientific notation integers
2 parents 3060283 + 205e1cb commit 7d194ca

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Application.cxx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <TRestRun.h>
1313

1414
#include <csignal>
15+
#include <regex>
1516
#ifndef GEANT4_WITHOUT_G4RunManagerFactory
1617
#include <G4RunManagerFactory.hh>
1718
#endif
@@ -124,6 +125,17 @@ int GetSecondsFromFullTimeExpression(const char* expression) {
124125
return seconds;
125126
}
126127

128+
int GetNumberFromStringScientific(string& s) {
129+
// Allows to parse inputs of the form 1E5, 1.5E10 etc.
130+
regex rgx("^([0-9]+.?([0-9]+)?)E([0-9]+)");
131+
std::smatch matches;
132+
if (std::regex_search(s, matches, rgx)) {
133+
return int(stod(matches[1].str()) * TMath::Power(10, stoi(matches[3].str())));
134+
} else {
135+
return stoi(s);
136+
}
137+
}
138+
127139
Options ProcessCommandLineOptions(int argc, char* const argv[]) {
128140
Options options;
129141
options.argc = argc;
@@ -183,21 +195,22 @@ Options ProcessCommandLineOptions(int argc, char* const argv[]) {
183195
exit(1);
184196
}
185197
} else if ((arg == "-n") || (arg == "--events")) {
186-
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
187-
options.nEvents =
188-
stoi(argv[++i]); // Increment 'i' so we don't get the argument as the next argv[i].
198+
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
199+
string s = argv[++i]; // Increment 'i' so we don't get the argument as the next argv[i].
200+
options.nEvents = GetNumberFromStringScientific(s);
189201
if (options.nEvents <= 0) {
190-
cout << "--events option error: number of events must be > 0" << endl;
202+
cout << "--events option error: number of events must be > 0 (input: " << s << ")"
203+
<< endl;
191204
exit(1);
192205
}
193206
} else {
194207
cerr << "--events option requires one argument." << endl;
195208
exit(1);
196209
}
197210
} else if ((arg == "-e") || (arg == "--entries")) {
198-
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
199-
options.nRequestedEntries =
200-
stoi(argv[++i]); // Increment 'i' so we don't get the argument as the next argv[i].
211+
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
212+
string s = argv[++i]; // Increment 'i' so we don't get the argument as the next argv[i].
213+
options.nRequestedEntries = GetNumberFromStringScientific(s);
201214
if (options.nRequestedEntries <= 0) {
202215
cout << "--entries option error: number of entries must be > 0" << endl;
203216
exit(1);

0 commit comments

Comments
 (0)