Introduce SimulatorBuilder
[gromacs.git] / src / gromacs / commandline / shellcompletions.cpp
blob18b49bc812e1e11cc55a7ca8fa1f891e6cb103cb
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 /*! \internal \file
38 * \brief
39 * Implements gmx::ShellCompletionWriter.
41 * \author Teemu Murtola <teemu.murtola@gmail.com>
42 * \ingroup module_commandline
44 #include "gmxpre.h"
46 #include "shellcompletions.h"
48 #include <cstdio>
50 #include <algorithm>
51 #include <memory>
52 #include <string>
54 #include "gromacs/commandline/cmdlinehelpcontext.h"
55 #include "gromacs/commandline/pargs.h"
56 #include "gromacs/fileio/filetypes.h"
57 #include "gromacs/options/basicoptions.h"
58 #include "gromacs/options/filenameoption.h"
59 #include "gromacs/options/options.h"
60 #include "gromacs/options/optionsvisitor.h"
61 #include "gromacs/utility/arrayref.h"
62 #include "gromacs/utility/exceptions.h"
63 #include "gromacs/utility/gmxassert.h"
64 #include "gromacs/utility/stringutil.h"
65 #include "gromacs/utility/textwriter.h"
67 namespace gmx
70 namespace
73 class OptionsListWriter : public OptionsVisitor
75 public:
76 const std::string &optionList() const { return optionList_; }
78 void visitSection(const OptionSectionInfo &section) override
80 OptionsIterator iterator(section);
81 iterator.acceptSections(this);
82 iterator.acceptOptions(this);
84 void visitOption(const OptionInfo &option) override
86 if (option.isHidden())
88 return;
90 if (!optionList_.empty())
92 optionList_.append("\\n");
94 optionList_.append("-");
95 const BooleanOptionInfo *booleanOption
96 = option.toType<BooleanOptionInfo>();
97 if (booleanOption != nullptr && booleanOption->defaultValue())
99 optionList_.append("no");
101 optionList_.append(option.name());
104 private:
105 std::string optionList_;
108 class OptionCompletionWriter : public OptionsVisitor
110 public:
111 explicit OptionCompletionWriter(TextWriter *out) : out_(*out) {}
113 void visitSection(const OptionSectionInfo &section) override
115 OptionsIterator iterator(section);
116 iterator.acceptSections(this);
117 iterator.acceptOptions(this);
119 void visitOption(const OptionInfo &option) override;
121 private:
122 void writeOptionCompletion(const OptionInfo &option,
123 const std::string &completion);
125 TextWriter &out_;
128 void OptionCompletionWriter::visitOption(const OptionInfo &option)
130 if (option.isHidden())
132 return;
134 const FileNameOptionInfo *fileOption = option.toType<FileNameOptionInfo>();
135 if (fileOption != nullptr)
137 if (fileOption->isDirectoryOption())
139 writeOptionCompletion(option, "compgen -S ' ' -d $c");
140 return;
142 const FileNameOptionInfo::ExtensionList &extensionList = fileOption->extensions();
143 if (extensionList.empty())
145 return;
147 std::string completion("compgen -S ' ' -X '!*");
148 std::string extensions(joinStrings(extensionList, "|"));
149 if (extensionList.size() > 1)
151 extensions = "@(" + extensions + ")";
153 completion.append(extensions);
154 // TODO: Don't duplicate this from filenm.c/futil.c.
155 completion.append("?(.gz|.Z)' -f -- $c ; compgen -S '/' -d $c");
156 writeOptionCompletion(option, completion);
157 return;
159 const StringOptionInfo *stringOption = option.toType<StringOptionInfo>();
160 if (stringOption != nullptr && stringOption->isEnumerated())
162 std::string completion("compgen -S ' ' -W $'");
163 completion.append(joinStrings(stringOption->allowedValues(), "\\n"));
164 completion.append("' -- $c");
165 writeOptionCompletion(option, completion);
166 return;
170 void OptionCompletionWriter::writeOptionCompletion(
171 const OptionInfo &option, const std::string &completion)
173 std::string result(formatString("-%s) ", option.name().c_str()));
174 if (option.maxValueCount() >= 0)
176 result.append(formatString("(( $n <= %d )) && ", option.maxValueCount()));
178 result.append("COMPREPLY=( $(");
179 result.append(completion);
180 result.append("));;");
181 out_.writeLine(result);
184 } // namespace
186 class ShellCompletionWriter::Impl
188 public:
189 Impl(const std::string &binaryName, ShellCompletionFormat /*format*/)
190 : binaryName_(binaryName)
194 std::string completionFunctionName(const char *moduleName) const
196 std::string result =
197 formatString("_%s_%s_compl", binaryName_.c_str(), moduleName);
198 std::replace(result.begin(), result.end(), '-', '_');
199 return result;
202 std::string binaryName_;
203 // Never releases ownership.
204 std::unique_ptr<TextWriter> file_;
207 ShellCompletionWriter::ShellCompletionWriter(const std::string &binaryName,
208 ShellCompletionFormat format)
209 : impl_(new Impl(binaryName, format))
213 ShellCompletionWriter::~ShellCompletionWriter()
217 TextWriter &ShellCompletionWriter::outputWriter()
219 return *impl_->file_;
222 void ShellCompletionWriter::startCompletions()
224 impl_->file_ = std::make_unique<TextWriter>(impl_->binaryName_ + "-completion.bash");
227 void ShellCompletionWriter::writeModuleCompletions(
228 const char *moduleName,
229 const Options &options)
231 TextWriter &out = *impl_->file_;
232 out.writeLine(formatString("%s() {", impl_->completionFunctionName(moduleName).c_str()));
233 out.writeLine("local IFS=$'\\n'");
234 out.writeLine("local c=${COMP_WORDS[COMP_CWORD]}");
235 out.writeLine("local n");
236 out.writeLine("for ((n=1;n<COMP_CWORD;++n)) ; do [[ \"${COMP_WORDS[COMP_CWORD-n]}\" == -* ]] && break ; done");
237 out.writeLine("local p=${COMP_WORDS[COMP_CWORD-n]}");
238 out.writeLine("COMPREPLY=()");
240 OptionsListWriter listWriter;
241 listWriter.visitSection(options.rootSection());
242 out.writeLine(formatString("if (( $COMP_CWORD <= 1 )) || [[ $c == -* ]]; then COMPREPLY=( $(compgen -S ' ' -W $'%s' -- $c)); return 0; fi", listWriter.optionList().c_str()));
244 out.writeLine("case \"$p\" in");
245 OptionCompletionWriter optionWriter(&out);
246 optionWriter.visitSection(options.rootSection());
247 out.writeLine("esac }");
250 void ShellCompletionWriter::writeWrapperCompletions(
251 const ModuleNameList &modules, const Options &options)
253 impl_->file_->writeLine("_" + impl_->binaryName_ + "_compl() {");
254 impl_->file_->writeLine("local i c m");
255 impl_->file_->writeLine("local IFS=$'\\n'\n");
256 impl_->file_->writeLine("COMPREPLY=()");
257 impl_->file_->writeLine("unset COMP_WORDS[0]");
258 impl_->file_->writeLine("for ((i=1;i<COMP_CWORD;++i)) ; do");
259 impl_->file_->writeLine("[[ \"${COMP_WORDS[i]}\" != -* ]] && break");
260 impl_->file_->writeLine("unset COMP_WORDS[i]");
261 impl_->file_->writeLine("done");
262 impl_->file_->writeLine("if (( i == COMP_CWORD )); then");
263 impl_->file_->writeLine("c=${COMP_WORDS[COMP_CWORD]}");
264 OptionsListWriter lister;
265 lister.visitSection(options.rootSection());
266 std::string completions(lister.optionList());
267 for (ModuleNameList::const_iterator i = modules.begin();
268 i != modules.end(); ++i)
270 completions.append("\\n");
271 completions.append(*i);
273 impl_->file_->writeLine("COMPREPLY=( $(compgen -S ' ' -W $'" + completions + "' -- $c) )");
274 impl_->file_->writeLine("return 0");
275 impl_->file_->writeLine("fi");
276 impl_->file_->writeLine("m=${COMP_WORDS[i]}");
277 impl_->file_->writeLine("COMP_WORDS=( \"${COMP_WORDS[@]}\" )");
278 impl_->file_->writeLine("COMP_CWORD=$((COMP_CWORD-i))");
279 impl_->file_->writeLine("case \"$m\" in");
280 for (ModuleNameList::const_iterator i = modules.begin();
281 i != modules.end(); ++i)
283 const char *const name = i->c_str();
284 impl_->file_->writeLine(formatString("%s) %s ;;", name,
285 impl_->completionFunctionName(name).c_str()));
287 impl_->file_->writeLine("esac }");
290 void ShellCompletionWriter::finishCompletions()
292 impl_->file_->close();
293 impl_->file_.reset();
296 } // namespace gmx