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

Last change on this file since 1466 was 1466, checked in by Peter, 13 years ago

refs #413

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