-
Notifications
You must be signed in to change notification settings - Fork 79
/
CmdlineParser.hh
59 lines (52 loc) · 1.49 KB
/
CmdlineParser.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
copyright 2018 Paul Dreik (earlier Paul Sundvall)
Distributed under GPL v 2.0 or later, at your option.
See LICENSE for further details.
*/
#ifndef RDFIND_CMDLINEPARSER_HH_
#define RDFIND_CMDLINEPARSER_HH_
/**
* Command line parser, designed to be easy to use for rdfind.
* It will signal user errors by a helpful printout followed by
* std::exit(EXIT_FAILURE)
*/
class Parser
{
public:
Parser(int argc, const char* argv[])
: m_argc(argc)
, m_argv(argv)
, m_index(1)
{}
/**
* tries to parse arg.
* @param arg
* @return true if argument could be parsed. get it with get_parsed_bool().
*/
bool try_parse_bool(const char* arg);
/**
* tries to parse arg
* @param arg
* @return true if argument could be parsed. get it with get_parsed_string().
*/
bool try_parse_string(const char* arg);
bool get_parsed_bool() const { return m_last_bool_result; }
const char* get_parsed_string() const { return m_last_str_result; }
[[gnu::pure]] bool parsed_string_is(const char* value) const;
/**
* advances to the next argument
* @return
*/
int advance() { return ++m_index; }
bool has_args_left() const { return m_index < m_argc; }
int get_current_index() const { return m_index; }
const char* get_current_arg() const;
bool current_arg_is(const char* what) const;
private:
const int m_argc{};
const char** m_argv{};
int m_index = 1;
bool m_last_bool_result{};
const char* m_last_str_result{};
};
#endif /* RDFIND_CMDLINEPARSER_HH_ */