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
44 #include "cmdlinehelpwriter.h"
51 #include <boost/scoped_ptr.hpp>
53 #include "gromacs/commandline/cmdlinehelpcontext.h"
54 #include "gromacs/commandline/shellcompletions.h"
55 #include "gromacs/onlinehelp/helpformat.h"
56 #include "gromacs/onlinehelp/helpwritercontext.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/options/timeunitmanager.h"
62 #include "gromacs/utility/arrayref.h"
63 #include "gromacs/utility/exceptions.h"
64 #include "gromacs/utility/file.h"
65 #include "gromacs/utility/stringutil.h"
73 //! \addtogroup module_commandline
76 /********************************************************************
77 * DescriptionsFormatter
80 class DescriptionsFormatter : public OptionsVisitor
84 * Creates a new description formatter.
86 * \param[in] context Help context to use to write the help.
88 explicit DescriptionsFormatter(const HelpWriterContext &context)
89 : context_(context), title_(NULL), bDidOutput_(false)
93 //! Formats all section descriptions from a given options.
94 void format(const Options &options, const char *title)
98 visitSubSection(options);
101 context_.outputFile().writeLine();
105 //! Formats the description for a single subsection and handles recursion.
106 virtual void visitSubSection(const Options §ion);
107 // This method is not used and never called.
108 virtual void visitOption(const OptionInfo & /*option*/) {}
111 const HelpWriterContext &context_;
115 GMX_DISALLOW_COPY_AND_ASSIGN(DescriptionsFormatter);
118 void DescriptionsFormatter::visitSubSection(const Options §ion)
120 if (!section.description().empty())
124 context_.outputFile().writeLine();
126 else if (title_ != NULL)
128 context_.writeTitle(title_);
130 // TODO: Print title for the section?
131 context_.writeTextBlock(section.description());
135 OptionsIterator iterator(section);
136 iterator.acceptSubSections(this);
139 /********************************************************************
140 * OptionsFormatterInterface
144 * Interface for output format specific formatting of options.
148 class OptionsFormatterInterface
151 virtual ~OptionsFormatterInterface() {}
153 //! Formats a single option option.
154 virtual void formatOption(const OptionInfo &option) = 0;
157 /********************************************************************
162 * Output format independent processing of options.
164 * Together with code in CommandLineHelpWriter::writeHelp(), this class
165 * implements the common logic for writing out the help.
166 * An object implementing the OptionsFormatterInterface must be provided to the
167 * constructor, and does the actual formatting that is specific to the output
170 class OptionsFilter : public OptionsVisitor
173 //! Specifies the type of output that formatSelected() produces.
181 * Creates a new filtering object.
186 : formatter_(NULL), filterType_(eSelectOtherOptions),
191 //! Sets whether hidden options will be shown.
192 void setShowHidden(bool bShowHidden)
194 bShowHidden_ = bShowHidden;
197 //! Formats selected options using the formatter.
198 void formatSelected(FilterType type,
199 OptionsFormatterInterface *formatter,
200 const Options &options);
202 virtual void visitSubSection(const Options §ion);
203 virtual void visitOption(const OptionInfo &option);
206 OptionsFormatterInterface *formatter_;
207 FilterType filterType_;
210 GMX_DISALLOW_COPY_AND_ASSIGN(OptionsFilter);
213 void OptionsFilter::formatSelected(FilterType type,
214 OptionsFormatterInterface *formatter,
215 const Options &options)
217 formatter_ = formatter;
219 visitSubSection(options);
222 void OptionsFilter::visitSubSection(const Options §ion)
224 OptionsIterator iterator(section);
225 iterator.acceptSubSections(this);
226 iterator.acceptOptions(this);
229 void OptionsFilter::visitOption(const OptionInfo &option)
231 if (!bShowHidden_ && option.isHidden())
235 if (option.isType<FileNameOptionInfo>())
237 if (filterType_ == eSelectFileOptions)
239 formatter_->formatOption(option);
243 if (filterType_ == eSelectOtherOptions)
245 formatter_->formatOption(option);
250 /********************************************************************
251 * CommonFormatterData
254 class CommonFormatterData
257 explicit CommonFormatterData(const char *timeUnit)
262 const char *timeUnit;
265 /********************************************************************
269 //! Formats option name and value.
270 void formatOptionNameAndValue(const OptionInfo &option, std::string *name,
273 *name = option.name();
274 *value = "<" + option.type() + ">";
275 if (option.isType<FileNameOptionInfo>())
277 // TODO: Make these work also for other option types.
278 if (option.maxValueCount() != 1)
282 if (option.minValueCount() == 0)
284 *value = "[" + *value + "]";
287 if (option.isType<BooleanOptionInfo>())
289 *name = "[no]" + *name;
290 // Old command-line parser doesn't accept any values for these.
291 // value = "[" + value + "]";
296 //! Formats the default option value as a string.
298 defaultOptionValue(const OptionInfo &option)
300 if (option.valueCount() == 0
301 || (option.valueCount() == 1 && option.formatValue(0).empty()))
303 return option.formatDefaultValueIfSet();
308 for (int i = 0; i < option.valueCount(); ++i)
314 result.append(option.formatValue(i));
320 //! Formats the flags for a file option as a string.
322 fileOptionFlagsAsString(const FileNameOptionInfo &option, bool bAbbrev)
325 if (option.isInputOutputFile())
327 type = bAbbrev ? "In/Out" : "Input/Output";
329 else if (option.isInputFile())
333 else if (option.isOutputFile())
337 if (!option.isRequired())
339 type += bAbbrev ? ", Opt." : ", Optional";
341 if (option.isLibraryFile())
343 type += bAbbrev ? ", Lib." : ", Library";
348 //! Formats the description for an option as a string.
350 descriptionWithOptionDetails(const CommonFormatterData &common,
351 const OptionInfo &option)
353 std::string description(option.formatDescription());
355 const FloatOptionInfo *floatOption = option.toType<FloatOptionInfo>();
356 const DoubleOptionInfo *doubleOption = option.toType<DoubleOptionInfo>();
357 if ((floatOption != NULL && floatOption->isTime())
358 || (doubleOption != NULL && doubleOption->isTime()))
360 // TODO: It could be nicer to have this in basicoptions.cpp.
361 description = replaceAll(description, "%t", common.timeUnit);
367 /********************************************************************
368 * OptionsSynopsisFormatter
372 * Formatter implementation for synopsis.
374 class SynopsisFormatter : public OptionsFormatterInterface
377 //! Creates a helper object for formatting the synopsis.
378 explicit SynopsisFormatter(const HelpWriterContext &context)
379 : context_(context), lineLength_(0), indent_(0), currentLength_(0)
383 //! Starts formatting the synopsis.
384 void start(const char *name);
385 //! Finishes formatting the synopsis.
388 virtual void formatOption(const OptionInfo &option);
391 const HelpWriterContext &context_;
396 GMX_DISALLOW_COPY_AND_ASSIGN(SynopsisFormatter);
399 void SynopsisFormatter::start(const char *name)
401 currentLength_ = std::strlen(name) + 1;
402 indent_ = std::min(currentLength_, 13);
403 File &file = context_.outputFile();
404 switch (context_.outputFormat())
406 case eHelpOutputFormat_Console:
408 file.writeString(name);
410 case eHelpOutputFormat_Man:
412 file.writeString(name);
414 case eHelpOutputFormat_Html:
416 file.writeLine("<pre>");
417 file.writeString(name);
420 GMX_THROW(NotImplementedError("Synopsis formatting not implemented for this output format"));
424 void SynopsisFormatter::finish()
426 File &file = context_.outputFile();
428 if (context_.outputFormat() == eHelpOutputFormat_Html)
430 file.writeLine("</pre>");
435 void SynopsisFormatter::formatOption(const OptionInfo &option)
437 std::string name, value;
438 formatOptionNameAndValue(option, &name, &value);
439 std::string fullOptionText(" [-" + name);
442 fullOptionText.append(" ");
443 fullOptionText.append(value);
445 fullOptionText.append("]");
446 const int totalLength = fullOptionText.size();
448 if (context_.outputFormat() == eHelpOutputFormat_Html)
450 value = replaceAll(value, "<", "<");
451 value = replaceAll(value, ">", ">");
454 File &file = context_.outputFile();
455 currentLength_ += totalLength;
456 if (currentLength_ >= lineLength_)
458 file.writeString(formatString("\n%*c", indent_ - 1, ' '));
459 currentLength_ = indent_ - 1 + totalLength;
461 file.writeString(fullOptionText);
464 /********************************************************************
465 * OptionsListFormatter
469 * Formatter implementation for help export.
471 class OptionsListFormatter : public OptionsFormatterInterface
474 //! Creates a helper object for formatting options.
475 OptionsListFormatter(const HelpWriterContext &context,
476 const CommonFormatterData &common,
479 //! Initiates a new section in the options list.
480 void startSection(const char *header)
485 //! Finishes a section in the options list.
490 context_.writeOptionListEnd();
491 context_.outputFile().writeLine();
495 virtual void formatOption(const OptionInfo &option);
498 void writeSectionStartIfNecessary()
502 context_.writeTitle(title_);
509 context_.writeTextBlock(header_);
511 context_.writeOptionListStart();
516 const HelpWriterContext &context_;
517 const CommonFormatterData &common_;
518 boost::scoped_ptr<TextTableFormatter> consoleFormatter_;
523 GMX_DISALLOW_COPY_AND_ASSIGN(OptionsListFormatter);
526 OptionsListFormatter::OptionsListFormatter(
527 const HelpWriterContext &context,
528 const CommonFormatterData &common,
530 : context_(context), common_(common),
531 title_(title), header_(NULL), bDidOutput_(false)
533 if (context.outputFormat() == eHelpOutputFormat_Console)
535 consoleFormatter_.reset(new TextTableFormatter());
536 consoleFormatter_->setFirstColumnIndent(1);
537 consoleFormatter_->setFoldLastColumnToNextLine(4);
538 consoleFormatter_->addColumn(NULL, 6, false);
539 consoleFormatter_->addColumn(NULL, 8, false);
540 consoleFormatter_->addColumn(NULL, 10, false);
541 consoleFormatter_->addColumn(NULL, 50, true);
545 void OptionsListFormatter::formatOption(const OptionInfo &option)
547 writeSectionStartIfNecessary();
548 std::string name, value;
549 formatOptionNameAndValue(option, &name, &value);
550 std::string defaultValue(defaultOptionValue(option));
552 if (!defaultValue.empty())
554 info = "(" + defaultValue + ")";
556 const FileNameOptionInfo *fileOption = option.toType<FileNameOptionInfo>();
557 if (fileOption != NULL)
559 const bool bAbbrev = (context_.outputFormat() == eHelpOutputFormat_Console);
565 info.append(fileOptionFlagsAsString(*fileOption, bAbbrev));
568 std::string description(descriptionWithOptionDetails(common_, option));
569 if (context_.outputFormat() == eHelpOutputFormat_Console)
571 consoleFormatter_->clear();
572 consoleFormatter_->addColumnLine(0, "-" + name);
573 consoleFormatter_->addColumnLine(1, value);
576 consoleFormatter_->addColumnLine(2, info);
578 consoleFormatter_->addColumnHelpTextBlock(3, context_, description);
579 context_.outputFile().writeString(consoleFormatter_->formatRow());
588 context_.writeOptionItem("-" + name, value, description);
596 /********************************************************************
597 * CommandLineHelpWriter::Impl
601 * Private implementation class for CommandLineHelpWriter.
603 * \ingroup module_commandline
605 class CommandLineHelpWriter::Impl
608 //! Sets the Options object to use for generating help.
609 explicit Impl(const Options &options);
611 //! Format the list of known issues.
612 void formatBugs(const HelpWriterContext &context);
614 //! Options object to use for generating help.
615 const Options &options_;
616 //! List of bugs/knows issues.
617 ConstArrayRef<const char *> bugs_;
618 //! Time unit to show in descriptions.
619 std::string timeUnit_;
620 //! Whether to write descriptions to output.
621 bool bShowDescriptions_;
624 CommandLineHelpWriter::Impl::Impl(const Options &options)
625 : options_(options), timeUnit_(TimeUnitManager().timeUnitAsString()),
626 bShowDescriptions_(false)
630 void CommandLineHelpWriter::Impl::formatBugs(const HelpWriterContext &context)
636 context.writeTitle("Known Issues");
637 if (context.outputFormat() != eHelpOutputFormat_Console)
639 context.writeTextBlock("[UL]");
641 ConstArrayRef<const char *>::const_iterator i;
642 for (i = bugs_.begin(); i != bugs_.end(); ++i)
644 const char *const bug = *i;
645 // TODO: The context should be able to do this also for console output, but
646 // that requires a lot more elaborate parser for the markup.
647 if (context.outputFormat() == eHelpOutputFormat_Console)
649 TextLineWrapperSettings settings;
650 settings.setIndent(2);
651 settings.setFirstLineIndent(0);
652 settings.setLineLength(78);
653 context.outputFile().writeLine(
654 context.substituteMarkupAndWrapToString(
655 settings, formatString("* %s", bug)));
659 context.writeTextBlock(formatString("[LI]%s", bug));
662 if (context.outputFormat() != eHelpOutputFormat_Console)
664 context.writeTextBlock("[ul]");
669 /********************************************************************
670 * CommandLineHelpWriter
673 CommandLineHelpWriter::CommandLineHelpWriter(const Options &options)
674 : impl_(new Impl(options))
678 CommandLineHelpWriter::~CommandLineHelpWriter()
682 CommandLineHelpWriter &
683 CommandLineHelpWriter::setShowDescriptions(bool bSet)
685 impl_->bShowDescriptions_ = bSet;
689 CommandLineHelpWriter &
690 CommandLineHelpWriter::setTimeUnitString(const char *timeUnit)
692 impl_->timeUnit_ = timeUnit;
696 CommandLineHelpWriter &
697 CommandLineHelpWriter::setKnownIssues(const ConstArrayRef<const char *> &bugs)
703 void CommandLineHelpWriter::writeHelp(const CommandLineHelpContext &context)
705 if (context.isCompletionExport())
707 context.shellCompletionWriter().writeModuleCompletions(
708 context.moduleDisplayName(), impl_->options_);
711 const HelpWriterContext &writerContext = context.writerContext();
712 OptionsFilter filter;
713 filter.setShowHidden(context.showHidden());
716 writerContext.writeTitle("Synopsis");
717 SynopsisFormatter synopsisFormatter(writerContext);
718 synopsisFormatter.start(context.moduleDisplayName());
719 filter.formatSelected(OptionsFilter::eSelectFileOptions,
720 &synopsisFormatter, impl_->options_);
721 filter.formatSelected(OptionsFilter::eSelectOtherOptions,
722 &synopsisFormatter, impl_->options_);
723 synopsisFormatter.finish();
726 if (impl_->bShowDescriptions_)
728 DescriptionsFormatter descriptionFormatter(writerContext);
729 descriptionFormatter.format(impl_->options_, "Description");
731 CommonFormatterData common(impl_->timeUnit_.c_str());
732 OptionsListFormatter formatter(writerContext, common, "Options");
733 formatter.startSection("Options to specify input and output files:[PAR]");
734 filter.formatSelected(OptionsFilter::eSelectFileOptions,
735 &formatter, impl_->options_);
736 formatter.finishSection();
737 formatter.startSection("Other options:[PAR]");
738 filter.formatSelected(OptionsFilter::eSelectOtherOptions,
739 &formatter, impl_->options_);
740 formatter.finishSection();
742 impl_->formatBugs(writerContext);