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

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

Updating copyright statements.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1#ifndef _theplu_yat_utility_option_
2#define _theplu_yat_utility_option_
3
4// $Id: Option.h 1275 2008-04-11 06:10:12Z 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://trac.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               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    std::string print(void);
85
86    /**
87       \brief sets present to false
88    */
89    void reset(void);
90
91    /**
92       \return short name e.g. 'h' for -h option.
93    */
94    char short_name(void) const;
95
96    /**
97       \brief Validate the Option
98
99       This function is called after commandline is parsed.
100     */
101    void validate(void);
102
103  protected:
104    /**
105       \return const reference to CommandLine Option belongs to.
106     */
107    const CommandLine& cmd(void) const;
108
109  private:
110    virtual void do_parse(std::vector<std::string>::iterator, 
111                          std::vector<std::string>::iterator)=0;
112
113    /**
114     */
115    virtual std::string print1(void) const;
116
117    /**
118     */
119    virtual std::string print2(void) const;
120
121    /**
122     */
123    virtual std::string print3(void) const;
124
125    /**
126     */
127    virtual std::string print4(void) const;
128
129    /**
130     */
131    virtual void do_validate(void) const=0;
132
133
134    const CommandLine& cmd_;
135    std::string description_;
136    std::string long_name_;
137    bool present_;
138    char short_name_;
139
140    // copy not allowed
141    Option(const Option&);
142    Option& operator=(const Option&);
143  };
144
145}}} // of namespace utility, yat, and theplu
146
147#endif
Note: See TracBrowser for help on using the repository browser.