OpenImageIO
 All Classes Files Friends Macros Pages
argparse.h
Go to the documentation of this file.
1 /*
2  Copyright 2008 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4  Based on BSD-licensed software Copyright 2004 NVIDIA Corp.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14  * Neither the name of the software's owners nor the names of its
15  contributors may be used to endorse or promote products derived from
16  this software without specific prior written permission.
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29  (This is the Modified BSD License)
30 */
31 
32 
35 
36 
37 #ifndef OPENIMAGEIO_ARGPARSE_H
38 #define OPENIMAGEIO_ARGPARSE_H
39 
40 #if defined(_MSC_VER)
41 // Ignore warnings about DLL exported classes with member variables that are template classes.
42 // This happens with the std::string m_errmessage member of ArgParse below.
43 # pragma warning (disable : 4251)
44 #endif
45 
46 #include <vector>
47 
48 #include "export.h"
49 #include "version.h"
50 #include "tinyformat.h"
51 
52 
53 OIIO_NAMESPACE_ENTER
54 {
55 
56 
57 class ArgOption; // Forward declaration
58 
59 
60 
143 
144 
145 class OIIO_API ArgParse {
146 public:
147  ArgParse (int argc=0, const char **argv=NULL);
148  ~ArgParse ();
149 
158  int options (const char *intro, ...);
159 
162  int parse (int argc, const char **argv);
163 
167  std::string geterror () const;
168 
172  void usage () const;
173 
176  std::string command_line () const;
177 
178 private:
179  int m_argc; // a copy of the command line argc
180  const char **m_argv; // a copy of the command line argv
181  mutable std::string m_errmessage; // error message
182  ArgOption *m_global; // option for extra cmd line arguments
183  std::string m_intro;
184  std::vector<ArgOption *> m_option;
185 
186  ArgOption *find_option(const char *name);
187  // void error (const char *format, ...)
188  TINYFORMAT_WRAP_FORMAT (void, error, ,
189  std::ostringstream msg;, msg, m_errmessage = msg.str();)
190 
191  int found (const char *option); // number of times option was parsed
192 };
193 
194 
195 }
196 OIIO_NAMESPACE_EXIT
197 
198 
199 #endif // OPENIMAGEIO_ARGPARSE_H