source: trunk/c++_tools/utility/CommandLine.h @ 675

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

References #83. Changing project name to yat. Compilation will fail in this revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1#ifndef _theplu_utility_commandline_
2#define _theplu_utility_commandline_
3
4//$Id: CommandLine.h 675 2006-10-10 12:08:45Z jari $
5
6/*
7  Copyright (C) 2006 Peter Johansson
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "Option.h"
28
29#include <list>
30#include <map>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace theplu {
36namespace utility {
37
38  /**
39     @brief Class for parsing the command line.
40     
41     Provides parsing and storage of command line arguments (argc,
42     argv). To use this class first add a set of valid parameters
43     using the add_parameter() function. Each parameter is associated
44     to a one-character flag and/or a longer string flag. The longer
45     flag expects to be preceded by '--' as e.g. '--help' for
46     help. The shorter flag expects to be preceded by '-' as
47     e.g. '-h', and can also be concatenated as e.g. "program -vf" is
48     equivalent to "program -v -f". Associated to each parameter is an
49     attribute telling what type of argument is expected for the
50     parameter. Several types of arguments are supported: no argument,
51     string argument, int argument, double argument. The argument
52     value for all found parameters are set during parsing, which
53     supports both gnu-style "--support-gnu=value", POSIX-like
54     "--support-posix value", as well as shorter "-s value". The
55     argument of an parameter is retrived by the value() function or
56     its sibblings for different types. By using the set_help()
57     function help will be displayed when flag (default is '-h' and
58     '--help') is found in parsing.
59     
60     Here is a small code example:
61     @code
62#include "yat/utility/CommandLine.h"
63#include <iostream>
64int main(const int argc,const char* argv[])
65{
66  theplu::utility::CommandLine cmd;
67  cmd.set_general_description("This is just an example program");
68  cmd.add_parameter('n', utility::Option::int_arg,
69                    "example of parameter taking an integer");
70  cmd.add_parameter('t', "target", utility::Option::string_arg);
71  cmd.add_parameter("version",utility::Option::no_arg,
72                    "output version infomation and exit");
73  cmd.set_help();
74  cmd.parse(argc, argv);
75  if (cmd.present("version"))
76    std::cout << "example 1.0" << std::endl;
77  if (cmd.present("target"))
78    std::cout << "using target: " << target << std::endl;
79  if (cmd.present('n')){
80    int n=cmd.value('n');
81    for (size_t i=0; i<n; ++i)
82      std::cout << "Hello World\n";
83    std::cout << endl;
84  }
85  return 0;
86}
87     @endcode
88     
89     @see Option
90     
91  **/
92  class CommandLine
93  {
94  public:
95
96    ///
97    /// @brief deafult constructor
98    ///
99    CommandLine(void);
100
101    ///
102    /// @brief Destructor
103    ///
104    virtual ~CommandLine(void);
105
106    ///
107    /// @brief Function to add a parameter.
108    ///
109    /// @param long_name string key such as "help" for --help flag
110    /// @param arg telling what kind argument this option expects
111    /// @param description string used in help display
112    ///
113    void add_parameter(const std::string& long_name,
114                       Option::argument_type arg = Option::no_arg,
115                       const std::string& description = std::string());
116
117    ///
118    /// @brief Function to add a parameter.
119    ///
120    /// @param short_name one character key such as 'h' for -h flag
121    /// @param arg telling what kind argument this option expects
122    /// @param description string used in help display
123    ///
124    void add_parameter(const char short_name,
125                       Option::argument_type arg = Option::no_arg,
126                       const std::string& description = std::string());
127
128    ///
129    /// @brief Function to add a parameter.
130    ///
131    /// @param short_name one character key such as 'h' for -h flag
132    /// @param long_name string key such as "help" for --help flag
133    /// @param arg telling what kind argument this option expects
134    /// @param description string used in help display
135    ///
136    inline void add_parameter(const char short_name,
137                              const std::string& long_name,
138                              Option::argument_type arg = Option::no_arg,
139                              const std::string& description = std::string())
140    { add(short_name, long_name, arg, description); }
141
142    ///
143    /// @return vector of arguments not associated to a specific parameter
144    ///
145    const std::vector<std::string>& arguments(void) const;
146
147    ///
148    /// If more than maximal number of arguments is found during
149    /// parsing an error message is displayed followed by exit.
150    ///
151    /// @return maximal number of arguments allowed.
152    ///
153    inline u_int& max_argument(void) { return max_argument_; }
154
155    ///
156    /// If less than minimal number of arguments is found during
157    /// parsing an error message is displayed followed by exit.
158    ///
159    /// @return minimal number of arguments allowed.
160    ///
161    inline u_int& min_argument(void) { return min_argument_; }
162
163    ///
164    /// @brief parse the commandline
165    ///
166    void parse(int argc, const char* argv[]);
167
168    ///
169    /// @return true if @a parameter has been detected in parsing.
170    ///
171    bool present(const std::string& parameter) const;
172
173    ///
174    /// @brief allow help.
175    ///
176    void set_help(char shortname = 'h',
177                  const std::string& longname = "help",
178                  const std::string& descr = "display this help and exit");
179
180    ///
181    /// The @a description will be included in help display giving a
182    /// general explanation what program is doing.
183    ///
184    inline void set_general_description(const std::string& description)
185    { general_description_=description; }
186
187    ///
188    /// @note Using function for @a parameter not added previously
189    /// will cause an error message and exit.
190    ///
191    /// @return argument value for @a parameter
192    ///
193    std::string value(const std::string& parameter) const;
194
195    ///
196    /// If the value for @a parameter is not a valid double an error
197    /// message will be displayed followed by an exit.
198    ///
199    /// @note Using function for @a parameter not added previously
200    /// will cause an error message and exit.
201    ///
202    /// @return argument value for @a parameter
203    ///
204    double value_double(const std::string& parameter) const;
205
206    ///
207    /// If the value for @a parameter is not a valid double an error
208    /// message will be displayed followed by an exit.
209    ///
210    /// @note Using function for @a parameter not added previously
211    /// will cause an error message and exit.
212    ///
213    /// @return argument value for @a parameter
214    ///
215    int value_int(const std::string& parameter) const;
216
217    ///
218    /// Function to display the help message.
219    ///
220    void usage(void) const;
221
222    private:
223    Option* add(char short_name,
224                const std::string& long_name,
225                Option::argument_type arg,
226                const std::string& describtion);
227
228    inline bool is_long_option(const std::string& str)
229    { return (str.size()>3 && str[0]=='-' && str[1]=='-'); }
230   
231    inline bool is_short_option(const std::string& str)
232    { return (str.size()==2 && str[0]=='-' && isalpha(str[1])); }
233
234    void print_try_help(void) const;
235    std::string split(std::string&, char) const;
236    bool update(const std::string& key, const std::string& value);
237
238
239    typedef std::map<std::string, Option*> key2option;
240
241    std::string app_name_;
242    const Option* help_option_;
243    std::string general_description_;
244    u_int max_argument_;
245    u_int min_argument_;
246    key2option param_;
247    std::list<Option*> options_;
248    std::vector<std::string> arguments_;
249   
250  };
251
252
253}} // end of namespace utility and namespace theplu
254
255#endif
256
257
258
259
Note: See TracBrowser for help on using the repository browser.