source: trunk/yat/utility/Option.h @ 1679

Last change on this file since 1679 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#ifndef _theplu_yat_utility_option_
2#define _theplu_yat_utility_option_
3
4// $Id: Option.h 1487 2008-09-10 08:41:36Z jari $
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 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 <iostream>
27#include <string>
28#include <vector>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  class CommandLine;
35  ///
36  /// @brief Container of variables for an option. @see CommandLine
37  ///
38  class Option
39  {
40  public:
41    /**
42       @brief Constructor
43       
44       \param cmd Commandline to be hooked up with.
45       @param name string such as "help" for --help, "h" for -h or
46       "h,help" for having both short and long option name
47       @param desc string used in help display
48    */
49    Option(CommandLine& cmd, std::string name, std::string desc); 
50
51    /**
52       @brief destructor
53    */
54    virtual ~Option(void);
55   
56    /**
57       @return description
58    */
59    std::string description(void) const;
60
61    /**
62       \return long name e.g. 'help' for --help option.
63    */
64    std::string long_name(void) const;
65
66    /**
67       \brief parsing the commandline
68     */
69    void parse(std::vector<std::string>::iterator&, 
70               const std::vector<std::string>::iterator&);
71
72    /**
73       @brief Get if option was found in cmd.
74       
75       @return true if option has been detected in parsing
76    */
77    bool present(void) const;
78
79    /**
80       \brief print help output
81
82       This function calls the four virtual private functions print1,
83       print2, print3, and print4. This allows an inherited class to
84       implement one (or several) of these functions and keep the
85       default output of the others. The default behavior is that:
86
87       - print1 prints the short name, '-h', as short_name(void) const
88       - print2 prints the long name, '--help', as long_name(void) const
89       - print3 is empty
90       - print4 prints the description as description(void) const.
91     */
92    std::string print(void);
93
94    /**
95       \brief sets present to false
96    */
97    void reset(void);
98
99    /**
100       \return short name e.g. 'h' for -h option.
101    */
102    char short_name(void) const;
103
104    /**
105       \brief Validate the Option
106
107       This function is called after commandline is parsed.
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.