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

Last change on this file since 1465 was 1465, checked in by Peter, 13 years ago

fixes #418

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