Changeset 1064 for trunk/lib


Ignore:
Timestamp:
Jun 6, 2010, 1:24:05 AM (11 years ago)
Author:
Peter Johansson
Message:

sort options alphabetically. refs #307

Location:
trunk/lib/yat
Files:
2 edited

Legend:

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

    r1039 r1064  
    1 // $Id: CommandLine.cc 2210 2010-03-05 22:59:01Z peter $
     1// $Id: CommandLine.cc 2265 2010-06-05 23:12:10Z peter $
    22
    33/*
     
    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/lib/yat/CommandLine.h

    r1058 r1064  
    22#define _theplu_yat_utility_commandline_
    33
    4 //$Id: CommandLine.h 2247 2010-04-22 00:54:34Z peter $
     4//$Id: CommandLine.h 2265 2010-06-05 23:12:10Z peter $
    55
    66/*
     
    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.