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
|
|
|
*/
|
|
|
|
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#ifndef CRUFT_UTIL_CMDLINE_HPP
|
|
|
|
#define CRUFT_UTIL_CMDLINE_HPP
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
#include "introspection.hpp"
|
|
|
|
#include "iterator.hpp"
|
|
|
|
|
|
|
|
#include <exception>
|
2016-03-15 13:55:49 +11:00
|
|
|
#include <functional>
|
2015-06-10 21:28:52 +10:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-03-15 13:55:49 +11:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
2018-07-17 16:02:04 +10:00
|
|
|
#include <map>
|
2018-02-28 11:49:13 +11:00
|
|
|
#include <sstream>
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2017-01-05 15:06:49 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
namespace util::cmdopt {
|
2018-02-28 11:49:13 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
class error : public std::exception { };
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
class invalid_key : public error {
|
|
|
|
public:
|
|
|
|
explicit invalid_key (std::string _key);
|
|
|
|
const char* what (void) const noexcept override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string m_key;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
class invalid_value : public error {
|
|
|
|
public:
|
|
|
|
explicit invalid_value (std::string _value);
|
|
|
|
const char* what (void) const noexcept override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
class invalid_null : public error {
|
|
|
|
public:
|
|
|
|
const char* what (void) const noexcept override;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
class invalid_required : public error {
|
|
|
|
public:
|
|
|
|
const char* what (void) const noexcept override;
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
class unhandled_argument : public error {
|
|
|
|
public:
|
|
|
|
explicit unhandled_argument (int index);
|
|
|
|
const char* what (void) const noexcept override;
|
|
|
|
|
|
|
|
int index (void) const noexcept;
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int m_index;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-06-10 21:28:52 +10:00
|
|
|
namespace option {
|
|
|
|
class base {
|
|
|
|
public:
|
2016-03-15 13:55:49 +11:00
|
|
|
// we deal almost exclusively with vtables, so disable copying
|
|
|
|
// just in case we do something stupid.
|
2017-01-31 20:29:27 +11:00
|
|
|
base () = default;
|
2016-03-15 13:55:49 +11:00
|
|
|
base (const base&) = delete;
|
|
|
|
base& operator= (const base&) = delete;
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
virtual ~base ();
|
|
|
|
|
|
|
|
virtual void execute (void);
|
|
|
|
virtual void execute (const char *restrict);
|
|
|
|
virtual void start (void);
|
|
|
|
virtual void finish (void);
|
|
|
|
|
2016-03-11 13:16:20 +11:00
|
|
|
virtual const std::string& example (void) const = 0;
|
|
|
|
|
2015-06-30 22:04:54 +10:00
|
|
|
bool required (void) const;
|
|
|
|
bool required (bool);
|
|
|
|
|
|
|
|
bool seen (void) const;
|
|
|
|
bool seen (bool);
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
private:
|
2016-03-15 13:55:49 +11:00
|
|
|
bool m_required = false;
|
|
|
|
bool m_seen = false;
|
2015-06-10 21:28:52 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-30 22:06:10 +10:00
|
|
|
class null : public base {
|
|
|
|
public:
|
|
|
|
virtual void execute (void) override;
|
|
|
|
virtual void execute (const char *restrict) override;
|
2016-03-11 13:16:20 +11:00
|
|
|
|
|
|
|
virtual const std::string& example (void) const override;
|
2015-06-30 22:06:10 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-30 22:07:02 +10:00
|
|
|
class present : public base {
|
|
|
|
public:
|
2016-03-15 13:55:49 +11:00
|
|
|
explicit present (bool&);
|
2016-03-17 18:05:28 +11:00
|
|
|
explicit present (bool&&) = delete;
|
2015-06-30 22:07:02 +10:00
|
|
|
|
|
|
|
using base::execute;
|
|
|
|
virtual void execute (void) override;
|
|
|
|
|
2016-03-11 13:16:20 +11:00
|
|
|
virtual const std::string& example (void) const override;
|
|
|
|
|
2015-06-30 22:07:02 +10:00
|
|
|
virtual void finish (void) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool &m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-02-28 11:49:13 +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;
|
|
|
|
std::copy (std::cbegin (enum_traits<T>::names),
|
|
|
|
std::cend (enum_traits<T>::names),
|
|
|
|
infix_iterator<const char*> (os, "|"));
|
|
|
|
return os.str ();
|
|
|
|
} ();
|
|
|
|
return EXAMPLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
template <typename T>
|
|
|
|
class value : public base {
|
|
|
|
public:
|
2018-02-28 11:49:13 +11:00
|
|
|
explicit value (T &_data): m_data (_data) { }
|
2016-03-17 18:05:28 +11:00
|
|
|
explicit value (T&&) = delete;
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2015-06-10 21:55:06 +10:00
|
|
|
using base::execute;
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
void execute (const char *restrict str) override
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
std::istringstream is (str);
|
|
|
|
is.exceptions (
|
|
|
|
std::istringstream::failbit
|
|
|
|
| std::istringstream::badbit
|
|
|
|
);
|
|
|
|
|
|
|
|
is >> m_data;
|
|
|
|
} catch (...) {
|
|
|
|
throw invalid_value (__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
seen (true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& example (void) const override
|
|
|
|
{
|
|
|
|
return detail::value_example<T> ();
|
|
|
|
}
|
2016-03-11 13:16:20 +11:00
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
const T& data (void) const&
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
T& data (void) &
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
T& data (T _data) &
|
|
|
|
{
|
|
|
|
return m_data = _data;
|
|
|
|
}
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
private:
|
|
|
|
T& m_data;
|
|
|
|
};
|
|
|
|
|
2018-02-28 11:49:13 +11:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
template <typename T = unsigned>
|
|
|
|
class count : public value<T> {
|
|
|
|
public:
|
2016-03-15 13:55:49 +11:00
|
|
|
explicit count (T&);
|
2016-03-17 18:05:28 +11:00
|
|
|
explicit count (T&&) = delete;
|
2015-06-10 21:55:06 +10:00
|
|
|
|
|
|
|
using value<T>::execute;
|
2015-06-10 21:28:52 +10:00
|
|
|
void execute (void) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class bytes : public value<size_t> {
|
|
|
|
public:
|
2017-05-23 12:50:51 +10:00
|
|
|
explicit bytes (size_t &_value): value (_value) { }
|
2015-06-10 21:55:06 +10:00
|
|
|
|
|
|
|
using value<size_t>::execute;
|
2015-06-10 21:28:52 +10:00
|
|
|
void execute (const char *restrict) override;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-31 20:29:53 +11:00
|
|
|
//-------------------------------------------------------------------------
|
2015-06-10 21:28:52 +10:00
|
|
|
class parser {
|
|
|
|
public:
|
|
|
|
template <typename T, typename ...Args>
|
|
|
|
T& add (char shortname,
|
|
|
|
std::string longname,
|
|
|
|
std::string description,
|
2018-02-28 11:49:13 +11:00
|
|
|
Args&&... args)
|
|
|
|
{
|
|
|
|
auto handler = std::make_unique<T> (std::forward<Args> (args)...);
|
|
|
|
T& ref = *handler;
|
|
|
|
|
2018-07-17 16:02:04 +10:00
|
|
|
m_short.insert({ shortname, ref });
|
|
|
|
m_long.insert({ std::move (longname), ref });
|
2018-02-28 11:49:13 +11:00
|
|
|
|
2018-07-17 16:02:04 +10:00
|
|
|
m_options.push_back ({ std::move (description), std::move (handler) });
|
2018-02-28 11:49:13 +11:00
|
|
|
|
|
|
|
return ref;
|
|
|
|
}
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2016-03-15 13:55:49 +11:00
|
|
|
template <typename T, typename ...Args>
|
|
|
|
T&
|
2018-02-28 11:49:13 +11:00
|
|
|
append (std::string description, Args&&...args)
|
|
|
|
{
|
|
|
|
auto handler = std::make_unique<T> (std::forward<Args> (args)...);
|
|
|
|
auto &ref = *handler;
|
|
|
|
m_positional.push_back (ref);
|
2018-07-17 16:02:04 +10:00
|
|
|
m_options.push_back ({ std::move (description), std::move (handler) });
|
2018-02-28 11:49:13 +11:00
|
|
|
return ref;
|
|
|
|
}
|
2016-03-15 13:55:49 +11:00
|
|
|
|
2015-06-30 22:07:34 +10:00
|
|
|
int scan (int argc, const char *const *argv);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
private:
|
2015-06-30 22:07:34 +10:00
|
|
|
int parse_long (int pos, int argc, const char *const *argv);
|
|
|
|
int parse_short (int pos, int argc, const char *const *argv);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2015-09-11 19:55:32 +10:00
|
|
|
void print_help [[noreturn]] (int argc, const char *const *argv) const;
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2018-07-17 16:02:04 +10:00
|
|
|
std::map<char,std::reference_wrapper<option::base>> m_short;
|
|
|
|
std::map<std::string,std::reference_wrapper<option::base>> m_long;
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2016-03-15 13:55:49 +11:00
|
|
|
std::vector<std::reference_wrapper<option::base>> m_positional;
|
2015-06-10 21:28:52 +10:00
|
|
|
|
2018-07-17 16:02:04 +10:00
|
|
|
struct entry {
|
|
|
|
std::string description;
|
|
|
|
std::unique_ptr<option::base> handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<entry> m_options;
|
2015-06-10 21:28:52 +10:00
|
|
|
};
|
2017-01-05 15:06:49 +11:00
|
|
|
}
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
#endif
|