OPAL (Object Oriented Parallel Accelerator Library)  2024.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 
32 void 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 
42 void 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 }
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed we want its recipients to know that what they have is not the so that any problems introduced by others will not reflect on the original authors reputations any free program is threatened constantly by software patents We wish to avoid the danger that redistributors of a free program will individually obtain patent in effect making the program proprietary To prevent we have made it clear that any patent must be licensed for everyone s free use or not licensed at all The precise terms and conditions for distribution and modification follow GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR DISTRIBUTION AND MODIFICATION This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License The refers to any such program or and a work based on the Program means either the Program or any derivative work under copyright a work containing the Program or a portion of it
Definition: LICENSE:43
std::map< std::string, std::string > arguments_
container for storing command line options
Definition: CmdArguments.h:159
arg(a))
void split(std::string &name, std::string &value, std::string arg)
helper to split string
void addArguments(int argc, char **argv)
parse user arguments
const std::string name
char ** getArguments() const
end
Definition: multipole_t.tex:9
T arg(const std::string name)
Definition: CmdArguments.h:179