source: trunk/lib/yat/Option.h @ 983

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

fetch yat files. refs #429

  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
1#ifndef _theplu_yat_utility_option_
2#define _theplu_yat_utility_option_
3
4// $Id: Option.h 2119 2009-12-12 23:11:43Z peter $
5
6/*
7  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009 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       \return long name e.g. 'help' for --help option.
62    */
63    std::string long_name(void) const;
64
65    /**
66       \brief parsing the commandline
67     */
68    void parse(std::vector<std::string>::iterator&, 
69               const std::vector<std::string>::iterator&);
70
71    /**
72       @brief Get if option was found in cmd.
73       
74       @return true if option has been detected in parsing
75    */
76    bool present(void) const;
77
78    /**
79       \brief print help output
80
81       This function calls the four virtual private functions print1,
82       print2, print3, and print4. This allows an inherited class to
83       implement one (or several) of these functions and keep the
84       default output of the others. The default behavior is that:
85
86       - print1 prints the short name, '-h', as short_name(void) const
87       - print2 prints the long name, '--help', as long_name(void) const
88       - print3 is empty
89       - print4 prints the description as description(void) const.
90     */
91    std::string print(void);
92
93    /**
94       \brief sets present to false
95    */
96    void reset(void);
97
98    /**
99       \return short name e.g. 'h' for -h option.
100    */
101    char short_name(void) const;
102
103    /**
104       \brief Validate the Option
105
106       This function is called by CommandLine::parse() after all
107       options have been detected and parsed (see Option::parse()).
108     */
109    void validate(void);
110
111  protected:
112    /**
113       \return const reference to CommandLine Option belongs to.
114     */
115    const CommandLine& cmd(void) const;
116
117  private:
118    virtual void do_parse(std::vector<std::string>::iterator&, 
119                          const std::vector<std::string>::iterator&)=0;
120
121    /**
122     */
123    virtual std::string print1(void) const;
124
125    /**
126     */
127    virtual std::string print2(void) const;
128
129    /**
130     */
131    virtual std::string print3(void) const;
132
133    /**
134     */
135    virtual std::string print4(void) const;
136
137    /**
138     */
139    virtual void do_validate(void) const=0;
140
141
142    const CommandLine& cmd_;
143    std::string description_;
144    std::string long_name_;
145    bool present_;
146    char short_name_;
147
148    // copy not allowed
149    Option(const Option&);
150    Option& operator=(const Option&);
151  };
152
153}}} // of namespace utility, yat, and theplu
154
155#endif
Note: See TracBrowser for help on using the repository browser.