1 /*
2 * This file is part of the GROMACS molecular simulation package.
3 *
4 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
9 *
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
14 *
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
32 *
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
35 */
36 /*! \internal \file
37 * \brief
38 * Implements gmx::CommandLineHelpContext.
39 *
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
42 */
43 #include "gmxpre.h"
44
45 #include "cmdlinehelpcontext.h"
46
47 #include "gromacs/utility/gmxassert.h"
48
49 #include "shellcompletions.h"
50
51 namespace gmx
52 {
53
54 namespace
55 {
56
57 /*! \brief
58 * Stores the global context set with GlobalCommandLineHelpContext.
59 *
60 * This is not protected by a mutex, since it is only used in command-line
61 * start-up (i.e., single-threaded context), and is inherently not thread-safe.
62 *
63 * \ingroup module_commandline
64 */
65 const CommandLineHelpContext* g_globalContext = nullptr;
66
67 } // namespace
68
69 /*! \internal \brief
70 * Private implementation class for CommandLineHelpContext.
71 *
72 * \ingroup module_commandline
73 */
74 class CommandLineHelpContext::Impl
75 {
76 public:
77 //! Creates the implementation class and the low-level context.
Impl(TextWriter * writer,HelpOutputFormat format,const HelpLinks * links)78 Impl(TextWriter* writer, HelpOutputFormat format, const HelpLinks* links) :
79 writerContext_(writer, format, links),
80 moduleDisplayName_("gmx"),
81 completionWriter_(nullptr),
82 bHidden_(false)
83 {
84 }
85 //! Creates an implementation class from a low-level context.
Impl(const HelpWriterContext & writerContext)86 explicit Impl(const HelpWriterContext& writerContext) :
87 writerContext_(writerContext),
88 completionWriter_(nullptr),
89 bHidden_(false)
90 {
91 }
92
93 //! Wrapped lower-level context.
94 HelpWriterContext writerContext_;
95 //! Display name for the module for which help is written.
96 std::string moduleDisplayName_;
97 //! Shell completion writer (`NULL` if not doing completions).
98 ShellCompletionWriter* completionWriter_;
99 //! Whether hidden options should be shown in help output.
100 bool bHidden_;
101 };
102
CommandLineHelpContext(TextWriter * writer,HelpOutputFormat format,const HelpLinks * links,const std::string & programName)103 CommandLineHelpContext::CommandLineHelpContext(TextWriter* writer,
104 HelpOutputFormat format,
105 const HelpLinks* links,
106 const std::string& programName) :
107 impl_(new Impl(writer, format, links))
108 {
109 impl_->writerContext_.setReplacement("[PROGRAM]", programName);
110 }
111
CommandLineHelpContext(const HelpWriterContext & writerContext)112 CommandLineHelpContext::CommandLineHelpContext(const HelpWriterContext& writerContext) :
113 impl_(new Impl(writerContext))
114 {
115 }
116
CommandLineHelpContext(ShellCompletionWriter * writer)117 CommandLineHelpContext::CommandLineHelpContext(ShellCompletionWriter* writer) :
118 impl_(new Impl(&writer->outputWriter(), eHelpOutputFormat_Other, nullptr))
119 {
120 impl_->completionWriter_ = writer;
121 }
122
CommandLineHelpContext(const CommandLineHelpContext & other)123 CommandLineHelpContext::CommandLineHelpContext(const CommandLineHelpContext& other) :
124 impl_(new Impl(*other.impl_))
125 {
126 }
127
CommandLineHelpContext(CommandLineHelpContext && other)128 CommandLineHelpContext::CommandLineHelpContext(CommandLineHelpContext&& other) noexcept :
129 impl_(std::move(other.impl_))
130 {
131 }
132
operator =(CommandLineHelpContext && other)133 CommandLineHelpContext& CommandLineHelpContext::operator=(CommandLineHelpContext&& other) noexcept
134 {
135 impl_ = std::move(other.impl_);
136 return *this;
137 }
138
~CommandLineHelpContext()139 CommandLineHelpContext::~CommandLineHelpContext() {}
140
setModuleDisplayName(const std::string & name)141 void CommandLineHelpContext::setModuleDisplayName(const std::string& name)
142 {
143 impl_->writerContext_.setReplacement("[THISMODULE]", "[TT]" + name + "[tt]");
144 impl_->moduleDisplayName_ = name;
145 }
146
setShowHidden(bool bHidden)147 void CommandLineHelpContext::setShowHidden(bool bHidden)
148 {
149 impl_->bHidden_ = bHidden;
150 }
151
enterSubSection(const std::string & title)152 void CommandLineHelpContext::enterSubSection(const std::string& title)
153 {
154 impl_->writerContext_.enterSubSection(title);
155 }
156
writerContext() const157 const HelpWriterContext& CommandLineHelpContext::writerContext() const
158 {
159 return impl_->writerContext_;
160 }
161
moduleDisplayName() const162 const char* CommandLineHelpContext::moduleDisplayName() const
163 {
164 return impl_->moduleDisplayName_.c_str();
165 }
166
showHidden() const167 bool CommandLineHelpContext::showHidden() const
168 {
169 return impl_->bHidden_;
170 }
171
isCompletionExport() const172 bool CommandLineHelpContext::isCompletionExport() const
173 {
174 return impl_->completionWriter_ != nullptr;
175 }
176
shellCompletionWriter() const177 ShellCompletionWriter& CommandLineHelpContext::shellCompletionWriter() const
178 {
179 GMX_RELEASE_ASSERT(isCompletionExport(), "Invalid call when not writing shell completions");
180 return *impl_->completionWriter_;
181 }
182
183 /********************************************************************
184 * GlobalCommandLineHelpContext
185 */
186
187 // static
get()188 const CommandLineHelpContext* GlobalCommandLineHelpContext::get()
189 {
190 return g_globalContext;
191 }
192
GlobalCommandLineHelpContext(const CommandLineHelpContext & context)193 GlobalCommandLineHelpContext::GlobalCommandLineHelpContext(const CommandLineHelpContext& context)
194 {
195 GMX_RELEASE_ASSERT(g_globalContext == nullptr, "Global context set more than once");
196 g_globalContext = &context;
197 }
198
~GlobalCommandLineHelpContext()199 GlobalCommandLineHelpContext::~GlobalCommandLineHelpContext()
200 {
201 g_globalContext = nullptr;
202 }
203
204 } // namespace gmx
205