Общая библиотека для работы с торговым оборудованием.  1.0.0
qgetopt.h
1 /**********************************************************************
2  * Copyright (c) 2003, 2004, froglogic Porten & Stadlbauer GbR
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials
15  * provided with the distribution.
16  *
17  * * Neither the name of the froglogic nor the names of its
18  * contributors may be used to endorse or promote products
19  * derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  **********************************************************************/
36 
37 #ifndef QGETOPT_H
38 #define QGETOPT_H
39 
40 #include <qstring.h>
41 #include <qstringlist.h>
42 #include <qmap.h>
43 #include "teglobal.h"
44 
45 class LIB_EXPORT GetOpt {
46 public:
47  GetOpt();
48  GetOpt( int offset );
49  GetOpt( int argc, char *argv[] );
50  GetOpt( const QStringList &a );
51 
52  QString appName() const { return aname; }
53 
54  // switch (no arguments)
55  void addSwitch( const QString &lname, bool *b );
56 
57  // options (with arguments, sometimes optional)
58  void addOption( char s, const QString &l, QString *v );
59  void addVarLengthOption( const QString &l, QStringList *v );
60  void addRepeatableOption( char s, QStringList *v );
61  void addRepeatableOption( const QString &l, QStringList *v );
62  void addOptionalOption( const QString &l, QString *v,
63  const QString &def );
64  void addOptionalOption( char s, const QString &l,
65  QString *v, const QString &def );
66 
67  // bare arguments
68  void addArgument( const QString &name, QString *v );
69  void addOptionalArgument( const QString &name, QString *v );
70 
71  bool parse( bool untilFirstSwitchOnly );
72  bool parse() { return parse( false ); }
73 
74  bool isSet( const QString &name ) const;
75 
76  int currentArgument() const { return currArg; }
77 
78 private:
79  enum OptionType { OUnknown, OEnd, OSwitch, OArg1, OOpt, ORepeat, OVarLen };
80 
81  struct LIB_EXPORT Option;
82  friend struct Option;
83 
84  struct Option {
85  Option( OptionType t = OUnknown,
86  char s = 0, const QString &l = QString::null )
87  : type( t ),
88  sname( s ),
89  lname( l ),
90  boolValue( 0 ) { }
91  bool operator==(const Option & opt) const
92  {
93  return (type==opt.type) && (sname==opt.sname) && (lname==opt.lname);
94  }
95 
96  OptionType type;
97  char sname; // short option name (0 if none)
98  QString lname; // long option name (null if none)
99  union {
100  bool *boolValue;
101  QString *stringValue;
102  QStringList *listValue;
103  };
104  QString def;
105  };
106 
107 #include "templexports.h"
108 
109  QValueList<Option> options;
110  typedef QValueList<Option>::const_iterator OptionConstIterator;
111 // template class LIB_EXPORT QMap<QString, int>;
112  QMap<QString, int> setOptions;
113 
114  void init( int argc, char *argv[], int offset = 1 );
115  void addOption( Option o );
116  void setSwitch( const Option &o );
117 
118  QStringList args;
119  QString aname;
120 
121  int numReqArgs;
122  int numOptArgs;
123  Option reqArg;
124  Option optArg;
125 
126  int currArg;
127 };
128 
129 template class LIB_EXPORT QValueList<GetOpt::Option>;
130 template class LIB_EXPORT QValueListIterator<GetOpt::Option>;
131 template class LIB_EXPORT QValueListConstIterator<GetOpt::Option>;
132 
133 #endif
134 
A command line option parser.
Definition: qgetopt.h:45
bool parse()
Definition: qgetopt.h:72