2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
2016-12-05 14:41:00 -05:00
|
|
|
#include <map>
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
#include "common.hpp"
|
2016-11-25 07:55:15 -05:00
|
|
|
#include "errors.hpp"
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
namespace command_line {
|
2016-10-10 14:22:47 -04:00
|
|
|
DEFINE_ERROR(argument_error);
|
|
|
|
DEFINE_ERROR(value_error);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
class option;
|
|
|
|
using choices = vector<string>;
|
|
|
|
using options = vector<option>;
|
2016-12-05 14:41:00 -05:00
|
|
|
using values = std::map<string, string>;
|
2017-01-12 22:47:25 -05:00
|
|
|
using posargs = vector<string>;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
// class definition : option {{{
|
|
|
|
|
|
|
|
class option {
|
|
|
|
public:
|
|
|
|
string flag;
|
|
|
|
string flag_long;
|
|
|
|
string desc;
|
|
|
|
string token;
|
2016-10-24 19:55:59 -04:00
|
|
|
const choices values;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2017-01-10 21:07:28 -05:00
|
|
|
explicit option(string&& flag, string&& flag_long, string&& desc, string&& token = "", const choices&& c = {})
|
|
|
|
: flag(forward<string>(flag))
|
|
|
|
, flag_long(forward<string>(flag_long))
|
|
|
|
, desc(forward<string>(desc))
|
|
|
|
, token(forward<string>(token))
|
|
|
|
, values(forward<const choices>(c)) {}
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// class definition : parser {{{
|
|
|
|
|
|
|
|
class parser {
|
|
|
|
public:
|
2016-12-09 03:40:46 -05:00
|
|
|
using make_type = unique_ptr<parser>;
|
2016-12-09 06:24:01 -05:00
|
|
|
static make_type make(string&& scriptname, const options&& opts);
|
2016-12-09 03:02:47 -05:00
|
|
|
|
2016-12-09 06:24:01 -05:00
|
|
|
explicit parser(string&& synopsis, const options&& opts);
|
2016-06-14 23:32:35 -04:00
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
void usage() const;
|
|
|
|
|
|
|
|
void process_input(const vector<string>& values);
|
|
|
|
|
|
|
|
bool has(const string& option) const;
|
2017-01-12 22:47:25 -05:00
|
|
|
bool has(size_t index) const;
|
2016-11-02 15:22:45 -04:00
|
|
|
string get(string opt) const;
|
2017-01-12 22:47:25 -05:00
|
|
|
string get(size_t index) const;
|
2016-11-25 07:55:15 -05:00
|
|
|
bool compare(string opt, const string& val) const;
|
2017-01-12 22:47:25 -05:00
|
|
|
bool compare(size_t index, const string& val) const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
protected:
|
2016-11-25 07:55:15 -05:00
|
|
|
auto is_short(const string& option, const string& opt_short) const;
|
|
|
|
auto is_long(const string& option, const string& opt_long) const;
|
|
|
|
auto is(const string& option, string opt_short, string opt_long) const;
|
2016-11-02 15:22:45 -04:00
|
|
|
|
2016-11-25 07:55:15 -05:00
|
|
|
auto parse_value(string input, const string& input_next, choices values) const;
|
|
|
|
void parse(const string& input, const string& input_next = "");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
private:
|
2016-12-14 21:30:41 -05:00
|
|
|
string m_synopsis{};
|
2016-12-09 06:24:01 -05:00
|
|
|
const options m_opts;
|
2016-12-14 21:30:41 -05:00
|
|
|
values m_optvalues{};
|
2017-01-12 22:47:25 -05:00
|
|
|
posargs m_posargs{};
|
2016-12-14 21:30:41 -05:00
|
|
|
bool m_skipnext{false};
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|