Qore Programming Language  1.12.0
QoreGetOpt.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreGetOpt.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 David Nichols
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_QOREGETOPT_H
33 
34 #define _QORE_QOREGETOPT_H
35 
36 #include <qore/common.h>
37 #include <qore/safe_dslist>
38 
39 #include <string>
40 #include <map>
41 
42 #define QGO_ERR_DUP_SHORT_OPT -1
43 #define QGO_ERR_DUP_LONG_OPT -2
44 #define QGO_ERR_DUP_NAME -3
45 #define QGO_ERR_NO_NAME -4
46 #define QGO_ERR_NO_OPTION -5
47 
48 #define QGO_OPT_NONE 0
49 #define QGO_OPT_ADDITIVE 1
50 #define QGO_OPT_LIST 2
51 #define QGO_OPT_MANDATORY 4
52 
53 #define QGO_OPT_LIST_OR_ADD (QGO_OPT_ADDITIVE|QGO_OPT_LIST)
54 
55 class QoreGetOptNode {
56 public:
57  std::string name;
58  char short_opt;
59  std::string long_opt;
60  qore_type_t argtype;
61  int option;
62 
63  DLLLOCAL QoreGetOptNode(const char* n, char so, const char* lo, qore_type_t at = -1, int o = QGO_OPT_NONE) :
64  name(n ? n : ""), short_opt(so), long_opt(lo ? lo : ""), argtype(at), option(o) {
65  }
66 
67  DLLLOCAL ~QoreGetOptNode() {
68  }
69 };
70 
71 typedef std::map<const char*, QoreGetOptNode*, ltstr> getopt_long_map_t;
72 typedef std::map<char, QoreGetOptNode*, ltchar> getopt_short_map_t;
73 typedef safe_dslist<QoreGetOptNode*> getopt_node_list_t;
74 
75 class QoreGetOpt {
76 private:
77  getopt_long_map_t long_map;
78  getopt_short_map_t short_map;
79  getopt_node_list_t node_list;
80 
81  DLLLOCAL static DateTimeNode* parseDate(const char* val);
82  DLLLOCAL void processLongArg(const char* arg, QoreListNode* l, QoreHashNode* h, unsigned& i, bool modify);
83  DLLLOCAL int processShortArg(const char* arg, QoreListNode* l, QoreHashNode* h, unsigned& i, int& j, bool modify);
84  DLLLOCAL QoreGetOptNode* find(const char* opt) const;
85  DLLLOCAL QoreGetOptNode* find(char opt) const;
86  DLLLOCAL void doOption(QoreGetOptNode* n, QoreHashNode* h, const char* val);
87  DLLLOCAL char* getNextArgument(class QoreListNode* l, QoreHashNode* h, unsigned& i, const char* lopt, char sopt);
88 
89 public:
90  DLLLOCAL QoreGetOpt() {
91  }
92 
93  DLLLOCAL ~QoreGetOpt();
94  // returns 0 for OK
95  DLLLOCAL int add(const char* name, char short_opt, const char* long_opt, qore_type_t argtype = -1, int option = QGO_OPT_NONE);
96  DLLLOCAL QoreHashNode* parse(QoreListNode* l, bool ml, ExceptionSink* xsink);
97 };
98 
99 #endif // _QORE_QOREGETOPT_H
Qore's parse tree/value type for date-time values, reference-counted, dynamically-allocated only.
Definition: DateTimeNode.h:45
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
int16_t qore_type_t
used to identify unique Qore data and parse types (descendents of AbstractQoreNode)
Definition: common.h:70