Skip to content

Commit

Permalink
Merge pull request #69 from rest-for-physics/lobis-cli
Browse files Browse the repository at this point in the history
CLI can now parse scientific notation integers
  • Loading branch information
lobis authored Sep 26, 2022
2 parents 3060283 + 205e1cb commit 7d194ca
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Application.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <TRestRun.h>

#include <csignal>
#include <regex>
#ifndef GEANT4_WITHOUT_G4RunManagerFactory
#include <G4RunManagerFactory.hh>
#endif
Expand Down Expand Up @@ -124,6 +125,17 @@ int GetSecondsFromFullTimeExpression(const char* expression) {
return seconds;
}

int GetNumberFromStringScientific(string& s) {
// Allows to parse inputs of the form 1E5, 1.5E10 etc.
regex rgx("^([0-9]+.?([0-9]+)?)E([0-9]+)");
std::smatch matches;
if (std::regex_search(s, matches, rgx)) {
return int(stod(matches[1].str()) * TMath::Power(10, stoi(matches[3].str())));
} else {
return stoi(s);
}
}

Options ProcessCommandLineOptions(int argc, char* const argv[]) {
Options options;
options.argc = argc;
Expand Down Expand Up @@ -183,21 +195,22 @@ Options ProcessCommandLineOptions(int argc, char* const argv[]) {
exit(1);
}
} else if ((arg == "-n") || (arg == "--events")) {
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
options.nEvents =
stoi(argv[++i]); // Increment 'i' so we don't get the argument as the next argv[i].
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
string s = argv[++i]; // Increment 'i' so we don't get the argument as the next argv[i].
options.nEvents = GetNumberFromStringScientific(s);
if (options.nEvents <= 0) {
cout << "--events option error: number of events must be > 0" << endl;
cout << "--events option error: number of events must be > 0 (input: " << s << ")"
<< endl;
exit(1);
}
} else {
cerr << "--events option requires one argument." << endl;
exit(1);
}
} else if ((arg == "-e") || (arg == "--entries")) {
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
options.nRequestedEntries =
stoi(argv[++i]); // Increment 'i' so we don't get the argument as the next argv[i].
if (i + 1 < argc) { // Make sure we aren't at the end of argv!
string s = argv[++i]; // Increment 'i' so we don't get the argument as the next argv[i].
options.nRequestedEntries = GetNumberFromStringScientific(s);
if (options.nRequestedEntries <= 0) {
cout << "--entries option error: number of entries must be > 0" << endl;
exit(1);
Expand Down

0 comments on commit 7d194ca

Please sign in to comment.