Separate job script files for gmxapi package versions.
[gromacs.git] / src / gromacs / commandline / cmdlinehelpcontext.cpp
blobaa7216cb5bf10127cafeab529ef461125fe702e1
1 /*
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, 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.
36 /*! \internal \file
37 * \brief
38 * Implements gmx::CommandLineHelpContext.
40 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_commandline
43 #include "gmxpre.h"
45 #include "cmdlinehelpcontext.h"
47 #include "gromacs/utility/gmxassert.h"
49 #include "shellcompletions.h"
51 namespace gmx
54 namespace
57 /*! \brief
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 const CommandLineHelpContext* g_globalContext = nullptr;
67 } // namespace
69 /*! \internal \brief
70 * Private implementation class for CommandLineHelpContext.
72 * \ingroup module_commandline
74 class CommandLineHelpContext::Impl
76 public:
77 //! Creates the implementation class and the low-level context.
78 Impl(TextWriter* writer, HelpOutputFormat format, const HelpLinks* links) :
79 writerContext_(writer, format, links),
80 moduleDisplayName_("gmx"),
81 completionWriter_(nullptr),
82 bHidden_(false)
85 //! Creates an implementation class from a low-level context.
86 explicit Impl(const HelpWriterContext& writerContext) :
87 writerContext_(writerContext),
88 completionWriter_(nullptr),
89 bHidden_(false)
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_;
103 CommandLineHelpContext::CommandLineHelpContext(TextWriter* writer,
104 HelpOutputFormat format,
105 const HelpLinks* links,
106 const std::string& programName) :
107 impl_(new Impl(writer, format, links))
109 impl_->writerContext_.setReplacement("[PROGRAM]", programName);
112 CommandLineHelpContext::CommandLineHelpContext(const HelpWriterContext& writerContext) :
113 impl_(new Impl(writerContext))
117 CommandLineHelpContext::CommandLineHelpContext(ShellCompletionWriter* writer) :
118 impl_(new Impl(&writer->outputWriter(), eHelpOutputFormat_Other, nullptr))
120 impl_->completionWriter_ = writer;
123 CommandLineHelpContext::CommandLineHelpContext(const CommandLineHelpContext& other) :
124 impl_(new Impl(*other.impl_))
128 CommandLineHelpContext::CommandLineHelpContext(CommandLineHelpContext&& other) noexcept :
129 impl_(std::move(other.impl_))
133 CommandLineHelpContext& CommandLineHelpContext::operator=(CommandLineHelpContext&& other) noexcept
135 impl_ = std::move(other.impl_);
136 return *this;
139 CommandLineHelpContext::~CommandLineHelpContext() {}
141 void CommandLineHelpContext::setModuleDisplayName(const std::string& name)
143 impl_->writerContext_.setReplacement("[THISMODULE]", "[TT]" + name + "[tt]");
144 impl_->moduleDisplayName_ = name;
147 void CommandLineHelpContext::setShowHidden(bool bHidden)
149 impl_->bHidden_ = bHidden;
152 void CommandLineHelpContext::enterSubSection(const std::string& title)
154 impl_->writerContext_.enterSubSection(title);
157 const HelpWriterContext& CommandLineHelpContext::writerContext() const
159 return impl_->writerContext_;
162 const char* CommandLineHelpContext::moduleDisplayName() const
164 return impl_->moduleDisplayName_.c_str();
167 bool CommandLineHelpContext::showHidden() const
169 return impl_->bHidden_;
172 bool CommandLineHelpContext::isCompletionExport() const
174 return impl_->completionWriter_ != nullptr;
177 ShellCompletionWriter& CommandLineHelpContext::shellCompletionWriter() const
179 GMX_RELEASE_ASSERT(isCompletionExport(), "Invalid call when not writing shell completions");
180 return *impl_->completionWriter_;
183 /********************************************************************
184 * GlobalCommandLineHelpContext
187 // static
188 const CommandLineHelpContext* GlobalCommandLineHelpContext::get()
190 return g_globalContext;
193 GlobalCommandLineHelpContext::GlobalCommandLineHelpContext(const CommandLineHelpContext& context)
195 GMX_RELEASE_ASSERT(g_globalContext == nullptr, "Global context set more than once");
196 g_globalContext = &context;
199 GlobalCommandLineHelpContext::~GlobalCommandLineHelpContext()
201 g_globalContext = nullptr;
204 } // namespace gmx