polybar-dwm/include/components/command_line.hpp

75 lines
1.8 KiB
C++
Raw Normal View History

2016-06-15 03:32:35 +00:00
#pragma once
#include <map>
2016-06-15 03:32:35 +00:00
#include "common.hpp"
2016-11-25 12:55:15 +00:00
#include "errors.hpp"
#include "utils/factory.hpp"
2016-06-15 03:32:35 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-06-15 03:32:35 +00:00
namespace command_line {
DEFINE_ERROR(argument_error);
DEFINE_ERROR(value_error);
2016-06-15 03:32:35 +00:00
class option;
using choices = vector<string>;
using options = vector<option>;
using values = std::map<string, string>;
2016-06-15 03:32:35 +00:00
// class definition : option {{{
class option {
public:
string flag;
string flag_long;
string desc;
string token;
const choices values;
2016-06-15 03:32:35 +00:00
2016-11-25 07:42:31 +00:00
explicit option(string flag, string flag_long, string desc, string token = "", const choices c = {})
: flag(flag), flag_long(flag_long), desc(desc), token(token), values(c) {}
2016-06-15 03:32:35 +00:00
};
// }}}
// class definition : parser {{{
class parser {
public:
2016-12-09 08:40:46 +00:00
using make_type = unique_ptr<parser>;
2016-12-09 11:24:01 +00:00
static make_type make(string&& scriptname, const options&& opts);
2016-12-09 08:02:47 +00:00
2016-12-09 11:24:01 +00:00
explicit parser(string&& synopsis, const options&& opts);
2016-06-15 03:32:35 +00:00
2016-11-02 19:22:45 +00:00
void usage() const;
void process_input(const vector<string>& values);
bool has(const string& option) const;
string get(string opt) const;
2016-11-25 12:55:15 +00:00
bool compare(string opt, const string& val) const;
2016-06-15 03:32:35 +00:00
protected:
2016-11-25 12:55:15 +00: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 19:22:45 +00:00
2016-11-25 12:55:15 +00:00
auto parse_value(string input, const string& input_next, choices values) const;
void parse(const string& input, const string& input_next = "");
2016-06-15 03:32:35 +00:00
private:
string m_synopsis;
2016-12-09 11:24:01 +00:00
const options m_opts;
2016-06-15 03:32:35 +00:00
values m_optvalues;
bool m_skipnext = false;
};
// }}}
}
2016-11-02 19:22:45 +00:00
using cliparser = command_line::parser;
using clioption = command_line::option;
using clioptions = command_line::options;
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END