votca 2024.2-dev
Loading...
Searching...
No Matches
application.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2024 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <iostream>
20
21// Third party includes
22#include <boost/algorithm/string/replace.hpp>
23
24// Local VOTCA includes
26#include "votca/tools/globals.h"
28#include "votca/tools/version.h"
29
30// Local private VOTCA includes
31#include "votca_tools_config.h"
32
33namespace votca {
34namespace tools {
35using namespace std;
36Application::Application() : op_desc_("Allowed options") {}
37
39
40void Application::ShowHelpText(std::ostream &out) {
41 out << "==================================================\n";
42 out << "======== VOTCA (http://www.votca.org) ========\n";
43 out << "==================================================\n\n";
44
45 out << "please sread and cite: " PROJECT_CITATION "\n";
46 out << "and submit bugs to " PROJECT_BUGREPORT "\n\n";
47 out << ProgramName();
48 if (VersionString() != "") {
49 out << ", version " << VersionString();
50 }
51 out << endl << "votca_tools, version " << ToolsVersionStr() << "\n\n";
52
53 HelpText(out);
54
55 // remove Hidden group from the option list and print
56 out << "\n\n" << VisibleOptions() << endl;
57}
58
59int Application::Exec(int argc, char **argv) {
60 try {
61 AddProgramOptions()("help,h", " display this help and exit");
62 AddProgramOptions()("verbose", " be loud and noisy");
63 AddProgramOptions()("verbose1", " be very loud and noisy");
64 AddProgramOptions()("verbose2,v", " be extremly loud and noisy");
65
66 Initialize(); // initialize program-specific parameters
67
69 argv); // initialize general parameters & read input file
70
72 if (op_vm_.count("verbose")) {
74 }
75 if (op_vm_.count("verbose1")) {
77 }
78
79 if (op_vm_.count("verbose2")) {
81 }
82
83 if (op_vm_.count("help")) {
84 ShowHelpText(cout);
85 return 0;
86 }
87
88 if (!EvaluateOptions()) {
89 ShowHelpText(cout);
90 return -1;
91 }
92
94 Run();
95 } else {
96 cout << "Done - stopping here\n";
97 }
98 } catch (std::exception &error) {
99 cerr << "an error occurred:\n" << error.what() << endl;
100 return -1;
101 }
102 return 0;
103}
104
105boost::program_options::options_description_easy_init
106 Application::AddProgramOptions(const string &group) {
107 // if no group is given, add it to standard options
108 if (group == "") {
109 return op_desc_.add_options();
110 }
111
112 // does group already exist, if yes, add it there
113 std::map<string, boost::program_options::options_description>::iterator iter =
114 op_groups_.find(group);
115 if (iter != op_groups_.end()) {
116 return iter->second.add_options();
117 }
118
119 // no group with given name was found -> create group
120 op_groups_.insert(
121 make_pair(group, boost::program_options::options_description(group)));
122
123 return op_groups_[group].add_options();
124}
125
126void Application::ParseCommandLine(int argc, char **argv) {
127 namespace po = boost::program_options;
128
129 // default options should be added to visible (the rest is handled via a map))
131
132 // add all categories to list of available options
133 for (const auto &pair : op_groups_) {
134 op_desc_.add(pair.second);
135 if (pair.first != "Hidden") {
136 visible_options_.add(pair.second);
137 }
138 }
139
140 // parse the command line
141 try {
142 po::store(po::parse_command_line(argc, argv, op_desc_), op_vm_);
143 po::notify(op_vm_);
144 } catch (boost::program_options::error &err) {
145 throw runtime_error(string("error parsing command line: ") + err.what());
146 }
147}
148
149void Application::CheckRequired(const string &option_name,
150 const string &error_msg) {
151 if (!op_vm_.count(option_name)) {
152 ShowHelpText(cout);
153 throw std::runtime_error("missing argument " + option_name + "\n" +
154 error_msg);
155 }
156}
157
158} // namespace tools
159} // namespace votca
int Exec(int argc, char **argv)
executes the program
void ParseCommandLine(int argc, char **argv)
get input parameters from file, location may be specified in command line
boost::program_options::variables_map op_vm_
Variable map containing all program options.
virtual bool EvaluateOptions()=0
Process command line options.
std::map< std::string, boost::program_options::options_description > op_groups_
virtual void HelpText(std::ostream &out)=0
help text of application without version information
boost::program_options::options_description & VisibleOptions()
filters out the Hidden group from the options descriptions
virtual std::string ProgramName()=0
program name
virtual std::string VersionString()
version string of application
Definition application.h:55
boost::program_options::options_description_easy_init AddProgramOptions(const std::string &group="")
add option for command line
boost::program_options::options_description visible_options_
program options without the Hidden group
virtual void Initialize()=0
Initialize application data.
virtual void Run()=0
Main body of application.
virtual void ShowHelpText(std::ostream &out)
void CheckRequired(const std::string &option_name, const std::string &error_msg="")
Check weather required option is set.
boost::program_options::options_description op_desc_
program options required by all applications
STL namespace.
const std::string & ToolsVersionStr()
Definition version.cc:33
base class for all analysis tools
Definition basebead.h:33
static Level current_level
Definition globals.h:30
#define PROJECT_BUGREPORT
#define PROJECT_CITATION