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

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

fixed typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1#ifndef _theplu_yat_utility_option_arg_
2#define _theplu_yat_utility_option_arg_
3
4// $Id: OptionArg.h 989 2007-11-14 15:35:02Z 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#include "CommandLine.h"
29
30#include <stdexcept>
31#include <string>
32#include <sstream>
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       
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    */
62    OptionArg(CommandLine& cmd, std::string name, std::string desc,
63              bool required=false)
64      : Option(cmd, name, desc), required_(required) {}
65
66    /**
67       \return value
68    */
69    T value(void) const { return value_; }
70
71  protected:
72    inline bool required(void) const { return required_; }
73
74  private:
75    bool required_;
76    T value_;
77
78    void do_parse(std::vector<std::string>::iterator first, 
79                  std::vector<std::string>::iterator last) 
80    {
81      if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){
82        std::stringstream ss;
83        ss << "option requires an argument -- " << short_name() << "\n"
84           << cmd().try_help();
85        throw std::runtime_error(ss.str());
86      }
87      if (first+1==last ) {
88        if (first->size()>2){
89          std::stringstream ss;
90          ss << "option `--" << long_name() << "' requires an argument\n"
91             << cmd().try_help();
92          throw std::runtime_error(ss.str());
93        }
94        else {
95          std::stringstream ss;
96          ss << "option requires an argument -- " << short_name() << "\n"
97             << cmd().try_help();
98          throw std::runtime_error(ss.str());
99        }
100      }       
101       
102      if ( *(first+1)->begin() == '"' && *((first+1)->end()-1) == '"')
103        *(first+1) = (first+1)->substr(1, (first+1)->size()-2); 
104      assign(value_, *(++first));
105    }
106
107    void assign(std::string& lhs, std::string rhs )
108    { 
109      lhs = rhs;
110    }
111   
112    template<class T1>
113    void assign(T1& lhs, std::string rhs )
114    { 
115      std::stringstream ss(rhs);
116      ss >> lhs;
117      bool fail = ss.fail();
118      std::string str;
119      ss >> str;
120      if (fail || str.size()) {
121        std::stringstream sstr(rhs);
122        sstr << ": invalid argument";
123        throw std::runtime_error(sstr.str());
124      }
125    }
126
127    /**
128     */
129    void do_validate(void) const 
130    {
131      if (required_ && !present()) {
132        std::stringstream ss;
133        ss << "mandatory option `";
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    }
144
145
146    virtual void do_validate2(void) const {}
147  };
148
149}}} // of namespace utility, yat, and theplu
150
151#endif
Note: See TracBrowser for help on using the repository browser.