MySQL++  3.2.5
cmdline.h
1 /***********************************************************************
2  cmdline.h - Declares the interface to the MySQL++'s command line
3  parsing logic, used by the examples and the utility programs.
4  Not intended for use by third parties! If it breaks, you
5  get to keep all the pieces.
6 
7  Copyright (c) 2007-2009 by Educational Technology Resources, Inc.
8  Others may also hold copyrights on code in this file. See the
9  CREDITS.txt file in the top directory of the distribution for details.
10 
11  This file is part of MySQL++.
12 
13  MySQL++ is free software; you can redistribute it and/or modify it
14  under the terms of the GNU Lesser General Public License as published
15  by the Free Software Foundation; either version 2.1 of the License, or
16  (at your option) any later version.
17 
18  MySQL++ is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
21  License for more details.
22 
23  You should have received a copy of the GNU Lesser General Public
24  License along with MySQL++; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
26  USA
27 ***********************************************************************/
28 
29 #if !defined(MYSQLPP_CMDLINE_H)
30 #define MYSQLPP_CMDLINE_H
31 
32 #include "common.h"
33 
34 #include <string>
35 #include <vector>
36 
37 #include <assert.h>
38 
39 namespace mysqlpp {
44  class MYSQLPP_EXPORT CommandLineBase
45  {
46  public:
49  typedef std::vector<std::string> ArgumentList;
51  typedef ArgumentList::const_iterator ArgumentListIt;
52 
56  const ArgumentList& extra_args() const
57  { return extra_args_; }
58 
61  operator void*() const
62  {
63  return successful_ ? const_cast<bool*>(&successful_) : 0;
64  }
65 
66  protected:
69  CommandLineBase(int argc, char* const argv[], const char* opts) :
70  argc_(argc),
71  argv_(argv),
72  opts_(opts),
73  successful_(argc > 0 && argv && opts)
74  {
75  assert(successful_);
76  }
78  virtual ~CommandLineBase() { }
79 
83  void finish_parse();
84 
86  const char* option_argument() const;
88  int option_index() const;
89 
95  void parse_error(const char* message = 0);
96 
98  int parse_next() const;
99 
101  virtual void print_usage() const = 0;
102 
104  const char* program_name() const { return argv_[0]; }
105 
108  bool successful() const { return successful_; }
109 
110  private:
112  int argc_;
113  char* const* argv_;
114  const char* opts_;
115  bool successful_;
116  ArgumentList extra_args_;
117  };
118 
119 
121  namespace examples {
123  extern MYSQLPP_EXPORT const char* db_name;
124 
126  class MYSQLPP_EXPORT CommandLine : public CommandLineBase
127  {
128  public:
131  CommandLine(int argc, char* const argv[],
132  const char* user = 0, const char* pass = 0,
133  const char* usage_extra = 0);
134 
139  void print_usage() const { print_usage(usage_extra_); }
140 
144  void print_usage(const char* extra) const;
145 
150  bool dtest_mode() const { return dtest_mode_; }
151 
153  const char* pass() const { return pass_; }
154 
160  int run_mode() const { return run_mode_; }
161 
163  const char* server() const { return server_; }
164 
166  const char* user() const { return user_; }
167 
168  private:
170  // Examples-specific command line parse results
171  bool dtest_mode_;
172  int run_mode_;
173  const char* server_;
174  const char* user_;
175  const char* pass_;
176  const char* usage_extra_;
177  };
178  } // end namespace mysqlpp::examples
179 
180 
182  namespace ssqlsxlat {
184  class MYSQLPP_EXPORT CommandLine : public CommandLineBase
185  {
186  public:
189  enum SourceSink {
190  ss_unknown,
191  ss_ssqls1,
192  ss_ssqls2,
193  ss_table
194  };
195 
198  CommandLine(int argc, char* const argv[]);
199 
201  void print_usage() const;
202 
207  const char* input() const { return input_; }
208 
210  SourceSink input_source() const { return input_source_; }
211 
213  SourceSink output_sink() const { return output_sink_; }
214 
216  const char* output() const { return output_; }
217 
219  const char* pass() const { return pass_; }
220 
222  const char* server() const { return server_; }
223 
225  const char* user() const { return user_; }
226 
227  private:
229  const char* input_;
230  const char* output_;
231  const char* pass_;
232  const char* server_;
233  const char* user_;
234  SourceSink input_source_;
235  SourceSink output_sink_;
236  };
237  } // end namespace mysqlpp::ssqlsxlat
238 } // end namespace mysqlpp
239 
240 #endif // !defined(MYSQLPP_CMDLINE_H)
241 
mysqlpp::CommandLineBase::print_usage
virtual void print_usage() const =0
Show a message explaining the program's proper usage.
mysqlpp::CommandLineBase::successful
bool successful() const
Returns true if nothing has gone wrong since calling the ctor.
Definition: cmdline.h:160
mysqlpp::ssqlsxlat::CommandLine::ss_ssqls1
@ ss_ssqls1
a C++ file containing an SSQLS v1 declaration
Definition: cmdline.h:217
mysqlpp::CommandLineBase::parse_error
void parse_error(const char *message=0)
Called by a subclass when encountering a command line parsing error.
Definition: cmdline.cpp:184
mysqlpp::examples::CommandLine::CommandLine
CommandLine(int argc, char *const argv[], const char *user=0, const char *pass=0, const char *usage_extra=0)
Constructor.
Definition: cmdline.cpp:204
mysqlpp::CommandLineBase::finish_parse
void finish_parse()
If object is still marked as "successful", save non-option arguments to extra_args_ list....
Definition: cmdline.cpp:130
mysqlpp::ssqlsxlat::CommandLine::ss_ssqls2
@ ss_ssqls2
an SSQLS v2 file
Definition: cmdline.h:218
mysqlpp::CommandLineBase::option_argument
const char * option_argument() const
Accessor for getopt()'s optarg global.
Definition: cmdline.cpp:149
mysqlpp::ssqlsxlat::CommandLine::print_usage
void print_usage() const
Show a message explaining the program's proper usage.
Definition: cmdline.cpp:335
mysqlpp::CommandLineBase
Parses command line arguments and holds the results.
Definition: cmdline.h:70
mysqlpp::examples::db_name
const char * db_name
Name of examples' DB.
Definition: cmdline.cpp:199
mysqlpp::ssqlsxlat::CommandLine::SourceSink
SourceSink
Types of inputs that ssqlsxlat will accept.
Definition: cmdline.h:215
mysqlpp::examples::CommandLine
Command line parsing mechanism for ../examples/*.cpp.
Definition: cmdline.h:152
mysqlpp::CommandLineBase::option_index
int option_index() const
Accessor for getopt()'s optind global.
Definition: cmdline.cpp:160
mysqlpp::CommandLineBase::ArgumentList
std::vector< std::string > ArgumentList
Type for a list of arguments.
Definition: cmdline.h:101
common.h
This file includes top-level definitions for use both internal to the library, and outside it....
mysqlpp::CommandLineBase::parse_next
int parse_next() const
Wrapper for getopt()
Definition: cmdline.cpp:171
mysqlpp::CommandLineBase::program_name
const char * program_name() const
Get the file name of the program's executable.
Definition: cmdline.h:156
mysqlpp::ssqlsxlat::CommandLine::CommandLine
CommandLine(int argc, char *const argv[])
Constructor.
Definition: cmdline.cpp:265
mysqlpp::ssqlsxlat::CommandLine::ss_unknown
@ ss_unknown
no known input type given yet
Definition: cmdline.h:216
mysqlpp::ssqlsxlat::CommandLine::ss_table
@ ss_table
an existing DB table schema
Definition: cmdline.h:219
mysqlpp::examples::CommandLine::print_usage
void print_usage() const
Show a message explaining the program's proper usage.
Definition: cmdline.h:165