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

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

fixing Doxygen parsing

  • 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 1125 2008-02-22 21:31:22Z peter $
5
6/*
7  Copyright (C) 2007 Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/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    /**
73       \return true if Option is required, i.e., if Option is not
74       found during parsing an exception will be thrown.
75     */
76    inline bool required(void) const { return required_; }
77
78  private:
79    bool required_;
80    T value_;
81
82    void do_parse(std::vector<std::string>::iterator first, 
83                  std::vector<std::string>::iterator last) 
84    {
85      if ( first->size()>2 && (*first)[0]=='-' && (*first)[1]!='-'){
86        std::stringstream ss;
87        ss << "option requires an argument -- " << short_name() << "\n"
88           << cmd().try_help();
89        throw std::runtime_error(ss.str());
90      }
91      if (first+1==last ) {
92        if (first->size()>2){
93          std::stringstream ss;
94          ss << "option `--" << long_name() << "' requires an argument\n"
95             << cmd().try_help();
96          throw std::runtime_error(ss.str());
97        }
98        else {
99          std::stringstream ss;
100          ss << "option requires an argument -- " << short_name() << "\n"
101             << cmd().try_help();
102          throw std::runtime_error(ss.str());
103        }
104      }       
105       
106      if ( *(first+1)->begin() == '"' && *((first+1)->end()-1) == '"')
107        *(first+1) = (first+1)->substr(1, (first+1)->size()-2); 
108      assign(value_, *(++first));
109    }
110
111    void assign(std::string& lhs, std::string rhs )
112    { 
113      lhs = rhs;
114    }
115   
116    template<class T1>
117    void assign(T1& lhs, std::string rhs )
118    { 
119      std::stringstream ss(rhs);
120      ss >> lhs;
121      bool fail = ss.fail();
122      std::string str;
123      ss >> str;
124      if (fail || str.size()) {
125        std::stringstream sstr(rhs);
126        sstr << ": invalid argument";
127        throw std::runtime_error(sstr.str());
128      }
129    }
130
131    /**
132     */
133    void do_validate(void) const 
134    {
135      if (required_ && !present()) {
136        std::stringstream ss;
137        ss << "mandatory option `";
138        if (long_name().size())
139          ss << long_name();
140        else
141          ss << short_name();
142        ss << "' not given\n";
143        ss << cmd().try_help();
144        throw std::runtime_error(ss.str());
145      }
146      do_validate2();
147    }
148
149
150    virtual void do_validate2(void) const {}
151  };
152
153}}} // of namespace utility, yat, and theplu
154
155#endif
Note: See TracBrowser for help on using the repository browser.