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.
|
|
|
|
*
|
|
|
|
* Copyright 2013 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef __UTIL_CMDLINE_HPP
|
|
|
|
#define __UTIL_CMDLINE_HPP
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace util { namespace cmdopt {
|
|
|
|
namespace option {
|
|
|
|
class base {
|
|
|
|
public:
|
|
|
|
base (std::string name);
|
|
|
|
virtual ~base ();
|
|
|
|
|
|
|
|
virtual void execute (void);
|
|
|
|
virtual void execute (const char *restrict);
|
|
|
|
virtual void start (void);
|
|
|
|
virtual void finish (void);
|
|
|
|
|
|
|
|
std::string name (void) const;
|
|
|
|
|
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:
|
|
|
|
std::string m_name;
|
2015-06-30 22:04:54 +10:00
|
|
|
bool m_required;
|
|
|
|
bool m_seen;
|
2015-06-10 21:28:52 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-30 22:06:10 +10:00
|
|
|
class null : public base {
|
|
|
|
public:
|
|
|
|
null (std::string name);
|
|
|
|
|
|
|
|
virtual void execute (void) override;
|
|
|
|
virtual void execute (const char *restrict) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-30 22:07:02 +10:00
|
|
|
class present : public base {
|
|
|
|
public:
|
|
|
|
present (std::string name, bool&);
|
|
|
|
|
|
|
|
using base::execute;
|
|
|
|
virtual void execute (void) override;
|
|
|
|
|
|
|
|
virtual void finish (void) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool &m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-06-10 21:28:52 +10:00
|
|
|
template <typename T>
|
|
|
|
class value : public base {
|
|
|
|
public:
|
|
|
|
value (std::string name, T&);
|
|
|
|
|
2015-06-10 21:55:06 +10:00
|
|
|
using base::execute;
|
2015-06-10 21:28:52 +10:00
|
|
|
void execute (const char *restrict) override;
|
|
|
|
|
|
|
|
T data (void) const;
|
|
|
|
T& data (void);
|
2015-06-30 22:08:07 +10:00
|
|
|
T& data (T);
|
2015-06-10 21:28:52 +10:00
|
|
|
|
|
|
|
private:
|
|
|
|
T& m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T = unsigned>
|
|
|
|
class count : public value<T> {
|
|
|
|
public:
|
2015-06-15 17:47:18 +10:00
|
|
|
count (std::string name, T&);
|
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:
|
2015-06-30 22:04:54 +10:00
|
|
|
using value<size_t>::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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class error : public std::runtime_error
|
|
|
|
{ using runtime_error::runtime_error; };
|
|
|
|
|
|
|
|
class invalid_key : public error
|
|
|
|
{ using error::error; };
|
|
|
|
|
|
|
|
class invalid_value : public error
|
|
|
|
{ using error::error; };
|
|
|
|
|
|
|
|
class invalid_null : public error
|
|
|
|
{ using error::error; };
|
|
|
|
|
2015-06-30 22:04:54 +10:00
|
|
|
class invalid_required : public error
|
|
|
|
{ using error::error; };
|
|
|
|
|
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,
|
|
|
|
Args&&...);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
using short_t = std::tuple<char,option::base&>;
|
|
|
|
using long_t = std::tuple<std::string,option::base&>;
|
|
|
|
|
|
|
|
std::vector<short_t> m_short;
|
|
|
|
std::vector<long_t> m_long;
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<option::base>> m_options;
|
|
|
|
};
|
|
|
|
} }
|
|
|
|
|
|
|
|
#include "cmdopt.ipp"
|
|
|
|
|
|
|
|
#endif
|