2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
5 * Copyright (c) 2019,2020,2021, 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.
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.
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.
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.
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.
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.
38 * Implements gmx::CommandLineHelpContext.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
45 #include "cmdlinehelpcontext.h"
47 #include "gromacs/utility/gmxassert.h"
49 #include "shellcompletions.h"
58 * Stores the global context set with GlobalCommandLineHelpContext.
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.
63 * \ingroup module_commandline
65 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
66 const CommandLineHelpContext* g_globalContext = nullptr;
71 * Private implementation class for CommandLineHelpContext.
73 * \ingroup module_commandline
75 class CommandLineHelpContext::Impl
78 //! Creates the implementation class and the low-level context.
79 Impl(TextWriter* writer, HelpOutputFormat format, const HelpLinks* links) :
80 writerContext_(writer, format, links),
81 moduleDisplayName_("gmx"),
82 completionWriter_(nullptr),
86 //! Creates an implementation class from a low-level context.
87 explicit Impl(const HelpWriterContext& writerContext) :
88 writerContext_(writerContext), completionWriter_(nullptr), bHidden_(false)
92 //! Wrapped lower-level context.
93 HelpWriterContext writerContext_;
94 //! Display name for the module for which help is written.
95 std::string moduleDisplayName_;
96 //! Shell completion writer (`NULL` if not doing completions).
97 ShellCompletionWriter* completionWriter_;
98 //! Whether hidden options should be shown in help output.
102 CommandLineHelpContext::CommandLineHelpContext(TextWriter* writer,
103 HelpOutputFormat format,
104 const HelpLinks* links,
105 const std::string& programName) :
106 impl_(new Impl(writer, format, links))
108 impl_->writerContext_.setReplacement("[PROGRAM]", programName);
111 CommandLineHelpContext::CommandLineHelpContext(const HelpWriterContext& writerContext) :
112 impl_(new Impl(writerContext))
116 CommandLineHelpContext::CommandLineHelpContext(ShellCompletionWriter* writer) :
117 impl_(new Impl(&writer->outputWriter(), eHelpOutputFormat_Other, nullptr))
119 impl_->completionWriter_ = writer;
122 CommandLineHelpContext::CommandLineHelpContext(const CommandLineHelpContext& other) :
123 impl_(new Impl(*other.impl_))
127 CommandLineHelpContext::CommandLineHelpContext(CommandLineHelpContext&& other) noexcept :
128 impl_(std::move(other.impl_))
132 CommandLineHelpContext& CommandLineHelpContext::operator=(CommandLineHelpContext&& other) noexcept
134 impl_ = std::move(other.impl_);
138 CommandLineHelpContext::~CommandLineHelpContext() {}
140 void CommandLineHelpContext::setModuleDisplayName(const std::string& name)
142 impl_->writerContext_.setReplacement("[THISMODULE]", "[TT]" + name + "[tt]");
143 impl_->moduleDisplayName_ = name;
146 void CommandLineHelpContext::setShowHidden(bool bHidden)
148 impl_->bHidden_ = bHidden;
151 void CommandLineHelpContext::enterSubSection(const std::string& title)
153 impl_->writerContext_.enterSubSection(title);
156 const HelpWriterContext& CommandLineHelpContext::writerContext() const
158 return impl_->writerContext_;
161 const char* CommandLineHelpContext::moduleDisplayName() const
163 return impl_->moduleDisplayName_.c_str();
166 bool CommandLineHelpContext::showHidden() const
168 return impl_->bHidden_;
171 bool CommandLineHelpContext::isCompletionExport() const
173 return impl_->completionWriter_ != nullptr;
176 ShellCompletionWriter& CommandLineHelpContext::shellCompletionWriter() const
178 GMX_RELEASE_ASSERT(isCompletionExport(), "Invalid call when not writing shell completions");
179 return *impl_->completionWriter_;
182 /********************************************************************
183 * GlobalCommandLineHelpContext
187 const CommandLineHelpContext* GlobalCommandLineHelpContext::get()
189 return g_globalContext;
192 GlobalCommandLineHelpContext::GlobalCommandLineHelpContext(const CommandLineHelpContext& context)
194 GMX_RELEASE_ASSERT(g_globalContext == nullptr, "Global context set more than once");
195 g_globalContext = &context;
198 GlobalCommandLineHelpContext::~GlobalCommandLineHelpContext()
200 g_globalContext = nullptr;