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

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

merge patch release 0.4.2 to trunk. Delta 0.4.2-0.4.1

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1#ifndef _theplu_yat_utility_option_arg_
2#define _theplu_yat_utility_option_arg_
3
4// $Id: OptionArg.h 1437 2008-08-25 17:55:00Z peter $
5
6/*
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2008 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
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
14  published by the Free Software Foundation; either version 2 of the
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
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include "Option.h"
29#include "CommandLine.h"
30#include "Exception.h"
31
32#include <string>
33#include <sstream>
34
35namespace theplu {
36namespace yat {
37namespace utility {
38
39  class CommandLine;
40  /**
41     \brief Option with argument
42
43     If the option is present, argument is set during
44     parsing. Supported formats are both gnu-style
45     "--support-gnu=value", POSIX-like "--support-posix value", as
46     well as shorter "-s value". The argument of an parameter is
47     retrived by the value() function
48   */
49  template<typename T>
50  class OptionArg : public Option
51  {
52  public:
53    /**
54       \brief Constructor
55       
56       \param cmd Commandline Option is associated with
57       \param name string such as "help" for --help, "h" for -h or
58       "h,help" for having both short and long option name
59       \param desc string used in help display
60       \param required If true option must be found in commandline or
61       exception is thrown in validation
62    */
63    OptionArg(CommandLine& cmd, std::string name, std::string desc,
64              bool required=false)
65      : Option(cmd, name, desc), required_(required) {}
66
67    /**
68       \return value
69    */
70    T value(void) const { return value_; }
71
72  protected:
73    /**
74       \return true if Option is required, i.e., if Option is not
75       found during parsing an exception will be thrown.
76     */
77    inline bool required(void) const { return required_; }
78
79  private:
80    bool required_;
81    T value_;
82
83    void do_parse(std::vector<std::string>::iterator first, 
84                  std::vector<std::string>::iterator last) 
85    {
86      if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){
87        std::stringstream ss;
88        ss << "option requires an argument -- " << short_name() << "\n"
89           << cmd().try_help();
90        throw cmd_error(ss.str());
91      }
92      if (first+1==last ) {
93        if (first->size()>2){
94          std::stringstream ss;
95          ss << "option `--" << long_name() << "' requires an argument\n"
96             << cmd().try_help();
97          throw cmd_error(ss.str());
98        }
99        else {
100          std::stringstream ss;
101          ss << "option requires an argument -- " << short_name() << "\n"
102             << cmd().try_help();
103          throw cmd_error(ss.str());
104        }
105      }       
106       
107      if ( *(first+1)->begin() == '"' && *((first+1)->end()-1) == '"')
108        *(first+1) = (first+1)->substr(1, (first+1)->size()-2); 
109      assign(value_, *(++first));
110    }
111
112    void assign(std::string& lhs, std::string rhs )
113    { 
114      lhs = rhs;
115    }
116   
117    template<class T1>
118    void assign(T1& lhs, std::string rhs )
119    { 
120      std::stringstream ss(rhs);
121      ss >> lhs;
122      bool fail = ss.fail();
123      std::string str;
124      ss >> str;
125      if (fail || str.size()) {
126        std::stringstream sstr(rhs);
127        sstr << ": invalid argument";
128        throw cmd_error(sstr.str());
129      }
130    }
131
132    /**
133     */
134    void do_validate(void) const 
135    {
136      if (required_ && !present()) {
137        std::stringstream ss;
138        ss << "mandatory option `";
139        if (long_name().size())
140          ss << long_name();
141        else
142          ss << short_name();
143        ss << "' not given\n";
144        ss << cmd().try_help();
145        throw cmd_error(ss.str());
146      }
147      do_validate2();
148    }
149
150
151    virtual void do_validate2(void) const {}
152  };
153
154}}} // of namespace utility, yat, and theplu
155
156#endif
Note: See TracBrowser for help on using the repository browser.