Resolve one TODO for command line help tests
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinehelpcontext.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source 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 /*! \libinternal \file
36  * \brief
37  * Declares gmx::CommandLineHelpContext.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \inlibraryapi
41  * \ingroup module_commandline
42  */
43 #ifndef GMX_COMMANDLINE_CMDLINEHELPCONTEXT_H
44 #define GMX_COMMANDLINE_CMDLINEHELPCONTEXT_H
45
46 #include <string>
47
48 #include "gromacs/onlinehelp/helpwritercontext.h"
49 #include "gromacs/utility/classhelpers.h"
50
51 namespace gmx
52 {
53
54 class ShellCompletionWriter;
55
56 /*! \libinternal \brief
57  * Context information for writing out command-line help.
58  *
59  * This class wraps a HelpWriterContext, extending it with information specific
60  * for command-line help export.  This way, code using only the routines in the
61  * onlinehelp module is not exposed to extra features of the command-line help
62  * export.
63  *
64  * Copying a context works like with HelpWriterContext: the output file and
65  * most state is shared.  However, setModuleDisplayName() and setShowHidden()
66  * can be set independently for the child context.  Defaults for these options
67  * are inherited from the parent.
68  *
69  * \ingroup module_commandline
70  */
71 class CommandLineHelpContext
72 {
73     public:
74         /*! \brief
75          * Creates a context for help export.
76          *
77          * Wraps the constructor of HelpWriterContext.
78          */
79         CommandLineHelpContext(File *file, HelpOutputFormat format,
80                                const HelpLinks *links,
81                                const std::string &programName);
82         //! Creates a context for a particular HelpWriterContext.
83         explicit CommandLineHelpContext(const HelpWriterContext &writerContext);
84         /*! \brief
85          * Creates a context for shell completion.
86          */
87         explicit CommandLineHelpContext(ShellCompletionWriter *writer);
88         //! Creates a copy of the context.
89         explicit CommandLineHelpContext(const CommandLineHelpContext &other);
90         ~CommandLineHelpContext();
91
92         /*! \brief
93          * Sets a display name for the module for which help is being written.
94          *
95          * \throws std::bad_alloc if out of memory.
96          */
97         void setModuleDisplayName(const std::string &name);
98         //! Sets whether hidden options should be shown in help output.
99         void setShowHidden(bool bHidden);
100         //! \copydoc HelpWriterContext::enterSubSection()
101         void enterSubSection(const std::string &title);
102
103         //! Returns the lower-level context for writing the help.
104         const HelpWriterContext &writerContext() const;
105         /*! \brief
106          * Returns a display name for the module for which help is being written.
107          *
108          * Does not throw.
109          */
110         const char *moduleDisplayName() const;
111         //! Returns whether hidden options should be shown in help output.
112         bool showHidden() const;
113         //! Returns whether this context is for exporting shell completions.
114         bool isCompletionExport() const;
115         /*! \brief
116          * Returns the shell completion writer for this context.
117          *
118          * Can only be called if isCompletionExport() returns `true`.
119          */
120         ShellCompletionWriter &shellCompletionWriter() const;
121
122     private:
123         class Impl;
124
125         PrivateImplPointer<Impl> impl_;
126
127         GMX_DISALLOW_ASSIGN(CommandLineHelpContext);
128 };
129
130 /*! \libinternal \brief
131  * Helper for passing CommandLineHelpContext into parse_common_args().
132  *
133  * This class provides a mechanism to set and retrieve a global
134  * CommandLineHelpContext object.  It is used to pass this object into
135  * parse_common_args() from CommandLineModuleManager::runAsMainCMain() through
136  * the main() function that is not aware of the wrapper binary mechanism.
137  * It is not thread-safe because in this limited use case, it is always called
138  * from a single-threaded context.
139  *
140  * \inlibraryapi
141  * \ingroup module_onlinehelp
142  */
143 class GlobalCommandLineHelpContext
144 {
145     public:
146         //! Returns the global context, or NULL if not set.
147         static const CommandLineHelpContext *get();
148
149         /*! \brief
150          * Sets the global context for the scope.
151          *
152          * The global context is cleared when this object goes out of scope.
153          *
154          * It is an error to have more than one GlobalCommandLineHelpContext
155          * object in existence at the same time.
156          */
157         explicit GlobalCommandLineHelpContext(const CommandLineHelpContext &context);
158         //! Clears the global context.
159         ~GlobalCommandLineHelpContext();
160 };
161
162 } // namespace gmx
163
164 #endif