2016-06-14 23:32:35 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
LEMONBUDDY_NS
|
|
|
|
|
|
|
|
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>;
|
|
|
|
using values = map<string, string>;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
explicit option(
|
2016-10-24 19:55:59 -04:00
|
|
|
string flag, string flag_long, string desc, string token = "", const choices c = {})
|
2016-10-25 14:14:44 -04:00
|
|
|
: flag(flag), flag_long(flag_long), desc(desc), token(token), values(c) {}
|
2016-06-14 23:32:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
// class definition : parser {{{
|
|
|
|
|
|
|
|
class parser {
|
|
|
|
public:
|
2016-10-24 19:55:59 -04:00
|
|
|
explicit parser(const string& synopsis, const options& opts)
|
|
|
|
: m_synopsis(synopsis), m_opts(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;
|
|
|
|
string get(string opt) const;
|
|
|
|
bool compare(string opt, string val) const;
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
protected:
|
2016-11-02 15:22:45 -04:00
|
|
|
auto is_short(string option, string opt_short) const;
|
|
|
|
auto is_long(string option, string opt_long) const;
|
|
|
|
auto is(string option, string opt_short, string opt_long) const;
|
|
|
|
|
|
|
|
auto parse_value(string input, string input_next, choices values) const;
|
|
|
|
void parse(string input, string input_next = "");
|
2016-06-14 23:32:35 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
string m_synopsis;
|
|
|
|
options m_opts;
|
|
|
|
values m_optvalues;
|
|
|
|
bool m_skipnext = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:22:45 -04:00
|
|
|
using cliparser = command_line::parser;
|
|
|
|
using clioption = command_line::option;
|
|
|
|
using clioptions = command_line::options;
|
|
|
|
|
2016-10-24 19:55:59 -04:00
|
|
|
namespace {
|
|
|
|
/**
|
|
|
|
* Configure injection module
|
|
|
|
*/
|
2016-11-02 15:22:45 -04:00
|
|
|
template <class T = cliparser>
|
|
|
|
di::injector<T> configure_cliparser(string scriptname, const clioptions& opts) {
|
2016-10-24 19:55:59 -04:00
|
|
|
// clang-format off
|
|
|
|
return di::make_injector(
|
|
|
|
di::bind<>().to("Usage: " + scriptname + " bar_name [OPTION...]"),
|
|
|
|
di::bind<>().to(opts));
|
|
|
|
// clang-format on
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 23:32:35 -04:00
|
|
|
LEMONBUDDY_NS_END
|