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

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

Addresses #436. GPL license copy reference should also be updated.

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