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

Last change on this file since 1197 was 1197, checked in by Peter, 14 years ago

docs

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