Changeset 10220


Ignore:
Timestamp:
Mar 3, 2011, 7:28:48 PM (9 years ago)
Author:
tbretz
Message:
Added fEnvMap and a default name mapper.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/Configuration.cc

    r10198 r10220  
    166166If the second option is false, the options will not be displayed in any
    167167\b --help directive, but are available to the user. Each of the functions
    168 can be called more than once.
     168can be called more than once. If an option should be available from
     169all kind of inputs AddOptions() can be used which will call all
     170four other AddOptions() functions.
    169171
    170172A special case are the options from environment variables. Since you might
    171 want to use the same option-key for the command-line and the environment
     173want to use the same option-key for the command-line and the environment,
    172174a mapping is needed (e.g. from \b PATH to \b --path). This mapping
    173 can be implemented by a mapping function and be initialized like this:
     175can be implemented by a mapping function or by the build in mapping
     176and be initialized like this:
    174177
    175178\code
    176179
    177    const string name_mapper(const string &str)
     180   conf.AddEnv("path", "PATH");
     181
     182\endcode
     183
     184or
     185
     186\code
     187
     188   const string name_mapper(const string str)
    178189   {
    179190      return str=="PATH" ? "path" : "";
     
    421432//!
    422433//
    423 Configuration::Configuration(const string &prgname) : fName(prgname), fNameMapper(&NameMapper)
     434Configuration::Configuration(const string &prgname) : fName(prgname),
     435fNameMapper(bind1st(mem_fun(&Configuration::DefaultMapper), this))
    424436{
    425437    po::options_description generic("Generic options");
     
    665677//!
    666678//
    667 void Configuration::SetNameMapper(const string (*mapper)(const string&))
    668 {
    669     fNameMapper = mapper;
     679void Configuration::SetNameMapper(const boost::function1<std::string, std::string> &func)
     680{
     681    fNameMapper = func;
     682}
     683
     684void Configuration::SetNameMapper()
     685{
     686    fNameMapper = bind1st(mem_fun(&Configuration::DefaultMapper), this);
    670687}
    671688
     
    902919    // ------------------------ (11) -------------------------
    903920
    904     const po::parsed_options parsed_environment = po::parse_environment(opt_environment, *fNameMapper);
     921    const po::parsed_options parsed_environment = po::parse_environment(opt_environment, fNameMapper);
    905922
    906923    // ------------------------ (12) -------------------------
  • trunk/FACT++/src/Configuration.h

    r10183 r10220  
    33
    44#include <boost/program_options.hpp>
    5 /*
    6 \attention
    7 \callgraph
    8 \callergraph
    9 \category
    10 \dot
    11 \remark
    12 \see
    13 \throws
    14 \todo
    15 \warning
    16 
    17 \e  italic
    18 \b  bold
    19 \c  typewriter
    20 
    21 \code
    22 \endcode
    23 
    24 \f$
    25 \f[ \f]
    26 */
    275
    286namespace po = boost::program_options;
     
    3917
    4018    const std::string fName; /// argv[0]
     19
     20    std::map<std::string, std::string> fEnvMap;
    4121
    4222    po::options_description fOptionsCommandline[2]; /// Description of the command-line options
     
    5939
    6040    /// A default mapper for environment variables skipping all of them
    61     static const std::string NameMapper(const std::string &) { return ""; }
     41    std::string DefaultMapper(const std::string env)
     42    {
     43        return fEnvMap[env];
     44    }
    6245
    6346    /// Pointer to the mapper function for environment variables
    64     const std::string (*fNameMapper)(const std::string&);
     47    boost::function1<std::string, std::string> fNameMapper;
    6548
    6649    /// Helper function which return the max of the two arguments in the first argument
     
    8871    void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
    8972    void AddOptionsDatabase(const po::options_description &db, bool visible=true);
     73    void AddOptions(const po::options_description &opt, bool visible=true)
     74    {
     75        AddOptionsCommandline(opt, visible);
     76        AddOptionsConfigfile(opt, visible);
     77        AddOptionsEnvironment(opt, visible);
     78        AddOptionsDatabase(opt, visible);
     79    }
    9080
    9181    void SetArgumentPositions(const po::positional_options_description &desc);
    9282
    93     void SetNameMapper(const std::string (*mapper)(const std::string&));
     83    void SetNameMapper(const boost::function1<std::string, std::string> &func);
     84    void SetNameMapper();
     85
     86    void AddEnv(const std::string &conf, const std::string &env)
     87    {
     88        fEnvMap[env] = conf;
     89    }
    9490
    9591    // Output
Note: See TracChangeset for help on using the changeset viewer.