source: trunk/lib/Option.h @ 791

Last change on this file since 791 was 705, checked in by Peter Johansson, 13 years ago

importing classes for commandline parsing from yat. This fixes #349 and #265

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#ifndef _theplu_svndigest_option_
2#define _theplu_svndigest_option_
3
4// $Id: Option.h 705 2008-11-25 23:33:26Z 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 svndigest, http://dev.thep.lu.se/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 3 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  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 svndigest. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include <iostream>
27#include <string>
28#include <vector>
29
30namespace theplu {
31namespace svndigest {
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 after commandline is parsed.
107     */
108    void validate(void);
109
110  protected:
111    /**
112       \return const reference to CommandLine Option belongs to.
113     */
114    const CommandLine& cmd(void) const;
115
116  private:
117    virtual void do_parse(std::vector<std::string>::iterator&, 
118                          const std::vector<std::string>::iterator&)=0;
119
120    /**
121     */
122    virtual std::string print1(void) const;
123
124    /**
125     */
126    virtual std::string print2(void) const;
127
128    /**
129     */
130    virtual std::string print3(void) const;
131
132    /**
133     */
134    virtual std::string print4(void) const;
135
136    /**
137     */
138    virtual void do_validate(void) const=0;
139
140
141    const CommandLine& cmd_;
142    std::string description_;
143    std::string long_name_;
144    bool present_;
145    char short_name_;
146
147    // copy not allowed
148    Option(const Option&);
149    Option& operator=(const Option&);
150  };
151
152}} // of namespace svndigest, and theplu
153
154#endif
Note: See TracBrowser for help on using the repository browser.