2015-06-10 21:28:52 +10:00
|
|
|
/*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
2016-03-15 13:55:49 +11:00
|
|
|
* Copyright 2013-2016 Danny Robson <danny@nerdcruft.net>
|
2015-06-10 21:28:52 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __UTIL_CMDLINE_IPP
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
#define __UTIL_CMDLINE_IPP
|
|
|
|
|
2015-06-10 21:54:42 +10:00
|
|
|
#include <sstream>
|
2016-04-27 15:52:52 +10:00
|
|
|
#include <algorithm>
|
2015-06-10 21:54:42 +10:00
|
|
|
|
2016-03-11 13:16:20 +11:00
|
|
|
#include "./introspection.hpp"
|
|
|
|
#include "./iterator.hpp"
|
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
namespace util::cmdopt {
|
2015-06-10 21:54:42 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2016-03-15 13:55:49 +11:00
|
|
|
option::value<T>::value (T &_data):
|
2015-06-10 21:54:42 +10:00
|
|
|
m_data (_data)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2016-04-27 15:52:52 +10:00
|
|
|
inline void
|
2015-06-10 21:54:42 +10:00
|
|
|
option::value<T>::execute (const char *restrict str)
|
|
|
|
{
|
|
|
|
try {
|
2016-04-02 13:36:32 +11:00
|
|
|
std::istringstream is (str);
|
|
|
|
is.exceptions (
|
2015-06-10 21:54:42 +10:00
|
|
|
std::istringstream::failbit
|
|
|
|
| std::istringstream::badbit
|
|
|
|
);
|
|
|
|
|
2016-04-02 13:36:32 +11:00
|
|
|
is >> m_data;
|
2015-06-10 21:54:42 +10:00
|
|
|
} catch (...) {
|
|
|
|
throw invalid_value (__func__);
|
|
|
|
}
|
2015-06-30 22:04:54 +10:00
|
|
|
|
|
|
|
seen (true);
|
2015-06-10 21:54:42 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-27 15:52:52 +10:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
namespace option {
|
|
|
|
template <>
|
|
|
|
inline void
|
|
|
|
value<bool>::execute (const char *restrict str)
|
|
|
|
{
|
|
|
|
static const std::string TRUE_STRING[] = {
|
|
|
|
"true",
|
|
|
|
"yes",
|
|
|
|
"y",
|
|
|
|
"1"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (std::any_of (std::begin (TRUE_STRING),
|
|
|
|
std::end (TRUE_STRING),
|
|
|
|
[str] (auto i) { return i == str; }))
|
|
|
|
{
|
|
|
|
m_data = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const std::string FALSE_STRING[] = {
|
|
|
|
"false",
|
|
|
|
"no",
|
|
|
|
"n",
|
|
|
|
"0"
|
|
|
|
};
|
|
|
|
|
|
|
|
if (std::any_of (std::begin (FALSE_STRING),
|
|
|
|
std::end (FALSE_STRING),
|
|
|
|
[str] (auto i) { return i == str; }))
|
|
|
|
{
|
|
|
|
m_data = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
base::execute (str);
|
|
|
|
seen (true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-11 13:16:20 +11:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
namespace detail {
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<!std::is_enum<T>::value, const std::string&>
|
|
|
|
value_example (void)
|
|
|
|
{
|
|
|
|
static const std::string EXAMPLE =
|
|
|
|
std::string {"<"} +
|
|
|
|
std::string {to_string<T> ()} +
|
|
|
|
std::string {">"};
|
|
|
|
|
|
|
|
return EXAMPLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_enum<T>::value, const std::string&>
|
|
|
|
value_example (void)
|
|
|
|
{
|
|
|
|
static const std::string EXAMPLE = [] (void) {
|
|
|
|
std::ostringstream os;
|
2016-03-11 19:16:55 +11:00
|
|
|
std::copy (std::cbegin (enum_traits<T>::names),
|
|
|
|
std::cend (enum_traits<T>::names),
|
|
|
|
infix_iterator<const char*> (os, "|"));
|
2016-03-11 13:16:20 +11:00
|
|
|
return os.str ();
|
|
|
|
} ();
|
|
|
|
return EXAMPLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
const std::string&
|
|
|
|
option::value<T>::example (void) const
|
|
|
|
{
|
|
|
|
return detail::value_example<T> ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:54:42 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2016-03-17 18:05:28 +11:00
|
|
|
const T&
|
|
|
|
option::value<T>::data (void) const&
|
2015-06-10 21:54:42 +10:00
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
T&
|
2016-03-17 18:05:28 +11:00
|
|
|
option::value<T>::data (void) &
|
2015-06-10 21:54:42 +10:00
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 22:08:07 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
T&
|
2016-03-17 18:05:28 +11:00
|
|
|
option::value<T>::data (T _data) &
|
2015-06-30 22:08:07 +10:00
|
|
|
{
|
|
|
|
return m_data = _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:54:42 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-06-10 21:28:52 +10:00
|
|
|
template <typename T, typename ...Args>
|
|
|
|
T&
|
2015-06-11 19:30:19 +10:00
|
|
|
parser::add (char shortname,
|
|
|
|
std::string longname,
|
|
|
|
std::string description,
|
|
|
|
Args&&... args)
|
2015-06-10 21:28:52 +10:00
|
|
|
{
|
2016-03-15 13:55:49 +11:00
|
|
|
auto handler = std::make_unique<T> (std::forward<Args> (args)...);
|
2015-06-10 21:28:52 +10:00
|
|
|
T& ref = *handler;
|
|
|
|
|
|
|
|
m_short.emplace_back (shortname, ref);
|
2016-03-15 13:55:49 +11:00
|
|
|
m_long .emplace_back (std::move (longname), ref);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2016-03-15 13:55:49 +11:00
|
|
|
m_options.emplace_back (std::move (description), std::move (handler));
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
2016-03-15 13:55:49 +11:00
|
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
T&
|
|
|
|
parser::append (std::string description,
|
|
|
|
Args &&...args)
|
|
|
|
{
|
|
|
|
auto handler = std::make_unique<T> (std::forward<Args> (args)...);
|
|
|
|
auto &ref = *handler;
|
|
|
|
m_positional.push_back (ref);
|
|
|
|
m_options.emplace_back (std::move (description), std::move (handler));
|
|
|
|
return ref;
|
|
|
|
}
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|