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

Last change on this file since 975 was 975, checked in by Peter, 14 years ago

adding Option class for argument associated with a file, and fixed minor issues

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1#ifndef _theplu_yat_utility_option_arg_
2#define _theplu_yat_utility_option_arg_
3
4// $Id: OptionArg.h 975 2007-10-17 21:03:37Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "Option.h"
28
29#include <stdexcept>
30#include <string>
31#include <sstream>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  class CommandLine;
38  /**
39     \brief Option with argument
40
41     If the option is present, argument is set during
42     parsing. Supported formats are both gnu-style
43     "--support-gnu=value", POSIX-like "--support-posix value", as
44     well as shorter "-s value". The argument of an parameter is
45     retrived by the value() function
46   */
47  template<typename T>
48  class OptionArg : public Option
49  {
50  public:
51    /**
52       \brief Constructor
53       
54       \param cmd Commandline Option is associated with
55       \param name string such as "help" for --help, "h" for -h or
56       "h,help" for having both short and long option name
57       \param desc string used in help display
58    */
59    OptionArg(CommandLine& cmd, std::string name, std::string desc)
60      : Option(cmd, name, desc) {}
61
62    /**
63       \return value
64    */
65    T value(void) const { return value_; }
66
67  private:
68    T def_;
69    T value_;
70
71    void do_parse(std::vector<std::string>::iterator first, 
72                  std::vector<std::string>::iterator last) 
73    {
74      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);
78      }
79      if (first+1==last ) {
80        if (first->size()>2){
81          std::stringstream ss;
82          ss << "option `--" << long_name() << "' requires an argument";
83          throw std::runtime_error(ss.str());
84        }
85        else {
86          std::string str("option requires an argument -- ");
87          str += short_name();
88          throw std::runtime_error(str);
89        }
90      }       
91       
92      if ( *(first+1)->begin() == '"' && *((first+1)->end()-1) == '"')
93        *(first+1) = (first+1)->substr(1, (first+1)->size()-2); 
94      assign(value_, *(++first));
95    }
96
97    void assign(std::string& lhs, std::string rhs )
98    { 
99      lhs = rhs;
100    }
101   
102    template<class T1>
103    void assign(T1& lhs, std::string rhs )
104    { 
105      std::stringstream ss(rhs);
106      ss >> lhs;
107      bool fail = ss.fail();
108      std::string str;
109      ss >> str;
110      if (fail || str.size()) {
111        std::stringstream sstr(rhs);
112        sstr << ": invalid argument";
113        throw std::runtime_error(sstr.str());
114      }
115    }
116
117    /**
118     */
119    virtual void do_validate(void) const {}
120
121  };
122
123}}} // of namespace utility, yat, and theplu
124
125#endif
Note: See TracBrowser for help on using the repository browser.