-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.hpp
More file actions
124 lines (94 loc) · 3.28 KB
/
Copy pathinterface.hpp
File metadata and controls
124 lines (94 loc) · 3.28 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* File: /lib/cli/interface.hpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#ifndef CPPLIB__lib__cli__interface__hpp
#define CPPLIB__lib__cli__interface__hpp
#include <functional>
#include <variant>
#include <string>
#include <vector>
#include <list>
#include <map>
#include "../../lib/tl/noncopyable.hpp"
#include "../../lib/tl/linkable.hpp"
#include "../../lib/cstring.hpp"
#include "./param.hpp"
namespace lib::cli {
// PREDECLARATIONS
struct Command;
class IDriver;
// DECLARATION lib::cli::detail::callback_short, lib::cli::detail::callback_full
namespace detail {
using callback_short = ::std::function<void()>;
using callback_full = ::std::function<void( const Command & )>;
} // namespace detail
// DECLARATION lib::cli::Interface
class Interface final
: tl::noncopymovable<Interface>
, public tl::linkable<Interface, IDriver>
{
public:
using Callback = ::std::variant
< ::std::monostate, detail::callback_short, detail::callback_full >;
Interface( IDriver * driver );
void out( const cstring & line ) const;
void out_commands() const;
bool autocomplete( ::std::string & commandline ) const;
void configure_split( const cstring & separators, const cstring & group );
const ::std::list<::std::string> & history() const { return history_; }
Interface & operator << ( const cstring & command );
Interface & operator << ( const char * command );
inline Interface & operator << ( const decltype(param) & );
inline Interface & operator << ( const decltype(params_list) & );
Interface & operator << ( const Callback & callback );
bool operator () ( const cstring & commandline );
private:
struct Lexeme final
: tl::noncopymovable<Lexeme>
{
Lexeme * append_named( const cstring & name_ );
Lexeme * append_param();
Lexeme * append_params_list();
void set_callback( const Callback & callback_ );
const Lexeme * next( const cstring & name_, Command * command = nullptr ) const;
bool invoke( Command & command ) const;
const ::std::string & get_name() const { return name; }
::std::vector<::std::string> list_commands() const;
::std::vector<cstring> list_suggested( const cstring & name_ ) const;
private:
static constexpr const char * PARAM_CHILD_KEY = "";
bool has_callback() const;
enum class Type {
ROOT, NAMED, PARAM, PARAMS_LIST
} type = Type::ROOT;
const Lexeme * parent = nullptr;
::std::map< ::std::string, Lexeme > childs;
Callback callback;
::std::string name;
};
void update_history( const ::std::vector<cstring> & lexemes );
Lexeme root;
Lexeme * tmp_lexeme;
::std::string split_separators = "\t ";
::std::string split_group = "";
/// @todo Manage capacity.
::std::list<::std::string> history_;
};
// INLINES lib::cli::Interface
inline Interface & Interface::operator << ( const char * command ) {
return operator << ( cstring( command ) );
}
inline Interface & Interface::operator << ( const decltype(param) & ) {
tmp_lexeme = tmp_lexeme->append_param();
return *this;
}
inline Interface & Interface::operator << ( const decltype(params_list) & ) {
tmp_lexeme = tmp_lexeme->append_params_list();
return *this;
}
} // namespace lib::cli
#endif // CPPLIB__lib__cli__interface__hpp