Changeset 980 for trunk/yat


Ignore:
Timestamp:
Oct 22, 2007, 2:55:19 AM (14 years ago)
Author:
Peter
Message:

adding possibility to make option required

Location:
trunk/yat/utility
Files:
5 edited

Legend:

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

    r975 r980  
    3636
    3737  Option::Option(CommandLine& cmd, std::string flag, std::string desc)
    38     : description_(desc), present_(false)
     38    : cmd_(cmd), description_(desc), present_(false)
    3939  {
    4040    if (flag.empty())
     
    5858  Option::~Option(void)
    5959  {
     60  }
     61
     62
     63  const CommandLine& Option::cmd(void) const
     64  {
     65    return cmd_;
    6066  }
    6167
  • trunk/yat/utility/Option.h

    r965 r980  
    9999    void validate(void);
    100100
     101  protected:
     102    /**
     103       \return const reference to CommandLine Option belongs to.
     104     */
     105    const CommandLine& cmd(void) const;
     106
    101107  private:
    102108    virtual void do_parse(std::vector<std::string>::iterator,
     
    124130
    125131
     132    const CommandLine& cmd_;
    126133    std::string description_;
    127134    std::string long_name_;
  • trunk/yat/utility/OptionArg.h

    r975 r980  
    2626
    2727#include "Option.h"
     28#include "CommandLine.h"
    2829
    2930#include <stdexcept>
     
    5657       "h,help" for having both short and long option name
    5758       \param desc string used in help display
     59       \param required If true option must be found in commandline or
     60       exception is thrown in validation
    5861    */
    59     OptionArg(CommandLine& cmd, std::string name, std::string desc)
    60       : Option(cmd, name, desc) {}
     62    OptionArg(CommandLine& cmd, std::string name, std::string desc,
     63              bool required=false)
     64      : Option(cmd, name, desc), required_(required) {}
    6165
    6266    /**
     
    6569    T value(void) const { return value_; }
    6670
     71  protected:
     72    inline bool required(void) const { return required_; }
     73
    6774  private:
    68     T def_;
     75    bool required_;
    6976    T value_;
    7077
     
    7380    {
    7481      if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){
    75         std::string str("option requires an argument -- ");
    76         str += short_name();
    77         throw std::runtime_error(str);
     82        std::stringstream ss;
     83        ss << "option requires an argument -- " << short_name() << "\n"
     84           << cmd().try_help();
     85        throw std::runtime_error(ss.str());
    7886      }
    7987      if (first+1==last ) {
    8088        if (first->size()>2){
    8189          std::stringstream ss;
    82           ss << "option `--" << long_name() << "' requires an argument";
     90          ss << "option `--" << long_name() << "' requires an argument\n"
     91             << cmd().try_help();
    8392          throw std::runtime_error(ss.str());
    8493        }
    8594        else {
    86           std::string str("option requires an argument -- ");
    87           str += short_name();
    88           throw std::runtime_error(str);
     95          std::stringstream ss;
     96          ss << "option requires an argument -- " << short_name() << "\n"
     97             << cmd().try_help();
     98          throw std::runtime_error(ss.str());
    8999        }
    90100      }       
     
    117127    /**
    118128     */
    119     virtual void do_validate(void) const {}
     129    void do_validate(void) const
     130    {
     131      if (required_ && !present()) {
     132        std::stringstream ss;
     133        ss << "mandatory opition `";
     134        if (long_name().size())
     135          ss << long_name();
     136        else
     137          ss << short_name();
     138        ss << "' not given\n";
     139        ss << cmd().try_help();
     140        throw std::runtime_error(ss.str());
     141      }
     142      do_validate2();
     143    }
    120144
     145
     146    virtual void do_validate2(void) const {}
    121147  };
    122148
  • trunk/yat/utility/OptionFile.cc

    r979 r980  
    2525#include "FileUtil.h"
    2626
     27#include <stdexcept>
    2728#include <string>
    2829
     
    3233
    3334
    34   OptionFile::OptionFile(CommandLine& cmd, std::string flag,
    35                          std::string desc, bool exist, std::string bits)
    36     : OptionArg<std::string>(cmd, flag, desc), exist_(exist), bits_(bits)
    37   {
    38   }
     35  OptionFile::OptionFile(CommandLine& cmd, std::string flag, std::string desc,
     36                         bool required, bool exist, std::string bits)
     37    : OptionArg<std::string>(cmd, flag, desc, required), exist_(exist),
     38      bits_(bits) {}
    3939
    4040
    41   void OptionFile::do_validate() const
     41  void OptionFile::do_validate2() const
    4242  {   
    4343    if (!present())
  • trunk/yat/utility/OptionFile.h

    r979 r980  
    4747       "f,file" for having both short and long option name
    4848       \param desc string used in help display
     49       \param required If true option must be found in commandline or
     50       exception is thrown in validation
    4951       \param exist if true File must exist
    5052       \param bits used to check permission on file, see
    5153       FileUtil
    5254    */
    53     OptionFile(CommandLine& cmd, std::string name,
    54                std::string desc, bool exist=false, std::string bits="");
     55    OptionFile(CommandLine& cmd, std::string name, std::string desc,
     56               bool required=false, bool exist=false, std::string bits="");
    5557
    5658
    5759  private:
    58     void do_validate() const;
     60    void do_validate2() const;
    5961
    6062    bool exist_;
Note: See TracChangeset for help on using the changeset viewer.