source: trunk/yat/utility/OptionArg.h @ 1630

Last change on this file since 1630 was 1630, checked in by Peter, 13 years ago

re-using convert function and correcting docs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.3 KB
RevLine 
[965]1#ifndef _theplu_yat_utility_option_arg_
2#define _theplu_yat_utility_option_arg_
3
4// $Id: OptionArg.h 1630 2008-11-17 22:11:32Z peter $
5
6/*
[1275]7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2008 Peter Johansson
[965]9
[1437]10  This file is part of the yat library, http://dev.thep.lu.se/yat
[965]11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
[1486]14  published by the Free Software Foundation; either version 3 of the
[965]15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
[1487]23  along with yat. If not, see <http://www.gnu.org/licenses/>.
[965]24*/
25
26#include "Option.h"
[980]27#include "CommandLine.h"
[1178]28#include "Exception.h"
[1630]29#include "utility.h"
[965]30
31#include <string>
[975]32#include <sstream>
[965]33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38  class CommandLine;
39  /**
40     \brief Option with argument
41
42     If the option is present, argument is set during
43     parsing. Supported formats are both gnu-style
44     "--support-gnu=value", POSIX-like "--support-posix value", as
45     well as shorter "-s value". The argument of an parameter is
46     retrived by the value() function
47   */
48  template<typename T>
49  class OptionArg : public Option
50  {
51  public:
52    /**
53       \brief Constructor
54       
[1630]55       \param cmd Commandline Option is associated with
56       \param name string such as "help" for --help, "h" for -h or
57       "h,help" for having both short and long option name
58       \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
61       \param arg string to be used in help output such as `TARGET' in
62       `--target=TARGET'. See print3(). New in yat 0.5.
[965]63    */
[980]64    OptionArg(CommandLine& cmd, std::string name, std::string desc,
[1629]65              bool required=false, std::string arg="")
66      : Option(cmd, name, desc), print_arg_(arg), required_(required) {}
[965]67
68    /**
69       \return value
70    */
[975]71    T value(void) const { return value_; }
[965]72
[1601]73    /**
74       \brief set value
75
76       \since new in yat 0.5
77    */
[1602]78    void value(T v) { value_ = v; }
[1601]79
[980]80  protected:
[1125]81    /**
82       \return true if Option is required, i.e., if Option is not
83       found during parsing an exception will be thrown.
84     */
[980]85    inline bool required(void) const { return required_; }
86
[965]87  private:
[1629]88    std::string print_arg_;
[980]89    bool required_;
[965]90    T value_;
91
[1465]92    void do_parse(std::vector<std::string>::iterator& first, 
93                  const std::vector<std::string>::iterator& last) 
[965]94    {
95      if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){
[980]96        std::stringstream ss;
97        ss << "option requires an argument -- " << short_name() << "\n"
98           << cmd().try_help();
[1178]99        throw cmd_error(ss.str());
[965]100      }
101      if (first+1==last ) {
102        if (first->size()>2){
103          std::stringstream ss;
[980]104          ss << "option `--" << long_name() << "' requires an argument\n"
105             << cmd().try_help();
[1178]106          throw cmd_error(ss.str());
[965]107        }
108        else {
[980]109          std::stringstream ss;
110          ss << "option requires an argument -- " << short_name() << "\n"
111             << cmd().try_help();
[1178]112          throw cmd_error(ss.str());
[965]113        }
114      }       
115       
116      if ( *(first+1)->begin() == '"' && *((first+1)->end()-1) == '"')
117        *(first+1) = (first+1)->substr(1, (first+1)->size()-2); 
118      assign(value_, *(++first));
119    }
120
121    void assign(std::string& lhs, std::string rhs )
122    { 
123      lhs = rhs;
124    }
125   
126    template<class T1>
127    void assign(T1& lhs, std::string rhs )
128    { 
[1630]129      try {
130        lhs = convert<T1>(rhs);
131      }
132      catch (std::runtime_error& e) {
[965]133        std::stringstream sstr(rhs);
134        sstr << ": invalid argument";
[1178]135        throw cmd_error(sstr.str());
[965]136      }
137    }
138
139    /**
140     */
[980]141    void do_validate(void) const 
142    {
143      if (required_ && !present()) {
144        std::stringstream ss;
[989]145        ss << "mandatory option `";
[980]146        if (long_name().size())
147          ss << long_name();
148        else
149          ss << short_name();
150        ss << "' not given\n";
151        ss << cmd().try_help();
[1178]152        throw cmd_error(ss.str());
[980]153      }
154      do_validate2();
155    }
[965]156
[980]157
158    virtual void do_validate2(void) const {}
[1629]159
160    virtual std::string print3(void) const 
161    { 
162      if (print_arg_.empty())
163        return std::string("");
164      return std::string("=")+print_arg_; 
165    }
166
[965]167  };
168
169}}} // of namespace utility, yat, and theplu
170
171#endif
Note: See TracBrowser for help on using the repository browser.