2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013,2014, 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.
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.
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.
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.
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.
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.
37 * Implements gmx::CommandLineHelpWriter.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_commandline
42 #include "cmdlinehelpwriter.h"
46 #include <boost/scoped_ptr.hpp>
48 #include "gromacs/commandline/cmdlinehelpcontext.h"
49 #include "gromacs/onlinehelp/helpformat.h"
50 #include "gromacs/onlinehelp/helpwritercontext.h"
51 #include "gromacs/options/basicoptions.h"
52 #include "gromacs/options/filenameoption.h"
53 #include "gromacs/options/options.h"
54 #include "gromacs/options/optionsvisitor.h"
55 #include "gromacs/options/timeunitmanager.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/file.h"
58 #include "gromacs/utility/stringutil.h"
66 //! \addtogroup module_commandline
69 /********************************************************************
70 * OptionsFormatterInterface
74 * Interface for output format specific formatting of options.
78 class OptionsFormatterInterface
81 virtual ~OptionsFormatterInterface() {}
83 //! Formats the description text block for a section.
84 virtual void formatDescription(const HelpWriterContext &context,
85 const Options §ion) = 0;
86 //! Formats a single file option.
87 virtual void formatFileOption(const HelpWriterContext &context,
88 const FileNameOptionInfo &option) = 0;
89 //! Formats a single non-file, non-selection option.
90 virtual void formatOption(const HelpWriterContext &context,
91 const OptionInfo &option) = 0;
94 /********************************************************************
99 * Output format independent processing of options.
101 * Together with code in CommandLineHelpWriter::writeHelp(), this class
102 * implements the common logic for writing out the help.
103 * An object implementing the OptionsFormatterInterface must be provided to the
104 * constructor, and does the actual formatting that is specific to the output
107 class OptionsFilter : public OptionsVisitor
110 //! Specifies the type of output that formatSelected() produces.
119 * Creates a new filtering object.
121 * \param[in] context Help context to use to write the help.
122 * \param[in] formatter Output format specific formatter.
126 OptionsFilter(const HelpWriterContext &context,
127 OptionsFormatterInterface *formatter)
128 : context_(context), formatter_(*formatter),
129 bShowHidden_(false), filterType_(eSelectOtherOptions),
130 title_(NULL), bDidOutput_(false)
134 //! Sets whether hidden options will be shown.
135 void setShowHidden(bool bShowHidden)
137 bShowHidden_ = bShowHidden;
140 //! Formats selected options using the formatter.
141 void formatSelected(FilterType type, const Options &options,
144 virtual void visitSubSection(const Options §ion);
145 virtual void visitOption(const OptionInfo &option);
148 void writeTitleIfSet()
152 context_.writeTitle(title_);
156 const HelpWriterContext &context_;
157 OptionsFormatterInterface &formatter_;
159 FilterType filterType_;
163 GMX_DISALLOW_COPY_AND_ASSIGN(OptionsFilter);
166 void OptionsFilter::formatSelected(FilterType type, const Options &options,
172 visitSubSection(options);
175 if (type != eSelectDescriptions)
177 context_.writeOptionListEnd();
179 context_.outputFile().writeLine();
183 void OptionsFilter::visitSubSection(const Options §ion)
185 if (filterType_ == eSelectDescriptions)
187 if (!section.description().empty())
191 context_.outputFile().writeLine();
197 formatter_.formatDescription(context_, section);
202 OptionsIterator iterator(section);
203 iterator.acceptSubSections(this);
204 iterator.acceptOptions(this);
207 void OptionsFilter::visitOption(const OptionInfo &option)
209 if (!bShowHidden_ && option.isHidden())
213 if (option.isType<FileNameOptionInfo>())
215 if (filterType_ == eSelectFileOptions)
220 context_.writeOptionListStart();
222 formatter_.formatFileOption(context_,
223 *option.toType<FileNameOptionInfo>());
228 if (filterType_ == eSelectOtherOptions)
233 context_.writeOptionListStart();
235 formatter_.formatOption(context_, option);
241 /********************************************************************
242 * CommonFormatterData
245 class CommonFormatterData
248 explicit CommonFormatterData(const char *timeUnit)
253 const char *timeUnit;
256 /********************************************************************
260 //! Formats the default option value as a string.
262 defaultOptionValue(const OptionInfo &option)
264 if (option.valueCount() == 0
265 || (option.valueCount() == 1 && option.formatValue(0).empty()))
267 return option.formatDefaultValueIfSet();
272 for (int i = 0; i < option.valueCount(); ++i)
278 result.append(option.formatValue(i));
284 //! Formats the flags for a file option as a string.
286 fileOptionFlagsAsString(const FileNameOptionInfo &option, bool bAbbrev)
289 if (option.isInputOutputFile())
291 type = bAbbrev ? "In/Out" : "Input/Output";
293 else if (option.isInputFile())
297 else if (option.isOutputFile())
301 if (!option.isRequired())
303 type += bAbbrev ? ", Opt." : ", Optional";
305 if (option.isLibraryFile())
307 type += bAbbrev ? ", Lib." : ", Library";
312 //! Formats the description for an option as a string.
314 descriptionWithOptionDetails(const CommonFormatterData &common,
315 const OptionInfo &option)
317 std::string description(option.formatDescription());
319 const FloatOptionInfo *floatOption = option.toType<FloatOptionInfo>();
320 const DoubleOptionInfo *doubleOption = option.toType<DoubleOptionInfo>();
321 if ((floatOption != NULL && floatOption->isTime())
322 || (doubleOption != NULL && doubleOption->isTime()))
324 // TODO: It could be nicer to have this in basicoptions.cpp.
325 description = replaceAll(description, "%t", common.timeUnit);
331 /********************************************************************
332 * OptionsExportFormatter
336 * Formatter implementation for help export.
338 class OptionsExportFormatter : public OptionsFormatterInterface
341 //! Creates a helper object for formatting options.
342 OptionsExportFormatter(const CommonFormatterData &common, bool bConsole);
344 virtual void formatDescription(const HelpWriterContext &context,
345 const Options §ion);
346 virtual void formatFileOption(const HelpWriterContext &context,
347 const FileNameOptionInfo &option);
348 virtual void formatOption(const HelpWriterContext &context,
349 const OptionInfo &option);
352 const CommonFormatterData &common_;
353 boost::scoped_ptr<TextTableFormatter> consoleFormatter_;
355 GMX_DISALLOW_COPY_AND_ASSIGN(OptionsExportFormatter);
358 OptionsExportFormatter::OptionsExportFormatter(
359 const CommonFormatterData &common, bool bConsole)
364 consoleFormatter_.reset(new TextTableFormatter());
365 consoleFormatter_->setFirstColumnIndent(1);
366 consoleFormatter_->setFoldLastColumnToNextLine(4);
367 consoleFormatter_->addColumn(NULL, 6, false);
368 consoleFormatter_->addColumn(NULL, 8, false);
369 consoleFormatter_->addColumn(NULL, 10, false);
370 consoleFormatter_->addColumn(NULL, 50, true);
374 void OptionsExportFormatter::formatDescription(
375 const HelpWriterContext &context, const Options §ion)
377 // TODO: Print title for the section?
378 context.writeTextBlock(section.description());
381 void OptionsExportFormatter::formatFileOption(
382 const HelpWriterContext &context, const FileNameOptionInfo &option)
384 const bool bAbbrev = (context.outputFormat() == eHelpOutputFormat_Console);
385 std::string value("<" + option.type() + ">");
386 if (option.maxValueCount() != 1)
390 if (option.minValueCount() == 0)
392 value = "[" + value + "]";
394 std::string defaultValue(defaultOptionValue(option));
395 std::string info = "(" + fileOptionFlagsAsString(option, bAbbrev) + ")";
396 if (!defaultValue.empty())
398 info = "(" + defaultValue + ") " + info;
400 std::string description(option.formatDescription());
401 if (context.outputFormat() == eHelpOutputFormat_Console)
403 consoleFormatter_->clear();
404 consoleFormatter_->addColumnLine(0, "-" + option.name());
405 consoleFormatter_->addColumnLine(1, value);
406 consoleFormatter_->addColumnLine(2, info);
407 consoleFormatter_->addColumnHelpTextBlock(3, context, description);
408 context.outputFile().writeString(consoleFormatter_->formatRow());
413 context.writeOptionItem("-" + option.name(), value, description);
417 void OptionsExportFormatter::formatOption(
418 const HelpWriterContext &context, const OptionInfo &option)
420 std::string name(option.name());
421 std::string value("<" + option.type() + ">");
422 std::string defaultValue(defaultOptionValue(option));
423 std::string description(descriptionWithOptionDetails(common_, option));
424 if (option.isType<BooleanOptionInfo>())
426 name = "[no]" + name;
427 // Old command-line parser doesn't accept any values for these.
428 // value = "[" + value + "]";
431 if (context.outputFormat() == eHelpOutputFormat_Console)
433 consoleFormatter_->clear();
434 consoleFormatter_->addColumnLine(0, "-" + name);
435 consoleFormatter_->addColumnLine(1, value);
436 if (!defaultValue.empty())
438 consoleFormatter_->addColumnLine(2, "(" + defaultValue + ")");
440 consoleFormatter_->addColumnHelpTextBlock(3, context, description);
441 context.outputFile().writeString(consoleFormatter_->formatRow());
445 if (!defaultValue.empty())
447 value += " (" + defaultValue + ")";
449 context.writeOptionItem("-" + name, value, description);
457 /********************************************************************
458 * CommandLineHelpWriter::Impl
462 * Private implementation class for CommandLineHelpWriter.
464 * \ingroup module_commandline
466 class CommandLineHelpWriter::Impl
469 //! Sets the Options object to use for generating help.
470 explicit Impl(const Options &options);
472 //! Options object to use for generating help.
473 const Options &options_;
474 //! Time unit to show in descriptions.
475 std::string timeUnit_;
476 //! Whether to write descriptions to output.
477 bool bShowDescriptions_;
480 CommandLineHelpWriter::Impl::Impl(const Options &options)
481 : options_(options), timeUnit_(TimeUnitManager().timeUnitAsString()),
482 bShowDescriptions_(false)
486 /********************************************************************
487 * CommandLineHelpWriter
490 CommandLineHelpWriter::CommandLineHelpWriter(const Options &options)
491 : impl_(new Impl(options))
495 CommandLineHelpWriter::~CommandLineHelpWriter()
499 CommandLineHelpWriter &CommandLineHelpWriter::setShowDescriptions(bool bSet)
501 impl_->bShowDescriptions_ = bSet;
505 CommandLineHelpWriter &CommandLineHelpWriter::setTimeUnitString(const char *timeUnit)
507 impl_->timeUnit_ = timeUnit;
511 void CommandLineHelpWriter::writeHelp(const CommandLineHelpContext &context)
513 const HelpWriterContext &writerContext = context.writerContext();
515 = (writerContext.outputFormat() == eHelpOutputFormat_Console);
516 CommonFormatterData common(impl_->timeUnit_.c_str());
517 OptionsExportFormatter formatter(common, bConsole);
518 OptionsFilter filter(writerContext, &formatter);
519 filter.setShowHidden(context.showHidden());
521 File &file = writerContext.outputFile();
524 // TODO: Write a proper synopsis, with all the options.
525 writerContext.writeTitle("Synopsis");
526 writerContext.writeTextBlock(context.moduleDisplayName());
527 file.writeLine("\n\n");
530 if (impl_->bShowDescriptions_)
532 filter.formatSelected(OptionsFilter::eSelectDescriptions,
533 impl_->options_, "Description");
535 filter.formatSelected(OptionsFilter::eSelectFileOptions,
536 impl_->options_, "File Options");
537 filter.formatSelected(OptionsFilter::eSelectOtherOptions,
538 impl_->options_, "Options");