OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
CmdArguments.cpp
Go to the documentation of this file.
1//
2// Class CmdArguments
3// Parsing command line arguments
4//
5// In order to have a flexible framework, each component implementation gets
6// access to all command line arguments.
7// All command line options have the form:
8// --name=value
9// Spaces before and after the "=" will be trimmed.
10//
11// Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
12// All rights reserved
13//
14// Implemented as part of the PhD thesis
15// "Toward massively parallel multi-objective optimization with application to
16// particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
17//
18// This file is part of OPAL.
19//
20// OPAL is free software: you can redistribute it and/or modify
21// it under the terms of the GNU General Public License as published by
22// the Free Software Foundation, either version 3 of the License, or
23// (at your option) any later version.
24//
25// You should have received a copy of the GNU General Public License
26// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
27//
28#include "Util/CmdArguments.h"
29
30#include "boost/algorithm/string.hpp"
31
32void CmdArguments::addArguments(int argc, char **argv) {
33
34 for(int i=1; i<argc; i++) {
35 std::string arg = argv[i];
36 std::string name, value;
37 this->split(name, value, arg);
38 arguments_.insert(std::pair<std::string, std::string>(name, value));
39 }
40}
41
42void CmdArguments::split(std::string &name,
43 std::string &value, std::string arg) {
44
45 size_t pos = arg.find("=");
46 //strip leading '--' and '='
47 name = arg.substr(2, pos - 2);
48 value = arg.substr(pos + 1);
49
50 boost::trim(name);
51 boost::trim(value);
52}
53
55 const unsigned int size = arguments_.size();
56 char** args = new char*[2 * size];
57
58 unsigned int i = 0;
59 auto it = arguments_.cbegin();
60 const auto end = arguments_.cend();
61 for (; it != end; ++ it) {
62 const std::string &key = it->first;
63 char* argname = new char[key.length() + 1];
64 strcpy(argname, key.c_str());
65 args[i ++] = argname;
66
67 const std::string &value = it->second;
68 char* argvalue = new char[value.length() + 1];
69 strcpy(argvalue, value.c_str());
70 args[i ++] = argvalue;
71 }
72
73 return args;
74}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
arg(a))
const std::string name
void split(std::string &name, std::string &value, std::string arg)
helper to split string
T arg(const std::string name)
Definition: CmdArguments.h:179
std::map< std::string, std::string > arguments_
container for storing command line options
Definition: CmdArguments.h:159
void addArguments(int argc, char **argv)
parse user arguments
char ** getArguments() const