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

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

Adding CommandLine? class. finishes #240

  • 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 965 2007-10-11 16:58:45Z 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
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  class CommandLine;
37  /**
38     \brief Option with argument
39
40     If the option is present, argument is set during
41     parsing. Supported formats are both gnu-style
42     "--support-gnu=value", POSIX-like "--support-posix value", as
43     well as shorter "-s value". The argument of an parameter is
44     retrived by the value() function
45   */
46  template<typename T>
47  class OptionArg : public Option
48  {
49  public:
50    /**
51       \brief Constructor
52       
53       \param cmd Commandline Option is associated with
54       \param name string such as "help" for --help, "h" for -h or
55       "h,help" for having both short and long option name
56       \param desc string used in help display
57    */
58    OptionArg(CommandLine& cmd, std::string name, std::string desc)
59      : Option(cmd, name, desc) {}
60
61    /**
62       \return value
63    */
64    T value(void) { return value_; }
65
66  private:
67    T def_;
68    T value_;
69
70    void do_parse(std::vector<std::string>::iterator first, 
71                  std::vector<std::string>::iterator last) 
72    {
73      if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){
74        std::string str("option requires an argument -- ");
75        str += short_name();
76        throw std::runtime_error(str);
77      }
78      if (first+1==last ) {
79        if (first->size()>2){
80          std::stringstream ss;
81          ss << "option `--" << long_name() << "' requires an argument";
82          throw std::runtime_error(ss.str());
83        }
84        else {
85          std::string str("option requires an argument -- ");
86          str += short_name();
87          throw std::runtime_error(str);
88        }
89      }       
90       
91      if ( *(first+1)->begin() == '"' && *((first+1)->end()-1) == '"')
92        *(first+1) = (first+1)->substr(1, (first+1)->size()-2); 
93      assign(value_, *(++first));
94    }
95
96    void assign(std::string& lhs, std::string rhs )
97    { 
98      lhs = rhs;
99    }
100   
101    template<class T1>
102    void assign(T1& lhs, std::string rhs )
103    { 
104      std::stringstream ss(rhs);
105      ss >> lhs;
106      bool fail = ss.fail();
107      std::string str;
108      ss >> str;
109      if (fail || str.size()) {
110        std::stringstream sstr(rhs);
111        sstr << ": invalid argument";
112        throw std::runtime_error(sstr.str());
113      }
114    }
115
116    /**
117     */
118    void do_validate(void) const {}
119
120  };
121
122}}} // of namespace utility, yat, and theplu
123
124#endif
Note: See TracBrowser for help on using the repository browser.