votca 2024-dev
Loading...
Searching...
No Matches
application.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 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 submit bugs to " TOOLS_BUGREPORT "\n\n";
46 out << ProgramName();
47 if (VersionString() != "") {
48 out << ", version " << VersionString();
49 }
50 out << endl << "votca_tools, version " << ToolsVersionStr() << "\n\n";
51
52 HelpText(out);
53
54 // remove Hidden group from the option list and print
55 out << "\n\n" << VisibleOptions() << endl;
56}
57
58int Application::Exec(int argc, char **argv) {
59 try {
60 AddProgramOptions()("help,h", " display this help and exit");
61 AddProgramOptions()("verbose", " be loud and noisy");
62 AddProgramOptions()("verbose1", " be very loud and noisy");
63 AddProgramOptions()("verbose2,v", " be extremly loud and noisy");
64
65 Initialize(); // initialize program-specific parameters
66
68 argv); // initialize general parameters & read input file
69
71 if (op_vm_.count("verbose")) {
73 }
74 if (op_vm_.count("verbose1")) {
76 }
77
78 if (op_vm_.count("verbose2")) {
80 }
81
82 if (op_vm_.count("help")) {
83 ShowHelpText(cout);
84 return 0;
85 }
86
87 if (!EvaluateOptions()) {
88 ShowHelpText(cout);
89 return -1;
90 }
91
93 Run();
94 } else {
95 cout << "Done - stopping here\n";
96 }
97 } catch (std::exception &error) {
98 cerr << "an error occurred:\n" << error.what() << endl;
99 return -1;
100 }
101 return 0;
102}
103
104boost::program_options::options_description_easy_init
105 Application::AddProgramOptions(const string &group) {
106 // if no group is given, add it to standard options
107 if (group == "") {
108 return op_desc_.add_options();
109 }
110
111 // does group already exist, if yes, add it there
112 std::map<string, boost::program_options::options_description>::iterator iter =
113 op_groups_.find(group);
114 if (iter != op_groups_.end()) {
115 return iter->second.add_options();
116 }
117
118 // no group with given name was found -> create group
119 op_groups_.insert(
120 make_pair(group, boost::program_options::options_description(group)));
121
122 return op_groups_[group].add_options();
123}
124
125void Application::ParseCommandLine(int argc, char **argv) {
126 namespace po = boost::program_options;
127
128 // default options should be added to visible (the rest is handled via a map))
130
131 // add all categories to list of available options
132 for (const auto &pair : op_groups_) {
133 op_desc_.add(pair.second);
134 if (pair.first != "Hidden") {
135 visible_options_.add(pair.second);
136 }
137 }
138
139 // parse the command line
140 try {
141 po::store(po::parse_command_line(argc, argv, op_desc_), op_vm_);
142 po::notify(op_vm_);
143 } catch (boost::program_options::error &err) {
144 throw runtime_error(string("error parsing command line: ") + err.what());
145 }
146}
147
148void Application::CheckRequired(const string &option_name,
149 const string &error_msg) {
150 if (!op_vm_.count(option_name)) {
151 ShowHelpText(cout);
152 throw std::runtime_error("missing argument " + option_name + "\n" +
153 error_msg);
154 }
155}
156
157} // namespace tools
158} // 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 TOOLS_BUGREPORT