-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargparse_cmdlineoption.h
606 lines (525 loc) · 28 KB
/
argparse_cmdlineoption.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
// Deal with a single command line option
// Copyright (C) 2017 Harro Verkouter, [email protected]
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef ARGPARSE11_CMDLINEOPTION_H
#define ARGPARSE11_CMDLINEOPTION_H
#include <argparse_basics.h>
#include <argparse_actions.h>
#include <tuple>
#include <string>
#include <memory>
#include <iterator>
#include <iostream>
#include <algorithm>
#include <functional>
namespace argparse { namespace detail {
///////////////////////////////////////////////////////////////////////////////////////////////
// the command line objects
///////////////////////////////////////////////////////////////////////////////////////////////
template <typename C, typename Iter>
void maybe_print(std::string const& topic, C const& c, Iter& iter) {
if( !c.empty() ) {
if( !topic.empty() )
*iter++ = topic;
std::copy(std::begin(c), std::end(c), iter);
}
}
// forward-declarartions so's we can have pointers-to
struct CmdLineOptionIF;
template <typename T> struct CmdLineOptionStorage;
using CmdLineOptionPtr = std::shared_ptr<CmdLineOptionIF>;
using ConstCmdLineOptionPtr = std::shared_ptr<CmdLineOptionIF const>;
// Non-templated baseclass
struct CmdLineOptionIF {
using condition_f = std::function<void(unsigned int, CmdLineOptionIF const*)>;
friend class argparse::ArgumentParser;
template <typename... Props>
friend CmdLineOptionPtr mk_argument(CmdLineBase*, Props&&...);
template <typename... Props>
friend CmdLineOptionPtr mk_argument(CmdLineBase*, std::tuple<Props...>&&);
template <typename... Props>
friend CmdLineOptionPtr mk_argument(CmdLineBase*, std::tuple<Props...>const&);
CmdLineOptionIF():
__m_requires_argument( false ),
__m_count( 0 ),
__m_precondition_f( nullptr ), __m_postcondition_f( nullptr )
{}
virtual std::string docstring( void ) const {
return std::string();
}
// Make sure we get a non-const lvalue reference
// we don't want r-value refs
template <typename T>
bool get(T& t) const {
// See if we can fulfill the request
using theType = typename std::decay<T>::type;
CmdLineOptionStorage<theType> const* upcast = dynamic_cast<CmdLineOptionStorage<theType> const*>(this);
if( !upcast )
throw std::runtime_error("Bad cast - requested option type is not actual type");
return upcast->get(t);
}
template <typename T>
T const& get( void ) const {
// See if we can fulfill the request
using theType = typename std::decay<T>::type;
CmdLineOptionStorage<theType> const* upcast = dynamic_cast<CmdLineOptionStorage<theType> const*>(this);
if( !upcast )
throw std::runtime_error("Bad cast - requested option type is not actual type");
return upcast->get();
}
// Process an actual command line argument
virtual void processArgument(std::string const&) = 0;
virtual ~CmdLineOptionIF() {}
// We could protect these members but that wouldn't be a lot of use
bool __m_requires_argument;
bool __m_required;
bool __m_invisible;
// pre-format the option's name(s), required/optional and, if applicable, type of argument:
std::string __m_usage;
unsigned int __m_count;
namecollection_t __m_names;
docstringlist_t __m_docstring;
docstringlist_t __m_defaults;
docstringlist_t __m_constraints;
docstringlist_t __m_requirements;
// Print the help for this option
void mini_help(std::ostream& os, bool usage) const {
if( __m_invisible )
return;
os << __m_usage << std::endl;
if( !usage ) {
std::ostream_iterator<std::string> lineprinter(std::cout, "\n\t");
// Print any documentation
maybe_print("\r\t", __m_docstring, lineprinter);
// Print only one default, if there's any
if( __m_defaults.size() )
os << "\r\tdefault: " << *__m_defaults.begin() << std::endl;
// list constraints and requirements
maybe_print("\r\t", __m_constraints, lineprinter);
maybe_print("\r\t", __m_requirements, lineprinter);
}
os << "\n";
}
protected:
// If an option has a default then it can override
// this one to set it
virtual void set_default( void ) { }
condition_f __m_precondition_f;
condition_f __m_postcondition_f;
private:
};
// We have to discriminate between two stages:
// 1. storing what was collected from the command line
// 2. processing the individual bits from the command line
// and they don't need to be equal ...
//
// To wit, the "collect" action collectes elements of type X
// and stores them in type Y.
// Some actions ignore the value on the command line (Y==ignore)
// but still store some value of type X.
// Sometimes X == Y, for the simple store_value<Y> actions
//
// Default should apply to X, constraints to Y?
// (But suppose action = store const and user set default and constraints,
// then Y==ignore then still need to apply constraint to default ...)
//
// So build a hierarchy:
//
// CmdLineOptionIF => template CmdLineOptionStorage<> => template <> CmdLineOption
//
// CmdLineOptionStorage<> stores values of type Y
//
// CmdLineOption<> processes values of type X and
// store them in base-class CmdLineOptionStorage<Y>
//
template <typename StoredType>
struct CmdLineOptionStorage: CmdLineOptionIF, value_holder<typename std::decay<StoredType>::type> {
using type = typename std::decay<StoredType>::type;
using holder_type = value_holder<type>;
using default_f = std::function<void(void)>;
CmdLineOptionStorage():
__m_default_f( nullptr )
{}
bool get(type& t) const {
if( __m_count==0 ) {
if( __m_default_f )
__m_default_f();
}
if( this->__m_count )
t = this->holder_type::__m_value;
return this->__m_count>0;
}
type const& get( void ) const {
if( __m_count==0 ) {
if( __m_default_f )
__m_default_f();
}
// not specified and no default? cannot get the thing!
if( this->__m_count==0 )
throw std::runtime_error("get(): Option was not set on the command line and no default was specified.");
return this->holder_type::__m_value;
}
default_f __m_default_f;
};
template <typename ElementType, typename StoredType>
struct CmdLineOption: CmdLineOptionStorage<typename std::decay<StoredType>::type> {
using type = typename std::decay<ElementType>::type;
using holder_type = CmdLineOptionStorage<typename std::decay<StoredType>::type>;
using held_type = typename holder_type::type;
using constraint_f = Constraint<type>;
using process_arg_f = std::function<void(held_type&, std::string const&)>;
CmdLineOption():
__m_constraint_f( nullptr ),
__m_process_arg_f( nullptr )
{}
void processArgument(std::string const& v) {
this->CmdLineOptionIF::__m_precondition_f( this->CmdLineOptionIF::__m_count, this );
// Request the action to do it's thing
__m_process_arg_f(this->holder_type::__m_value, v);
// Chalk up another entry of this command line option
this->CmdLineOptionIF::__m_count++;
}
constraint_f __m_constraint_f;
process_arg_f __m_process_arg_f;
};
// A functor to extract the name from any type
struct name_getter_t {
template <typename U>
std::string operator()(U const& u) const {
return name_getter_t::to_str(u.__m_value);
}
static std::string to_str(std::string const& s) {
return s;
}
static std::string to_str(char const& c) {
return std::string(&c, &c+1);
}
};
///////////////////////////////////////////
// Depending on wether action ignores its
// argument or not return the correct
// processing function
//////////////////////////////////////////
template <typename U, typename V>
struct ignore_both:
std::integral_constant<bool, std::is_same<typename std::decay<U>::type, typename std::decay<V>::type>::value &&
std::is_same<typename std::decay<U>::type, ignore_t>::value>
{};
template <typename Value>
struct ignore_both_t {
using type = std::function<void(Value&, std::string const&)>;
template <typename Object, typename Action, typename Convert, typename Constrain, typename PreConstrain>
static type mk(const Object* o, Action const& action, Convert const&, Constrain const&, PreConstrain const&) {
return [=](Value&, std::string const&) {
// these actions ignore everything
action(o, std::cref(std::ignore));
};
}
static constexpr bool usesArgument = false;
};
template <typename Value>
struct ignore_argument_t {
using type = std::function<void(Value&, std::string const&)>;
template <typename Object, typename Action, typename Convert, typename Constrain, typename PreConstrain>
static type mk(const Object*, Action const& action, Convert const&, Constrain const& constrain, PreConstrain const&) {
return [=](Value& v, std::string const&) {
// these actions ingnore the value passed in but rather,
// they'll do something with the value stored in them
// so we better apply our constraints to that value
// before using it
constrain(action.__m_value);
// passed constraint, now apply
action(v, std::cref(std::ignore));
};
}
static constexpr bool usesArgument = false;
};
template <typename Value, typename Element>
struct use_argument_t {
using type = std::function<void(Value&, std::string const&)>;
template <typename Object, typename Action, typename Convert, typename Constrain, typename PreConstrain>
static type mk(const Object*, Action const& action, Convert const& convert, Constrain const& constrain, PreConstrain const& preconstrain) {
return [=](Value& v, std::string const& s) {
preconstrain(s);
Element tmp;
convert(tmp, s);
constrain(tmp);
action(v, tmp);
};
}
static constexpr bool usesArgument = true;
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Another gruesome thing - this is DAS TEMPLATE
// this is what it's all about. Collect the user specified properties
// and turn them into a usable command line option object.
//
// Really. This is the function what does it all - enforcing, setting
// defaults, constraining, building docstrings etc.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////
template <typename... Props>
CmdLineOptionPtr mk_argument(CmdLineBase* cmdline, std::tuple<Props...>const& props) {
// get the action!
const auto allAction = get_all<action_t>( props );
static_assert( std::tuple_size<decltype(allAction)>::value==1, "You must specify exactly one Action" );
// Once we know what the action is, we know:
// 1. the type of the element(argument) it expects
// 2. the type where to store the element(s)
auto theAction = std::get<0>(allAction);
using actionType = decltype(theAction);
using actionValue = typename std::decay<typename actionType::type>::type;
using actionElement = typename std::decay<typename actionType::element_type>::type;
// From the amount of names (short and/or long ones) we can infer wether
// this is a command line option or an argument
auto allNames = get_all<name_t>( props );
constexpr bool isArgument = (std::tuple_size< decltype(allNames) >::value == 0);
// Remember if the option takes an argument or not (element type == ignore_t)
// De Morgan: !a && !b == !(a || b)
constexpr bool requiresArgument = !(isArgument || std::is_same<actionElement, ignore_t>::value);
// Depending on /that/ we can infer to which type to apply any
// constraints; it's either applying a constraint o the action's stored
// type (actionValue) or to the argument to the option (actionArgument)
// Use the actionElement as type if an argument is required, otherwise
// use the actionValue /UNLESS/ that is ignore_t in which case we fall
// back to using the actionElement again
using actionArgument =
typename std::conditional<isArgument || requiresArgument,
actionElement,
typename std::conditional<std::is_same<actionValue, ignore_t>::value,
actionElement,
actionValue>::type
>::type;
// Once we know what the action wants to store we can already
// construct the appropriate command line option:
auto optionPtr = std::make_shared< CmdLineOption<actionArgument, actionValue> >();
auto optionStore = reinterpret_cast< CmdLineOptionStorage<actionValue>* >(optionPtr.get());
auto optionIF = reinterpret_cast< CmdLineOptionIF* >(optionPtr.get());
// Collect all names and verify they don't collide
// if no names were given this should be a command line argument
// i.e. the option /is/ the value
functools::copy(functools::map(allNames, name_getter_t()),
std::inserter(optionIF->__m_names, optionIF->__m_names.end()));
// Create a descriptive name for displaying errors
const std::string name_s = [&]( void ){
std::ostringstream err;
// The nameless command line option ("positional argument") has no name ...
if( optionIF->__m_names.empty() ) {
err << "the positional argument (the nameless option)";
} else {
// Names are sorted by reverse size
unsigned int n{ 0 };
err << "the option '";
for(auto const& nm: reversed(optionIF->__m_names))
err << (n++ ? " " : "") << (nm.size()==1 ? "-" : "--") << nm;
err << "'";
}
return err.str();
}();
/////////////////// Pre-Constraint handling //////////////////////////
//
// This one operates at the string-representation level on the
// command line; i.e. before conversion is attempted
// Here we count the preconstraints and make the function. Later on
// - when we've decided what the 'actionmaker' is - we can test
// wether the action actually uses the string on the command line
// and thus wether it actually makes sense to try to execute such a
// constraint
auto allPreconstraints = get_all<formatcondition>( props );
constexpr bool hasPreconstraint = (std::tuple_size< decltype(allPreconstraints) >::value > 0);
auto DoPreConstrain =
constraint_maker<formatcondition, std::string, ExecuteConstraints>::mk( optionIF->__m_constraints,
allPreconstraints /*std::forward_as_tuple(props...)*/ );
auto preconstrain_f = Constraint<std::string>(
[=](std::string const& value) {
DoPreConstrain(value);
return true;
});
/////////////////// Constraint handling /////////////////////////////
//
// Allow for any number of constraints to apply to an element's value
// We must do this such that we can verify if e.g. a specified
// default violates these constraints
auto DoConstrain =
constraint_maker<constraint, actionArgument, ExecuteConstraints>::mk( optionIF->__m_constraints, props );
optionPtr->__m_constraint_f = Constraint<actionArgument>(
[=](actionArgument const& value) {
DoConstrain(value);
return true;
});
/////////////////// Argument count constraints //////////////////////
//
// Allow for setting pre- and/or post conditions on the amount of
// times the option is allowed
using argcount_t = decltype(optionIF->__m_count);
auto DoPreCond = constraint_maker<precondition, argcount_t, ExecuteConstraints>::mk( optionIF->__m_requirements, props );
auto DoPostCond = constraint_maker<postcondition, argcount_t, ExecuteConstraints>::mk( optionIF->__m_requirements, props );
optionIF->__m_precondition_f = [=](argcount_t v, CmdLineOptionIF const*) { DoPreCond(v); };
optionIF->__m_postcondition_f = [=](argcount_t v, CmdLineOptionIF const*) { DoPostCond(v); };
// a commandline option/argument is required if there is (at least one)
// postcondition that fails with a count of 0
try {
optionIF->__m_postcondition_f(0, nullptr);
optionIF->__m_required = false;
}
catch( std::exception const& ) {
optionIF->__m_required = true;
}
// Wether to include ellipsis; out of [0 or 1, 0 or more, 1 or more]
// the latter two need to have "..." to indicate "or more"
// Test wether there is (a) precondition that fails on '1'
// because if it does then the option is not allowed to be present more
// than once
std::string ellipsis;
try {
optionIF->__m_precondition_f(1, nullptr);
ellipsis = "...";
}
catch( std::exception const& ) { }
/////////////////// Visibility handling /////////////////////////////
//
// Regrettably we cannot at compile time deduce if the user is
// doing something insensible, like creating a hidden command line
// option and making it required.
// At the moment we can only do this at runtime ...
// But we make that a fatal one
//
// The one (simple) way to do this at *compile* time is the
// following:
// - if one assumes a postcondition on the argument count
// ("at_least(n)" or "exactly(n)" or "required()")
// only makes /sense/ if n>0 (without actually testing
// the value of "n")
// - then we can count the number of postconditions:
// because they indicate the option is NOT optional (by
// the *assumption* that n>0)
// - and then test if the hidden attribute is set in the
// properties
// - THEN we can do a static assert at compile time
//
// This approach was rejected because "n=0" in any of the
// postconditions may be a no-op but not actually inconsistent.
// Preventing the ppl from doing stupid things also prevents them
// from doing smart things is a principle which carries a lot of weight.
optionIF->__m_invisible = std::tuple_size< decltype(get_all<invisible_t>(props)) >::value > 0;
if( optionIF->__m_invisible && optionIF->__m_required )
fatal_error(std::cerr, name_s, " is both invisible AND required. ", ENDL(std::cerr),
"This combination is confusing for the user: we can't display what he or she /should/ have entered yet she or he is required to.", ENDL(std::cerr),
"Therefore the framework forbids construction of this option.");
/////////////////// Default handling /////////////////////////////
//
// Generate a function to set the default value, if one was supplied.
// Make sure that at most one was given ...
// Note that the default applies to the action's stored type not the
// action's argument type
auto allDefaults = get_all<default_t>( props );
static_assert( std::tuple_size<decltype(allDefaults)>::value<=1, "You may specify one default at most" );
// Now filter the ones whose actual default type is not ignore_t, which
// just indicates that the user wasn't supposed to set a default ...
auto actualDefaults = functools::filter_t<is_actual_default::template test>( allDefaults );
constexpr std::size_t nDefault = std::tuple_size< decltype(actualDefaults) >::value;
// Allow only defaults to be set where the stored type == argument type?
static_assert( nDefault==0 || std::is_same<actionArgument, actionValue>::value,
"You can only set defaults for options that store values, not collect them" );
// Verify that any remaining defaults have the correct type
auto okDefaults = functools::filter_t<is_ok_default<actionArgument>::template test>( actualDefaults );
static_assert( std::tuple_size<decltype(okDefaults)>::value==std::tuple_size<decltype(actualDefaults)>::value,
"The type of the default is incompatible with the type of the option" );
// Verify that any defaults that are set do not violate any constraints
try {
functools::map(okDefaults, default_constrainer_t(), optionPtr->__m_constraint_f);
}
catch( std::exception const& e ) {
// So the default triggered an error. Turn this into an
// intelligible error message
fatal_error(std::cerr, e.what(), " creating ",name_s);
}
// Now we can blindly 'map' the default setter over ALL defaults (well,
// there's at most one ...). This function can be void(void) because
// it requires no further inputs than everything we already
// have available here ... and they don't violate any of the constraints
optionStore->__m_default_f =
[=](void) -> void {
functools::map(okDefaults, default_setter_t(), optionPtr);
};
// Capture the defaults
auto defbuilder = std::inserter(optionIF->__m_defaults, optionIF->__m_defaults.end());
functools::copy(functools::map(okDefaults, docstr_getter_t()), defbuilder);
/////////////////// Conversion handling /////////////////////////////
//
// Allow users to specify their own converter for string -> element
// We append the built-in default conversion and take the first
// element from the tuple, which is guaranteed to exists
auto allConverters = std::tuple_cat(get_all<conversion_t>( props ),
std::make_tuple(std_conversion_t()));
// The user may specify up to one converter so the total number
// of converters that we find must be >=1 && <=2
constexpr std::size_t nConvert = std::tuple_size< decltype(allConverters) >::value;
static_assert( nConvert>=1 && nConvert<=2,
"You may specify at most one user-defined converter");
// Verify that the converter has an operator of the correct type?
static_assert( !(isArgument || requiresArgument) ||
has_exact_operator<typename std::tuple_element<0, decltype(allConverters)>::type const,
void, actionArgument&, std::string const&>::value,
"The converter can not convert to the requested value of the action" );
// We need to build a function that takes a string and executes the
// action with the converted string
optionPtr->__m_requires_argument = requiresArgument;
// Deal with documentation - we may add to this later
auto allDocstr = get_all<docstring_t>( props );
auto docstrbuilder = std::inserter(optionIF->__m_docstring, optionIF->__m_docstring.end());
functools::copy(functools::map(allDocstr, docstr_getter_t()), docstrbuilder);
// we may add docstr()'d constraints, defaults and whatnots.
// Now we can pre-format the option's usage
// [...] if not required
// -<short name> --long-name [<type>]
// (type only appended if argument required)
// + ellipsis to indicate "or more"
//
unsigned int n( 0 );
std::ostringstream usage;
// Names are sorted by reverse size
for(auto const& nm: reversed(optionIF->__m_names))
usage << (n++ ? " " : "") << (nm.size()==1 ? "-" : "--") << nm;
if( optionIF->__m_requires_argument || isArgument )
usage << (n ? " " : "") << "<" << optiontype<actionElement>() << ">" << ellipsis;
optionIF->__m_usage = usage.str();
if( !optionIF->__m_required )
optionIF->__m_usage = "[" + optionIF->__m_usage + "]";
/////////////////// The actual action builder /////////////////////////////
//
// Depending on wether we actually need to look at the (converted)
// value we choose the correct action maker
using actionMaker = typename
std::conditional<ignore_both<actionValue, actionElement>::value,
ignore_both_t<actionValue>,
typename std::conditional<isArgument || requiresArgument,
use_argument_t<actionValue, actionArgument>,
ignore_argument_t<actionValue>
>::type
>::type;
// Time to check wether there are pre-constraints and if it makes
// sense to enforce those
static_assert( !hasPreconstraint || actionMaker::usesArgument,
"You have specified constraint(s) on the command line string but the action does not use it." );
// Pass the zeroth element of the converters into the processing
// function - it's either the user's one or our default one
optionPtr->__m_process_arg_f = actionMaker::mk(cmdline, theAction, std::get<0>(allConverters),
optionPtr->__m_constraint_f, preconstrain_f);
return optionPtr;
}
} } // namespace argparse, namespace detail
#endif