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

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

trac moved to new location.

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