More flexible handling of enum option descriptions.
authorTeemu Murtola <teemu.murtola@gmail.com>
Fri, 21 Dec 2012 05:59:10 +0000 (07:59 +0200)
committerTeemu Murtola <teemu.murtola@gmail.com>
Thu, 7 Mar 2013 18:57:28 +0000 (20:57 +0200)
Instead of always appending the list of allowed values to the
description of enum options, expose the list through StringOptionInfo
and construct the final description in cmdlinehelpwriter.cpp.

This gives better control for printing the option lists in different
formats.  Also allows removal of AbstractOption::createDescription(),
streamlining the core option implementation slightly.

Prerequisite for #969.

Change-Id: I26494f79757ad6894f1930b1bff2f2c74cc26f9c

src/gromacs/commandline/cmdlinehelpwriter.cpp
src/gromacs/options/abstractoption.cpp
src/gromacs/options/abstractoption.h
src/gromacs/options/basicoptions.cpp
src/gromacs/options/basicoptions.h
src/gromacs/options/basicoptionstorage.h

index 0545af2faf1e3be1444392970d8fa29a0e2449f1..d91cc3f19a8f1fba511723f8c970bf1e1da9733e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -399,6 +399,7 @@ void OptionsConsoleFormatter::formatOption(
     {
         genericOptionFormatter_.setColumnFirstLineOffset(1, 1);
     }
+
     // TODO: Better handling of multiple long values
     std::string values;
     for (int i = 0; i < option.valueCount(); ++i)
@@ -410,12 +411,29 @@ void OptionsConsoleFormatter::formatOption(
         values.append(option.formatValue(i));
     }
     genericOptionFormatter_.addColumnLine(2, values);
+
     std::string             description(context.substituteMarkup(option.description()));
     const DoubleOptionInfo *doubleOption = option.toType<DoubleOptionInfo>();
     if (doubleOption != NULL && doubleOption->isTime())
     {
         description = replaceAll(description, "%t", common_.timeUnit);
     }
+    const StringOptionInfo *stringOption = option.toType<StringOptionInfo>();
+    if (stringOption != NULL && stringOption->isEnumerated())
+    {
+        const std::vector<std::string> &allowedValues
+            = stringOption->allowedValues();
+        description.append(": ");
+        for (size_t i = 0; i < allowedValues.size(); ++i)
+        {
+            if (i > 0)
+            {
+                description.append(i + 1 < allowedValues.size()
+                                   ? ", " : ", or ");
+            }
+            description.append(allowedValues[i]);
+        }
+    }
     genericOptionFormatter_.addColumnLine(3, description);
     if (values.length() > 6U)
     {
index 90da5ac92f277d865939eb542cb828623ee3be89..6d0bef51708d90ea0e6202e59528a8ae1a3ba1f5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -72,7 +72,10 @@ AbstractOptionStorage::AbstractOptionStorage(const AbstractOption &settings,
     {
         name_  = settings.name_;
     }
-    descr_ = settings.createDescription();
+    if (settings.descr_ != NULL)
+    {
+        descr_ = settings.descr_;
+    }
     setFlag(efOption_ClearOnNextSet);
 }
 
index 63e30524ff04f6b9a538e453e968d38870a8e037..e80ad0200e64a4c7a012e33076142ce79395ba7b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -122,20 +122,6 @@ class AbstractOption
          */
         virtual AbstractOptionStoragePointer createStorage() const = 0;
 
-        /*! \brief
-         * Creates the description string for the option.
-         *
-         * \returns Description string for the option.
-         *
-         * This function is virtual to allow derived classes to customize the
-         * description programmatically, e.g., by adding the list of allowed
-         * values.
-         * The default implementation simply returns the user-provided
-         * description.
-         */
-        virtual std::string createDescription() const
-        { return descr_ ? descr_ : ""; }
-
         //! Sets the description for the option.
         void setDescription(const char *descr) { descr_ = descr; }
         //! Sets a flag for the option.
index 869c9214111a482f9a2be1e6483d2c0ff9b51041..f7209545a7ce28c948a60e40f45125efb538f594 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -418,6 +418,26 @@ StringOptionInfo::StringOptionInfo(StringOptionStorage *option)
 {
 }
 
+StringOptionStorage &StringOptionInfo::option()
+{
+    return static_cast<StringOptionStorage &>(OptionInfo::option());
+}
+
+const StringOptionStorage &StringOptionInfo::option() const
+{
+    return static_cast<const StringOptionStorage &>(OptionInfo::option());
+}
+
+bool StringOptionInfo::isEnumerated() const
+{
+    return !allowedValues().empty();
+}
+
+const std::vector<std::string> &StringOptionInfo::allowedValues() const
+{
+    return option().allowedValues();
+}
+
 /********************************************************************
  * StringOption
  */
@@ -427,23 +447,4 @@ AbstractOptionStoragePointer StringOption::createStorage() const
     return AbstractOptionStoragePointer(new StringOptionStorage(*this));
 }
 
-std::string StringOption::createDescription() const
-{
-    std::string value(MyBase::createDescription());
-
-    if (enumValues_ != NULL)
-    {
-        value.append(": ");
-        for (int i = 0; enumValues_[i] != NULL; ++i)
-        {
-            value.append(enumValues_[i]);
-            if (enumValues_[i + 1] != NULL)
-            {
-                value.append(enumValues_[i + 2] != NULL ? ", " : ", or ");
-            }
-        }
-    }
-    return value;
-}
-
 } // namespace gmx
index 52504ac6a9f74a15d8879e57a048b5637a7b0c1c..8a318f14e0cad6cc16b608209a92d76e0c0af61b 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -271,7 +271,6 @@ class StringOption : public OptionTemplate<std::string, StringOption>
     private:
         //! Creates a StringOptionStorage object.
         virtual AbstractOptionStoragePointer createStorage() const;
-        virtual std::string createDescription() const;
 
         const char *const      *enumValues_;
         int                     defaultEnumIndex_;
@@ -347,6 +346,24 @@ class StringOptionInfo : public OptionInfo
     public:
         //! Creates an option info object for the given option.
         explicit StringOptionInfo(StringOptionStorage *option);
+
+        /*! \brief
+         * Whether this option accepts an enumerated set of values.
+         *
+         * Returns true if StringOption::enumValues() was used when creating
+         * this option.
+         */
+        bool isEnumerated() const;
+        /*! \brief
+         * Returns the set of allowed values for this option.
+         *
+         * Returns an empty vector if isEnumerated() returns false.
+         */
+        const std::vector<std::string> &allowedValues() const;
+
+    private:
+        StringOptionStorage &option();
+        const StringOptionStorage &option() const;
 };
 
 /*!\}*/
index 2eb4dff7133ac2075a6effc40cbd965069730fde..e65098e204642b445866dddab4e28c1797bab348 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the GROMACS molecular simulation package.
  *
- * Copyright (c) 2010,2011,2012, by the GROMACS development team, led by
+ * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
  * David van der Spoel, Berk Hess, Erik Lindahl, and including many
  * others, as listed in the AUTHORS file in the top-level source
  * directory and at http://www.gromacs.org.
@@ -151,6 +151,9 @@ class StringOptionStorage : public OptionStorageTemplate<std::string>
         virtual const char *typeString() const { return allowed_.empty() ? "string" : "enum"; }
         virtual std::string formatSingleValue(const std::string &value) const;
 
+        //! \copydoc StringOptionInfo::allowedValues()
+        const ValueList &allowedValues() const { return allowed_; }
+
     private:
         virtual void convertValue(const std::string &value);
         virtual void refreshValues();