Uniform and less verbose startup for all binaries.
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinemodulemanager.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012, by the GROMACS development team, led by
5  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6  * others, as listed in the AUTHORS file in the top-level source
7  * directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \file
36  * \brief
37  * Declares gmx::CommandLineModuleManager.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inpublicapi
41  * \ingroup module_commandline
42  */
43 #ifndef GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
44 #define GMX_COMMANDLINE_CMDLINEMODULEMANAGER_H
45
46 #include "../onlinehelp/helptopicinterface.h"
47 #include "../utility/common.h"
48 #include "../utility/uniqueptr.h"
49
50 namespace gmx
51 {
52
53 class CommandLineModuleInterface;
54 class ProgramInfo;
55
56 //! Smart pointer type for managing a CommandLineModuleInterface.
57 typedef gmx_unique_ptr<CommandLineModuleInterface>::type
58     CommandLineModulePointer;
59
60 /*! \brief
61  * Implements a wrapper command-line interface for multiple modules.
62  *
63  * Typical usage:
64  * \code
65    int
66    main(int argc, char *argv[])
67    {
68        const gmx::ProgramInfo &programInfo =
69            gmx::ProgramInfo::init("gmx", argc, argv);
70        try
71        {
72            gmx::CommandLineModuleManager manager(programInfo);
73            // <register all necessary modules>
74            return manager.run(argc, argv);
75        }
76        catch (const std::exception &ex)
77        {
78            gmx::printFatalErrorMessage(stderr, ex);
79            return 1;
80        }
81    }
82  * \endcode
83  *
84  * \inpublicapi
85  * \ingroup module_commandline
86  */
87 class CommandLineModuleManager
88 {
89     public:
90         /*! \brief
91          * Initializes a command-line module manager.
92          *
93          * \param[in] programInfo  Program information for the running binary.
94          * \throws    std::bad_alloc if out of memory.
95          *
96          * The binary name is used to detect when the binary is run through a
97          * symlink, and automatically invoke a matching module in such a case.
98          */
99         explicit CommandLineModuleManager(const ProgramInfo &programInfo);
100         ~CommandLineModuleManager();
101
102         /*! \brief
103          * Sets the module manager to quiet mode: don't print anything.
104          *
105          * \param[in] bQuiet  Whether the module manager should remain silent.
106          *
107          * Normally, the module manager prints out some information to stderr
108          * before it starts the module and after it finishes.  This removes
109          * that output, which is useful in particular for unit tests so that
110          * they don't spam stderr.
111          */
112         void setQuiet(bool bQuiet);
113
114         /*! \brief
115          * Adds a given module to this manager.
116          *
117          * \param   module  Module to add.
118          * \throws  std::bad_alloc if out of memory.
119          *
120          * The manager takes ownership of the object.
121          *
122          * This method is public mostly for testing purposes; for typical uses,
123          * registerModule() is a more convenient way of adding modules.
124          *
125          * \see registerModule()
126          */
127         void addModule(CommandLineModulePointer module);
128         /*! \brief
129          * Registers a module of a certain type to this manager.
130          *
131          * \tparam  Module  Type of module to register.
132          * \throws  std::bad_alloc if out of memory.
133          *
134          * \p Module must be default-constructible and implement
135          * CommandLineModuleInterface.
136          *
137          * This method is provided as a convenient alternative to addModule()
138          * for cases where each module is implemented by a different type
139          * (which should be the case for typical situations outside unit
140          * tests).
141          */
142         template <class Module>
143         void registerModule()
144         {
145             addModule(CommandLineModulePointer(new Module));
146         }
147
148         /*! \brief
149          * Make given help topic available through the manager's help module.
150          *
151          * \param[in]  topic  Help topic to add.
152          * \throws     std::bad_alloc if out of memory.
153          *
154          * The manager takes ownership of the help topic.
155          */
156         void addHelpTopic(HelpTopicPointer topic);
157
158         /*! \brief
159          * Runs a module based on given command line.
160          *
161          * \param[in] argc  Number of elements in \p argv.
162          * \param[in] argv  Command-line arguments.
163          * \throws   unspecified  Throws any exception that the selected module
164          *      throws.
165          * \returns  Exit code for the program.
166          * \retval   0 on successful termination.
167          * \retval   2 if no module is specified, or if the module is not found.
168          *
169          * Runs the module whose name matches \p argv[1].
170          */
171         int run(int argc, char *argv[]);
172
173     private:
174         class Impl;
175
176         PrivateImplPointer<Impl> impl_;
177 };
178
179 } // namespace gmx
180
181 #endif