OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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 "Utilities/Util.h"
31
32#include <cstddef>
33#include <cstring>
34#include <string>
35
36void CmdArguments::addArguments(int argc, char **argv) {
37
38 for (int i=1; i<argc; i++) {
39 std::string arg = argv[i];
40 std::string name, value;
41 this->split(name, value, arg);
42 arguments_.insert(std::pair<std::string, std::string>(name, value));
43 }
44}
45
46void CmdArguments::split(std::string& name,
47 std::string& value, std::string arg) {
48
49 std::size_t pos = arg.find("=");
50 //strip leading '--' and '='
51 name = arg.substr(2, pos - 2);
52 value = arg.substr(pos + 1);
53
54 name = Util::trim_chars(name, " \t\n\r\f\v");
55 value = Util::trim_chars(value, " \t\n\r\f\v");
56}
57
59 const unsigned int size = arguments_.size();
60 char** args = new char*[2 * size];
61
62 unsigned int i = 0;
63 auto it = arguments_.cbegin();
64 const auto end = arguments_.cend();
65 for (; it != end; ++ it) {
66 const std::string& key = it->first;
67 char* argname = new char[key.length() + 1];
68 std::strcpy(argname, key.c_str());
69 args[i++] = argname;
70
71 const std::string& value = it->second;
72 char* argvalue = new char[value.length() + 1];
73 std::strcpy(argvalue, value.c_str());
74 args[i++] = argvalue;
75 }
76
77 return args;
78}
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
arg(a))
const std::string name
std::string trim_chars(const std::string &s, const std::string &chars)
Definition Util.h:245
void split(std::string &name, std::string &value, std::string arg)
helper to split string
T arg(const std::string name)
std::map< std::string, std::string > arguments_
container for storing command line options
void addArguments(int argc, char **argv)
parse user arguments
char ** getArguments() const