Changeset 2265


Ignore:
Timestamp:
Jun 6, 2010, 1:12:10 AM (13 years ago)
Author:
Peter
Message:

adding two functions sort in CommandLine? that can be used to (re)sort the order of option in help output.

Location:
trunk/yat/utility
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/CommandLine.cc

    r2210 r2265  
    3232
    3333#include <algorithm>
     34#include <cassert>
    3435#include <functional>
    3536#include <fstream>
     
    197198
    198199
     200  void CommandLine::sort(void)
     201  {
     202    sort(OptionCompare());
     203  }
     204
     205
    199206  std::vector<std::string> CommandLine::split(std::string str, char del) const
    200207  {
     
    237244
    238245
     246  bool CommandLine::OptionCompare::operator()(const Option* lhs,
     247                                              const Option* rhs) const
     248  {
     249    assert(lhs);
     250    assert(rhs);
     251    std::string lhs_str = lhs->long_name();
     252    if (lhs_str.empty())
     253      lhs_str = lhs->short_name();
     254    std::string rhs_str = rhs->long_name();
     255    if (rhs_str.empty())
     256      rhs_str = rhs->short_name();
     257    return lhs_str < rhs_str;
     258  }
     259
     260
    239261}}} // of namespace utility, yat, and theplu
  • trunk/yat/utility/CommandLine.h

    r2247 r2265  
    2323*/
    2424
     25#include <algorithm>
    2526#include <cctype>
    2627#include <map>
     
    157158
    158159    /**
     160       \brief Sort Options how they will appear in (help) output.
     161
     162       This function will sort the Options in alphabetical order. If
     163       the Option has a long_name, it is used for the sorting;
     164       otherwise, the short_name is used.
     165
     166       \since New in yat 0.7
     167     */
     168    void sort(void);
     169
     170    /**
     171       Like sort(void) but using \a compare to sort Options.
     172
     173       The functor Compare must be a <a
     174       href="http://www.sgi.com/tech/stl/BinaryFunction.html">Binary
     175       Function</a> with both argument types \c const \c Option* and
     176       return type \c bool.
     177
     178       \since New in yat 0.7
     179     */
     180    template<class Compare>
     181    void sort(Compare compare);
     182
     183    /**
    159184       \return something like "Try `<program_name()> --help` for
    160185       more information."
     
    176201    bool parsed_;
    177202    std::string program_name_;
     203
     204    struct OptionCompare
     205    {
     206      bool operator()(const Option*, const Option*) const;
     207    };
     208
    178209  };
    179210
     
    198229  std::ostream& operator<<(std::ostream&, const CommandLine&);
    199230
     231  template<class Compare>
     232  void CommandLine::sort(Compare compare)
     233  {
     234    std::sort(options_.begin(), options_.end(), compare);
     235  }
     236
    200237}}} // end of namespace utility, yat, and theplu
    201238
Note: See TracChangeset for help on using the changeset viewer.