-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathargparse_basics.h
151 lines (124 loc) · 5.83 KB
/
argparse_basics.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
// We need some classes and types defined before others
// Copyright (C) 2017 Harro Verkouter, verkouter@jive.eu
//
// 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_BASICS_H
#define ARGPARSE11_BASICS_H
#include <argparse_detail.h>
#include <set>
#include <list>
#include <string>
#include <sstream>
#include <exception>
#include <memory>
#include <typeindex>
#include <functional>
#include <type_traits>
#include <cstdlib>
namespace argparse {
// forward declaration
class ArgumentParser;
struct CmdLineBase {
// Print help, short (true) or long (false) format
virtual void print_help( bool ) const = 0;
virtual void print_version( void ) const = 0;
virtual ~CmdLineBase() {}
};
struct constraint_violation: public std::domain_error {
using std::domain_error::domain_error;
};
// The constraint implementation details shouldn't have to be in the
// argparse namespace
namespace detail {
template <typename T>
using Constraint = std::function<bool(typename std::decay<T>::type const&)>;
template <typename T>
using ConstraintS = std::function<std::string(typename std::decay<T>::type const&)>;
struct constraint_impl {
constraint_impl() = delete;
template <typename F,
typename std::enable_if<!std::is_bind_expression<F>::value, int>::type = 0,
typename std::enable_if<is_unary_fn< deduce_signature<F> >::value, int>::type = 0,
typename Arg = typename std::decay<typename std::tuple_element<0, typename deduce_signature<F>::argument_type>::type>::type,
typename Ret = typename std::decay<typename deduce_signature<F>::return_type>::type,
typename std::enable_if<std::is_same<Ret, bool>::value, int>::type = 0>
constraint_impl(std::string const& descr, F f):
__m_docstr( descr ),
__m_index( std::type_index(typeid(typename std::decay<F>::type)) ),
__m_function( new typename std::decay<F>::type(f) )
{}
template <typename T,
typename RealType = typename std::decay<T>::type>
std::string constrain(T const& t) const {
string_repr stringer{};
auto ti = std::type_index(typeid(Constraint<RealType>));
if( ti!=__m_index )
throw std::runtime_error(std::string("Constraint types did not match: expect=")+__m_index.name()+" got="+ti.name());
if( (*reinterpret_cast<Constraint<RealType>*>(__m_function.get()))( t ) )
return std::string();
// Form error message
std::ostringstream oss;
oss << "constraint \"" << __m_docstr << "\" violated by value '" << stringer(t) << "'";
return oss.str();
}
std::string docstr( void ) const {
return __m_docstr;
}
virtual ~constraint_impl() {}
const std::string __m_docstr;
const std::type_index __m_index;
const std::shared_ptr<void> __m_function;
};
// Sometimes one needs case-insensitive string comparison
struct case_insensitive_char_cmp {
bool operator()(char l, char r) const {
return ::toupper(l) < ::toupper(r);
}
};
// Sort names in descending length and then alphabetically
template <typename T>
struct name_sort {
bool operator()(T const& l, T const& r) const {
if( l.size()==r.size() )
return std::lexicographical_compare(std::begin(l), std::end(l),
std::begin(r), std::end(r), case_insensitive_char_cmp());
return l.size() > r.size();
}
};
} // namespace detail
// The collection of command line option names
using namecollection_t = std::set<std::string, detail::name_sort<std::string>>;
using docstringlist_t = std::list<std::string>;
// This function template allows one to get the appropriate "std::endl" for
// a particular stream. The "std::endl" isn't a thing - it's a template.
// As such "std::endl" by itself cannot be passed as function argument.
// But using the ENDL(...) function you can - it'll return a reference to
// the correctly instantiated version of std::endl.
// (Not so well done on that one c++!)
// See: http://stackoverflow.com/a/10016203
template <typename... Traits>
auto ENDL(std::basic_ostream<Traits...> const&) -> decltype( &std::endl<Traits...> ) {
return std::endl<Traits...>;
}
// Note: need to use ENDL(...) to pass endl as argument
// See above
template <int exit_code = EXIT_FAILURE, typename... Ts,
typename std::enable_if<exit_code==EXIT_FAILURE || exit_code==EXIT_SUCCESS, int>::type = 0>
void fatal_error(std::ostream& os, std::string const& e, Ts&&... ts) {
char dummy[] = {(os << e << " ", 'a'), (os << std::forward<Ts>(ts), 'a')..., (os << std::endl, 'a')};
(void)dummy;
std::exit( exit_code );
}
} // namespace argparse {
#endif