source: trunk/yat/utility/CommandLine.h @ 718

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

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1#ifndef _theplu_yat_utility_commandline_
2#define _theplu_yat_utility_commandline_
3
4//$Id: CommandLine.h 718 2006-12-26 09:56:26Z jari $
5
6/*
7  Copyright (C) 2006 Peter Johansson, Jari Hakkinen
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 <cctype>
30#include <list>
31#include <map>
32#include <string>
33#include <utility>
34#include <vector>
35
36namespace theplu {
37namespace yat {
38namespace utility {
39
40  /**
41     @brief Class for parsing the command line.
42     
43     Provides parsing and storage of command line arguments (argc,
44     argv). To use this class first add a set of valid parameters
45     using the add_parameter() function. Each parameter is associated
46     to a one-character flag and/or a longer string flag. The longer
47     flag expects to be preceded by '--' as e.g. '--help' for
48     help. The shorter flag expects to be preceded by '-' as
49     e.g. '-h', and can also be concatenated as e.g. "program -vf" is
50     equivalent to "program -v -f". Associated to each parameter is an
51     attribute telling what type of argument is expected for the
52     parameter. Several types of arguments are supported: no argument,
53     string argument, int argument, double argument. The argument
54     value for all found parameters are set during parsing, which
55     supports both gnu-style "--support-gnu=value", POSIX-like
56     "--support-posix value", as well as shorter "-s value". The
57     argument of an parameter is retrived by the value() function or
58     its sibblings for different types. By using the set_help()
59     function help will be displayed when flag (default is '-h' and
60     '--help') is found in parsing.
61     
62     Here is a small code example:
63     @code
64#include "yat/utility/CommandLine.h"
65#include <iostream>
66int main(const int argc,const char* argv[])
67{
68  theplu::utility::CommandLine cmd;
69  cmd.set_general_description("This is just an example program");
70  cmd.add_parameter('n', utility::Option::int_arg,
71                    "example of parameter taking an integer");
72  cmd.add_parameter('t', "target", utility::Option::string_arg);
73  cmd.add_parameter("version",utility::Option::no_arg,
74                    "output version infomation and exit");
75  cmd.set_help();
76  cmd.parse(argc, argv);
77  if (cmd.present("version"))
78    std::cout << "example 1.0" << std::endl;
79  if (cmd.present("target"))
80    std::cout << "using target: " << target << std::endl;
81  if (cmd.present('n')){
82    int n=cmd.value('n');
83    for (size_t i=0; i<n; ++i)
84      std::cout << "Hello World\n";
85    std::cout << endl;
86  }
87  return 0;
88}
89     @endcode
90     
91     @see Option
92     
93  **/
94  class CommandLine
95  {
96  public:
97
98    ///
99    /// @brief deafult constructor
100    ///
101    CommandLine(void);
102
103    ///
104    /// @brief Destructor
105    ///
106    virtual ~CommandLine(void);
107
108    ///
109    /// @brief Function to add a parameter.
110    ///
111    /// @param long_name string key such as "help" for --help flag
112    /// @param arg telling what kind argument this option expects
113    /// @param description string used in help display
114    ///
115    void add_parameter(const std::string& long_name,
116                       Option::argument_type arg = Option::no_arg,
117                       const std::string& description = std::string());
118
119    ///
120    /// @brief Function to add a parameter.
121    ///
122    /// @param short_name one character key such as 'h' for -h flag
123    /// @param arg telling what kind argument this option expects
124    /// @param description string used in help display
125    ///
126    void add_parameter(const char short_name,
127                       Option::argument_type arg = Option::no_arg,
128                       const std::string& description = std::string());
129
130    ///
131    /// @brief Function to add a parameter.
132    ///
133    /// @param short_name one character key such as 'h' for -h flag
134    /// @param long_name string key such as "help" for --help flag
135    /// @param arg telling what kind argument this option expects
136    /// @param description string used in help display
137    ///
138    void add_parameter(char short_name, const std::string& long_name,
139                       Option::argument_type arg = Option::no_arg,
140                       const std::string& description = std::string());
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    /// @brief If more than maximal number of arguments is found
149    /// during parsing an error message is displayed followed by exit.
150    ///
151    /// @return maximal number of arguments allowed.
152    ///
153    u_int& max_argument(void);
154
155    ///
156    /// @brief If less than minimal number of arguments is found
157    /// during parsing an error message is displayed followed by exit.
158    ///
159    /// @return minimal number of arguments allowed.
160    ///
161    u_int& min_argument(void);
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    /// @brief The @a description will be included in help display
182    /// giving a general explanation what program is doing.
183    ///
184    void set_general_description(const std::string& description);
185
186    ///
187    /// @note Using function for @a parameter not added previously
188    /// will cause an error message and exit.
189    ///
190    /// @return argument value for @a parameter
191    ///
192    std::string value(const std::string& parameter) const;
193
194    ///
195    /// If the value for @a parameter is not a valid double an error
196    /// message will be displayed followed by an exit.
197    ///
198    /// @note Using function for @a parameter not added previously
199    /// will cause an error message and exit.
200    ///
201    /// @return argument value for @a parameter
202    ///
203    double value_double(const std::string& parameter) const;
204
205    ///
206    /// If the value for @a parameter is not a valid double an error
207    /// message will be displayed followed by an exit.
208    ///
209    /// @note Using function for @a parameter not added previously
210    /// will cause an error message and exit.
211    ///
212    /// @return argument value for @a parameter
213    ///
214    int value_int(const std::string& parameter) const;
215
216    ///
217    /// Function to display the help message.
218    ///
219    void usage(void) const;
220
221  private:
222    Option* add(char short_name,
223                const std::string& long_name,
224                Option::argument_type arg,
225                const std::string& describtion);
226    bool is_long_option(const std::string& str);
227    bool is_short_option(const std::string& str);
228    void print_try_help(void) const;
229    std::string split(std::string&, char) const;
230    bool update(const std::string& key, const std::string& value);
231
232    typedef std::map<std::string, Option*> key2option;
233
234    std::string app_name_;
235    const Option* help_option_;
236    std::string general_description_;
237    u_int max_argument_;
238    u_int min_argument_;
239    key2option param_;
240    std::list<Option*> options_;
241    std::vector<std::string> arguments_;
242  };
243
244
245}}} // end of namespace utility, yat, and theplu
246
247#endif
Note: See TracBrowser for help on using the repository browser.