@@ -88,7 +88,7 @@ class Parser {
88
88
: info_(std::move(info)), subParsers(tuple){};
89
89
90
90
template <class Type , ArgName Name, auto arg1 = Unspecified(),
91
- auto arg2 = Unspecified(), class ... T>
91
+ auto arg2 = Unspecified(), bool ISPArg, class ... T>
92
92
auto createArg (T... args) {
93
93
static_assert (!Name.containsInvalidChar (), " Name has invalid char" );
94
94
static_assert (Name.hasValidNameLength (),
@@ -135,13 +135,16 @@ class Parser {
135
135
if constexpr (is_tuple_v<Type>) {
136
136
return NArgs{static_cast <int >(std::tuple_size_v<Type>)};
137
137
}
138
+ if constexpr (ISPArg) {
139
+ return NArgs (1 );
140
+ }
138
141
return NArgs (' ?' );
139
142
}
140
143
}();
141
144
142
- static_assert (!(is_array_v<Type> and nargs.nargs == 1 ),
145
+ static_assert (!(is_array_v<Type> and nargs.getNargs () == 1 ),
143
146
" Array size must be more than one" );
144
- static_assert (!(is_tuple_v<Type> and nargs.nargs == 1 ),
147
+ static_assert (!(is_tuple_v<Type> and nargs.getNargs () == 1 ),
145
148
" Tuple size must be more than one" );
146
149
147
150
static constexpr auto required = []() {
@@ -156,8 +159,7 @@ class Parser {
156
159
}
157
160
}();
158
161
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
159
- static_assert (!(std::string_view (Name) == std::string_view (PArg::name)),
160
- " Duplicated name" );
162
+ static_assert (SearchIndex<PArg, Name>::value == -1 , " Duplicated name" );
161
163
}
162
164
static_assert (
163
165
(Name.shortName == ' \0 ' ) ||
@@ -166,11 +168,11 @@ class Parser {
166
168
static_assert ( //
167
169
Argo::SearchIndex<Args, Name>::value == -1 , //
168
170
" Duplicated name" );
169
- static_assert ( //
170
- (nargs.nargs > 0 //
171
- || nargs.nargs_char == ' ?' //
172
- || nargs.nargs_char == ' +' //
173
- || nargs.nargs_char == ' *' ), //
171
+ static_assert ( //
172
+ (nargs.getNargs () > 0 //
173
+ || nargs.getNargsChar () == ' ?' //
174
+ || nargs.getNargsChar () == ' +' //
175
+ || nargs.getNargsChar () == ' *' ), //
174
176
" nargs must be '?', '+', '*' or int" );
175
177
176
178
ArgInitializer<Type, Name, nargs, required, ID>::init (
@@ -179,39 +181,47 @@ class Parser {
179
181
}
180
182
181
183
/* !
182
- * Type: type of argument
183
184
* Name: name of argument
184
- * arg1: ShortName or NArgs or Unspecified
185
- * arg2: NArgs or Unspecified
185
+ * Type: type of argument
186
+ * arg1: Required(bool) or NArgs or Unspecified
187
+ * arg2: Required(bool) or NArgs or Unspecified
186
188
*/
187
189
template <ArgName Name, class Type , auto arg1 = Unspecified(),
188
190
auto arg2 = Unspecified(), class ... T>
189
191
auto addArg (T... args) {
190
- auto arg = createArg<Type, Name, arg1, arg2>(std::forward<T>(args)...);
191
- return Parser<ID,
192
- tuple_append_t <Args, typename decltype (arg)::type>,
193
- PArg,
194
- HArg,
195
- SubParsers>(std::move (this ->info_ ), subParsers);
192
+ auto arg =
193
+ createArg<Type, Name, arg1, arg2, false >(std::forward<T>(args)...);
194
+ return Parser<ID, tuple_append_t <Args, typename decltype (arg)::type>, PArg,
195
+ HArg, SubParsers>(std::move (this ->info_ ), subParsers);
196
196
}
197
197
198
+ /* !
199
+ * Name: name of argument
200
+ * Type: type of argument
201
+ * arg1: Required(bool) or NArgs or Unspecified
202
+ * arg2: Required(bool) or NArgs or Unspecified
203
+ */
198
204
template <ArgName Name, class Type , auto arg1 = Unspecified(),
199
205
auto arg2 = Unspecified(), class ... T>
200
206
auto addPositionalArg (T... args) {
201
- static_assert (std::is_same_v<PArg, std::tuple<>>,
202
- " Positional argument cannot set more than one" );
203
207
static_assert (Name.shortName == ' \0 ' ,
204
208
" Positional argment cannot have short name" );
205
- auto arg = createArg<Type, Name, arg1, arg2>(std::forward<T>(args)...);
206
- return Parser<ID, Args, typename decltype (arg)::type, HArg, SubParsers>(
207
- std::move (this ->info_ ), subParsers);
209
+ auto arg =
210
+ createArg<Type, Name, arg1, arg2, true >(std::forward<T>(args)...);
211
+
212
+ static_assert (decltype (arg)::type::nargs.getNargsChar () != ' ?' ,
213
+ " Cannot assign narg: ? to the positional argument" );
214
+ static_assert (decltype (arg)::type::nargs.getNargsChar () != ' *' ,
215
+ " Cannot assign narg: * to the positional argument" );
216
+
217
+ return Parser<ID, Args, tuple_append_t <PArg, typename decltype (arg)::type>,
218
+ HArg, SubParsers>(std::move (this ->info_ ), subParsers);
208
219
}
209
220
210
221
template <ArgName Name, class ... T>
211
222
auto addFlag (T... args) {
212
223
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
213
- static_assert (!(std::string_view (Name) == std::string_view (PArg::name)),
214
- " Duplicated name" );
224
+ static_assert (SearchIndex<PArg, Name>::value == -1 , " Duplicated name" );
215
225
}
216
226
static_assert (
217
227
(Name.shortName == ' \0 ' ) ||
@@ -220,10 +230,7 @@ class Parser {
220
230
static_assert (Argo::SearchIndex<Args, Name>::value == -1 ,
221
231
" Duplicated name" );
222
232
FlagArgInitializer<Name, ID>::init (std::forward<T>(args)...);
223
- return Parser<ID,
224
- tuple_append_t <Args, FlagArg<Name, ID>>,
225
- PArg,
226
- HArg,
233
+ return Parser<ID, tuple_append_t <Args, FlagArg<Name, ID>>, PArg, HArg,
227
234
SubParsers>(std::move (this ->info_ ), subParsers);
228
235
}
229
236
@@ -257,8 +264,9 @@ class Parser {
257
264
throw ParseError (" Parser did not parse argument, call parse first" );
258
265
}
259
266
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
260
- if constexpr (std::string_view (Name) == std::string_view (PArg::name)) {
261
- return PArg::value;
267
+ if constexpr (SearchIndex<PArg, Name>::value != -1 ) {
268
+ return std::tuple_element_t <SearchIndex<PArg, Name>::value,
269
+ PArg>::value;
262
270
} else {
263
271
static_assert (SearchIndex<Args, Name>::value != -1 ,
264
272
" Argument does not exist" );
0 commit comments