source: trunk/yat/Option.h @ 1336

Last change on this file since 1336 was 1336, checked in by Peter Johansson, 12 years ago

latest yat

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1#ifndef _theplu_yat_utility_option_
2#define _theplu_yat_utility_option_
3
4// $Id: Option.h 2384 2010-12-22 14:03:36Z peter $
5
6/*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <string>
27#include <vector>
28
29namespace theplu {
30namespace yat {
31namespace utility {
32
33  class CommandLine;
34  ///
35  /// @brief Container of variables for an option. @see CommandLine
36  ///
37  class Option
38  {
39  public:
40    /**
41       @brief Constructor
42       
43       \param cmd Commandline to be hooked up with.
44       @param name string such as "help" for --help, "h" for -h or
45       "h,help" for having both short and long option name
46       @param desc string used in help display
47    */
48    Option(CommandLine& cmd, std::string name, std::string desc); 
49
50    /**
51       @brief destructor
52    */
53    virtual ~Option(void);
54   
55    /**
56       @return description
57    */
58    std::string description(void) const;
59
60    /**
61       \brief set description
62
63       \since New in yat 0.7
64    */
65    void description(const std::string& description);
66
67    /**
68       \return long name e.g. 'help' for --help option.
69    */
70    std::string long_name(void) const;
71
72    /**
73       \brief parsing the commandline
74     */
75    void parse(std::vector<std::string>::iterator&, 
76               const std::vector<std::string>::iterator&);
77
78    /**
79       @brief Get if option was found in cmd.
80       
81       @return true if option has been detected in parsing
82    */
83    bool present(void) const;
84
85    /**
86       \brief print help output
87
88       This function calls the four virtual private functions print1,
89       print2, print3, and print4. This allows an inherited class to
90       implement one (or several) of these functions and keep the
91       default output of the others. The default behavior is that:
92
93       - print1 prints the short name, '-h', as short_name(void) const
94       - print2 prints the long name, '--help', as long_name(void) const
95       - print3 is empty
96       - print4 prints the description as description(void) const.
97     */
98    std::string print(void);
99
100    /**
101       \brief sets present to false
102    */
103    void reset(void);
104
105    /**
106       \return short name e.g. 'h' for -h option.
107    */
108    char short_name(void) const;
109
110    /**
111       \brief Validate the Option
112
113       This function is called by CommandLine::parse() after all
114       options have been detected and parsed (see Option::parse()).
115     */
116    void validate(void);
117
118  protected:
119    /**
120       \return const reference to CommandLine Option belongs to.
121     */
122    const CommandLine& cmd(void) const;
123
124  private:
125    virtual void do_parse(std::vector<std::string>::iterator&, 
126                          const std::vector<std::string>::iterator&)=0;
127
128    /**
129     */
130    virtual std::string print1(void) const;
131
132    /**
133     */
134    virtual std::string print2(void) const;
135
136    /**
137     */
138    virtual std::string print3(void) const;
139
140    /**
141     */
142    virtual std::string print4(void) const;
143
144    /**
145     */
146    virtual void do_validate(void) const=0;
147
148
149    const CommandLine& cmd_;
150    std::string description_;
151    std::string long_name_;
152    bool present_;
153    char short_name_;
154
155    // copy not allowed
156    Option(const Option&);
157    Option& operator=(const Option&);
158  };
159
160}}} // of namespace utility, yat, and theplu
161
162#endif
Note: See TracBrowser for help on using the repository browser.