source: trunk/lib/CommandLine.h @ 745

Last change on this file since 745 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: 5.0 KB
Line 
1#ifndef _theplu_svndigest_commandline_
2#define _theplu_svndigest_commandline_
3
4//$Id: CommandLine.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 <cctype>
27#include <map>
28#include <iostream>
29#include <sstream>
30#include <stdexcept>
31#include <string>
32#include <typeinfo>
33#include <utility>
34#include <vector>
35
36namespace theplu {
37namespace svndigest {
38
39  class Option;
40
41  /**
42     @brief Class for parsing the command line.
43     
44     Provides parsing and storage of command line arguments. The class
45     is typically used by hooking a number of Option objects to
46     CommandLine, and then call the parse() function. Here is a short
47     example how the class may be used:
48 
49     \code
50 
51     CommandLine cmd;
52     OptionArg<std::string> dir(cmd, "d,dir", "output directory");
53     OptionHelp help(cmd);
54     OptionSwitch target(cmd, "T,target", "treat DEST as a normal file", true);
55     OptionSwitch verbose(cmd, "v,verbose", "explain what is being done");
56     OptionSwitch version(cmd, "version", "output version and exit");
57     try {
58       cmd.parse(argc, argv);
59     }
60     catch (cmd_error& e){
61       if (version.present()){
62         std::cout << cmd.program_name() << " 1.0\n"
63                   << "Copyright (C) 2007 Peter Johansson\n\n"
64                   << "This is free software see the source for copying "
65                   << "conditions. There is NO\nwarranty; not even for "
66                   << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
67         exit(0);
68       }
69       std::cout << e.what() << std::endl;
70       return 1;
71     } 
72     ...
73     \endcode
74
75     After creation a number of Option classes are hooked up to the
76     CommandLine object in their constructors.
77
78     Each parameter is associated to a one-character flag and/or a
79     longer string flag. The longer flag expects to be preceded by
80     '--' as e.g. '--help' for help. The shorter flag expects to be
81     preceded by '-' as e.g. '-h', and can also be concatenated like
82     "program -vf", which is equivalent to "program -v -f". or
83     its sibblings for different types.
84  */
85  class CommandLine
86  {
87  public:
88    /**
89       \brief default constructor
90
91       \param str text preceeding the list of option in output
92    */
93    CommandLine(std::string str="Available options are:");
94
95    /**
96       \brief Destructor
97    */
98    virtual ~CommandLine(void);
99
100    /**
101       \brief Function to add an option.
102    */
103    void add(Option&);
104
105    /**
106       \brief Allow at most \a n free arguments.
107
108       An free argument is an argument not associated with an Option,
109       allowing commandlines such as \code prog foo bar \endcode
110     */
111    void allow_free_args(size_t n);
112
113    /**
114       \brief Arguments not associated with an Option
115
116       \see allow_free_args(size_t n)
117    */
118    const std::vector<std::string>& free_args(void) const;
119
120    /**
121       \brief parse the commandline
122
123       throw cmd_error if an error is detected.
124    */
125    void parse(int argc, char* argv[]);
126
127    /**
128       @return Name of more; more specifically argv[0] is
129       stripped so only string after the last '/' remains.
130     */
131    std::string program_name(void) const;
132
133    /**
134       \return something like "Try `<program_name()> --help` for
135       more information."
136     */
137    std::string try_help(void) const;
138
139  private:
140    friend std::ostream& operator<<(std::ostream& os, const CommandLine& cl);
141    void add_private(std::string, Option&);
142    bool is_long_option(std::string str) const;
143    bool is_short_option(std::string str) const;
144    std::vector<std::string> split(std::string str, char del) const;
145
146    std::string description_;
147    std::vector<std::string> free_arg_;
148    size_t free_arg_max_;
149    std::vector<Option*> options_;
150    std::map<char, Option*> short_options_;
151    std::map<std::string, Option*> long_options_;
152    std::string program_name_;
153  };
154
155  /**
156     \brief CommandLine output operator
157
158     A typical output may look like this
159     \verbatim
160     Available options are:
161     -h, --help      display this help and exit
162     -v, --verbose   explain what is being done
163     \endverbatim
164     The output starts with a descriptive line such as "Available
165     options are:" (default) that can be set in constructor. Then follows the
166     options described in the order they were added to
167     Commandline. Each Option is described according to
168     Option::print(void) function.
169     \see OptionHelp
170  */
171  std::ostream& operator<<(std::ostream&, const CommandLine&);
172
173}} // end of namespace svndigest and theplu
174
175#endif
Note: See TracBrowser for help on using the repository browser.