ReXGlue SDK
Toggle main menu visibility
Loading...
Searching...
No Matches
cli.h
Go to the documentation of this file.
1
10
11
#pragma once
12
13
#include <algorithm>
14
#include <optional>
15
#include <span>
16
#include <string>
17
#include <string_view>
18
#include <vector>
19
20
#include <
rex/platform.h
>
21
#include <
rex/types.h
>
22
23
namespace
rex
{
24
25
struct
CLIOption
{
27
std::string
name
;
29
std::optional<std::string>
value
;
30
};
31
36
class
REX_API
CLIArgList
{
37
public
:
44
static
CLIArgList
Parse
(i32 argc,
char
** argv);
45
51
static
CLIArgList
Parse
(std::span<const std::string> args);
52
59
bool
Has
(std::string_view name)
const
;
60
68
std::optional<std::string_view>
Get
(std::string_view name)
const
;
69
71
std::span<const CLIOption>
GetOptions
()
const
;
72
74
std::span<const std::string>
GetPositionals
()
const
;
75
79
std::span<const std::string>
GetAll
()
const
;
80
81
private
:
82
std::vector<std::string> all_;
83
std::vector<CLIOption> options_;
84
std::vector<std::string> positionals_;
85
};
86
88
struct
CLIAction
{
89
std::string_view
name
;
90
std::string_view description;
91
};
92
93
struct
CLIActionHit
{
94
CLIAction
action;
95
CLIOption
option;
96
};
97
98
namespace
detail {
99
105
constexpr
bool
IsActionChar(
char
c) {
106
return
(c >=
'a'
&& c <=
'z'
) || (c >=
'0'
&& c <=
'9'
) || c ==
'_'
|| c ==
'-'
;
107
}
108
113
constexpr
bool
IsActionName(std::string_view name) {
114
return
!name.empty() && name.front() >=
'a'
&& name.front() <=
'z'
&&
115
std::ranges::all_of(name, IsActionChar);
116
}
117
123
consteval
CLIAction MakeAction(std::string_view name, std::string_view description) {
124
if
(!IsActionName(name)) {
125
throw
"an action name is lowercase letters, digits, underscores and hyphens"
;
126
}
127
return
CLIAction{name, description};
128
}
129
130
}
// namespace detail
131
132
}
// namespace rex
133
144
#define REX_CLI_ACTION(name, description) ::rex::detail::MakeAction(#name, description)
rex::CLIArgList
Definition
cli.h:36
rex::CLIArgList::Get
std::optional< std::string_view > Get(std::string_view name) const
rex::CLIArgList::Parse
static CLIArgList Parse(std::span< const std::string > args)
rex::CLIArgList::Parse
static CLIArgList Parse(i32 argc, char **argv)
rex::CLIArgList::GetPositionals
std::span< const std::string > GetPositionals() const
rex::CLIArgList::Has
bool Has(std::string_view name) const
rex::CLIArgList::GetOptions
std::span< const CLIOption > GetOptions() const
rex::CLIArgList::GetAll
std::span< const std::string > GetAll() const
rex
Definition
app.h:36
platform.h
Platform flags, packing macros and export markers.
REX_API
#define REX_API
Definition
platform.h:177
rex::CLIActionHit
Definition
cli.h:93
rex::CLIAction
Definition
cli.h:88
rex::CLIAction::name
std::string_view name
Definition
cli.h:89
rex::CLIOption
Definition
cli.h:25
rex::CLIOption::name
std::string name
Definition
cli.h:27
rex::CLIOption::value
std::optional< std::string > value
Definition
cli.h:29
types.h
Integer types, byte order wrappers and size literals.
rex
cli.h
Generated by
1.18.0