2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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 "gromacs/commandline/cmdlinehelpcontext.h"
47 #include "gromacs/onlinehelp/helpformat.h"
48 #include "gromacs/onlinehelp/helpwritercontext.h"
49 #include "gromacs/options/basicoptions.h"
50 #include "gromacs/options/filenameoption.h"
51 #include "gromacs/options/options.h"
52 #include "gromacs/options/optionsvisitor.h"
53 #include "gromacs/options/timeunitmanager.h"
54 #include "gromacs/selection/selectionfileoption.h"
55 #include "gromacs/selection/selectionoption.h"
56 #include "gromacs/utility/exceptions.h"
57 #include "gromacs/utility/file.h"
58 #include "gromacs/utility/stringutil.h"
66 /********************************************************************
67 * OptionsFormatterInterface
71 * Interface for output format specific formatting of options.
75 * \ingroup module_commandline
77 class OptionsFormatterInterface
80 virtual ~OptionsFormatterInterface() {}
82 //! Formats the description text block for a section.
83 virtual void formatDescription(const HelpWriterContext &context,
84 const Options §ion) = 0;
85 //! Formats a single file option.
86 virtual void formatFileOption(const HelpWriterContext &context,
87 const FileNameOptionInfo &option) = 0;
88 //! Formats a single non-file, non-selection option.
89 virtual void formatOption(const HelpWriterContext &context,
90 const OptionInfo &option) = 0;
91 //! Formats a single selection option.
92 virtual void formatSelectionOption(const HelpWriterContext &context,
93 const OptionInfo &option) = 0;
96 /********************************************************************
101 * Output format independent processing of options.
103 * Together with code in CommandLineHelpWriter::writeHelp(), this class
104 * implements the common logic for writing out the help.
105 * An object implementing the OptionsFormatterInterface must be provided to the
106 * constructor, and does the actual formatting that is specific to the output
109 * \ingroup module_commandline
111 class OptionsFilter : public OptionsVisitor
114 //! Specifies the type of output that formatSelected() produces.
119 eSelectSelectionOptions,
124 * Creates a new filtering object.
126 * \param[in] context Help context to use to write the help.
127 * \param[in] formatter Output format specific formatter.
131 OptionsFilter(const HelpWriterContext &context,
132 OptionsFormatterInterface *formatter)
133 : context_(context), formatter_(*formatter),
134 filterType_(eSelectOtherOptions), bShowHidden_(false),
139 //! Sets whether hidden options will be shown.
140 void setShowHidden(bool bShowHidden)
142 bShowHidden_ = bShowHidden;
145 //! Formats selected options using the formatter.
146 void formatSelected(FilterType type, const Options &options);
148 virtual void visitSubSection(const Options §ion);
149 virtual void visitOption(const OptionInfo &option);
152 const HelpWriterContext &context_;
153 OptionsFormatterInterface &formatter_;
154 FilterType filterType_;
159 void OptionsFilter::formatSelected(FilterType type, const Options &options)
163 visitSubSection(options);
166 context_.outputFile().writeLine();
170 void OptionsFilter::visitSubSection(const Options §ion)
172 if (filterType_ == eSelectDescriptions)
176 context_.outputFile().writeLine();
178 formatter_.formatDescription(context_, section);
182 OptionsIterator iterator(section);
183 iterator.acceptSubSections(this);
184 iterator.acceptOptions(this);
187 void OptionsFilter::visitOption(const OptionInfo &option)
189 if (!bShowHidden_ && option.isHidden())
193 if (option.isType<FileNameOptionInfo>())
195 if (filterType_ == eSelectFileOptions)
197 formatter_.formatFileOption(context_,
198 *option.toType<FileNameOptionInfo>());
203 if (option.isType<SelectionFileOptionInfo>()
204 || option.isType<SelectionOptionInfo>())
206 if (filterType_ == eSelectSelectionOptions)
208 formatter_.formatSelectionOption(context_, option);
213 if (filterType_ == eSelectOtherOptions)
215 formatter_.formatOption(context_, option);
221 /********************************************************************
222 * CommonFormatterData
225 class CommonFormatterData
228 explicit CommonFormatterData(const char *timeUnit)
233 const char *timeUnit;
236 /********************************************************************
237 * OptionsConsoleFormatter
241 * Formatter implementation for console help.
243 * \ingroup module_commandline
245 class OptionsConsoleFormatter : public OptionsFormatterInterface
248 //! Creates a helper object for formatting options help for console.
249 explicit OptionsConsoleFormatter(const CommonFormatterData &common);
251 virtual void formatDescription(const HelpWriterContext &context,
252 const Options §ion);
253 virtual void formatFileOption(const HelpWriterContext &context,
254 const FileNameOptionInfo &option);
255 virtual void formatOption(const HelpWriterContext &context,
256 const OptionInfo &option);
257 virtual void formatSelectionOption(const HelpWriterContext &context,
258 const OptionInfo &option);
261 const CommonFormatterData &common_;
262 TextTableFormatter fileOptionFormatter_;
263 TextTableFormatter genericOptionFormatter_;
264 TextTableFormatter selectionOptionFormatter_;
267 OptionsConsoleFormatter::OptionsConsoleFormatter(const CommonFormatterData &common)
270 fileOptionFormatter_.addColumn("Option", 6, false);
271 fileOptionFormatter_.addColumn("Filename", 12, false);
272 fileOptionFormatter_.addColumn("Type", 12, false);
273 fileOptionFormatter_.addColumn("Description", 45, true);
275 genericOptionFormatter_.addColumn("Option", 12, false);
276 genericOptionFormatter_.addColumn("Type", 6, false);
277 genericOptionFormatter_.addColumn("Value", 6, false);
278 genericOptionFormatter_.addColumn("Description", 51, true);
280 selectionOptionFormatter_.addColumn("Selection", 10, false);
281 selectionOptionFormatter_.addColumn("Description", 67, true);
284 void OptionsConsoleFormatter::formatDescription(
285 const HelpWriterContext &context, const Options §ion)
287 if (!section.description().empty())
289 File &file = context.outputFile();
290 const std::string &title = section.title();
293 file.writeLine(title);
296 context.writeTextBlock(section.description());
300 void OptionsConsoleFormatter::formatFileOption(
301 const HelpWriterContext &context, const FileNameOptionInfo &option)
303 int firstShortValue = 0; // The first value after which the type fits.
304 int firstLongValue = -1; // First value that overlaps description column.
305 int lastLongValue = -1; // Last value like the above.
307 // Get the values to write and check where text overflows the columns.
308 fileOptionFormatter_.clear();
309 std::string name(formatString("-%s", option.name().c_str()));
310 fileOptionFormatter_.addColumnLine(0, name);
311 for (int i = 0; i < option.valueCount() || i == 0; ++i)
314 if (option.valueCount() == 0
315 || (option.valueCount() == 1 && option.formatValue(0).empty()))
317 value = option.formatDefaultValueIfSet();
321 value = option.formatValue(i);
323 fileOptionFormatter_.addColumnLine(1, value);
324 if (value.length() > 12U && i == firstShortValue)
326 firstShortValue = i + 1;
328 if (value.length() > 25U)
330 if (firstLongValue == -1)
338 if (option.isInputOutputFile())
342 else if (option.isInputFile())
346 else if (option.isOutputFile())
350 if (!option.isRequired())
354 if (option.isLibraryFile())
358 bool bLongType = (type.length() > 12U);
359 fileOptionFormatter_.addColumnLine(2, type);
360 fileOptionFormatter_.addColumnLine(3, context.substituteMarkup(option.description()));
363 if (name.length() > 6U || firstShortValue > 0)
365 fileOptionFormatter_.setColumnFirstLineOffset(1, 1);
366 // Assume that the name is <20 chars, so that the type fits
367 if (firstLongValue >= 0)
373 int firstDescriptionLine = 0;
376 firstDescriptionLine = 1;
378 fileOptionFormatter_.setColumnFirstLineOffset(3, firstDescriptionLine);
379 if (firstLongValue >= 0
380 && fileOptionFormatter_.lastColumnLine(3) >= firstLongValue)
382 firstDescriptionLine = lastLongValue + 1;
383 fileOptionFormatter_.setColumnFirstLineOffset(3, firstDescriptionLine);
386 // Do the formatting.
387 context.outputFile().writeString(fileOptionFormatter_.formatRow());
390 void OptionsConsoleFormatter::formatOption(
391 const HelpWriterContext &context, const OptionInfo &option)
393 genericOptionFormatter_.clear();
394 bool bIsBool = option.isType<BooleanOptionInfo>();
395 std::string name(formatString("-%s%s", bIsBool ? "[no]" : "",
396 option.name().c_str()));
397 genericOptionFormatter_.addColumnLine(0, name);
398 genericOptionFormatter_.addColumnLine(1, option.type());
399 if (name.length() > 12U)
401 genericOptionFormatter_.setColumnFirstLineOffset(1, 1);
404 // TODO: Better handling of multiple long values
406 for (int i = 0; i < option.valueCount(); ++i)
412 values.append(option.formatValue(i));
414 genericOptionFormatter_.addColumnLine(2, values);
416 std::string description(context.substituteMarkup(option.description()));
417 const DoubleOptionInfo *doubleOption = option.toType<DoubleOptionInfo>();
418 if (doubleOption != NULL && doubleOption->isTime())
420 description = replaceAll(description, "%t", common_.timeUnit);
422 const StringOptionInfo *stringOption = option.toType<StringOptionInfo>();
423 if (stringOption != NULL && stringOption->isEnumerated())
425 const std::vector<std::string> &allowedValues
426 = stringOption->allowedValues();
427 description.append(": ");
428 for (size_t i = 0; i < allowedValues.size(); ++i)
432 description.append(i + 1 < allowedValues.size()
435 description.append(allowedValues[i]);
438 genericOptionFormatter_.addColumnLine(3, description);
439 if (values.length() > 6U)
441 genericOptionFormatter_.setColumnFirstLineOffset(3, 1);
444 context.outputFile().writeString(genericOptionFormatter_.formatRow());
447 void OptionsConsoleFormatter::formatSelectionOption(
448 const HelpWriterContext &context, const OptionInfo &option)
450 File &file = context.outputFile();
452 selectionOptionFormatter_.clear();
453 std::string name(formatString("-%s", option.name().c_str()));
454 selectionOptionFormatter_.addColumnLine(0, name);
455 selectionOptionFormatter_.addColumnLine(1, context.substituteMarkup(option.description()));
456 file.writeString(selectionOptionFormatter_.formatRow());
458 TextLineWrapper wrapper;
459 wrapper.settings().setLineLength(77);
460 wrapper.settings().setFirstLineIndent(4);
461 wrapper.settings().setIndent(8);
462 wrapper.settings().setContinuationChar('\\');
463 // TODO: What to do with selection variables?
464 // They are not printed as values for any option.
465 for (int i = 0; i < option.valueCount(); ++i)
467 std::string value(option.formatValue(i));
468 file.writeLine(wrapper.wrapToString(value));
474 /********************************************************************
475 * CommandLineHelpWriter::Impl
479 * Private implementation class for CommandLineHelpWriter.
481 * \ingroup module_commandline
483 class CommandLineHelpWriter::Impl
486 //! Sets the Options object to use for generating help.
487 explicit Impl(const Options &options);
489 //! Options object to use for generating help.
490 const Options &options_;
491 //! Time unit to show in descriptions.
492 std::string timeUnit_;
493 //! Whether to write descriptions to output.
494 bool bShowDescriptions_;
497 CommandLineHelpWriter::Impl::Impl(const Options &options)
498 : options_(options), timeUnit_(TimeUnitManager().timeUnitAsString()),
499 bShowDescriptions_(false)
503 /********************************************************************
504 * CommandLineHelpWriter
507 CommandLineHelpWriter::CommandLineHelpWriter(const Options &options)
508 : impl_(new Impl(options))
512 CommandLineHelpWriter::~CommandLineHelpWriter()
516 CommandLineHelpWriter &CommandLineHelpWriter::setShowDescriptions(bool bSet)
518 impl_->bShowDescriptions_ = bSet;
522 CommandLineHelpWriter &CommandLineHelpWriter::setTimeUnitString(const char *timeUnit)
524 impl_->timeUnit_ = timeUnit;
528 void CommandLineHelpWriter::writeHelp(const CommandLineHelpContext &context)
530 boost::scoped_ptr<OptionsFormatterInterface> formatter;
531 const HelpWriterContext &writerContext = context.writerContext();
532 CommonFormatterData common(impl_->timeUnit_.c_str());
533 switch (writerContext.outputFormat())
535 case eHelpOutputFormat_Console:
536 formatter.reset(new OptionsConsoleFormatter(common));
539 // TODO: Implement once the situation with Redmine issue #969 is
541 GMX_THROW(NotImplementedError(
542 "Command-line help is not implemented for this output format"));
544 OptionsFilter filter(writerContext, formatter.get());
545 filter.setShowHidden(context.showHidden());
547 if (impl_->bShowDescriptions_)
549 File &file = writerContext.outputFile();
550 file.writeLine("DESCRIPTION");
551 file.writeLine("-----------");
553 filter.formatSelected(OptionsFilter::eSelectDescriptions,
556 filter.formatSelected(OptionsFilter::eSelectFileOptions,
558 filter.formatSelected(OptionsFilter::eSelectOtherOptions,
560 filter.formatSelected(OptionsFilter::eSelectSelectionOptions,