ReXGlue SDK
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
23namespace rex {
24
25struct CLIOption {
27 std::string name;
29 std::optional<std::string> value;
30};
31
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
88struct CLIAction {
89 std::string_view name;
90 std::string_view description;
91};
92
94 CLIAction action;
95 CLIOption option;
96};
97
98namespace detail {
99
105constexpr bool IsActionChar(char c) {
106 return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_' || c == '-';
107}
108
113constexpr 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
123consteval 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)
Definition cli.h:36
std::optional< std::string_view > Get(std::string_view name) const
static CLIArgList Parse(std::span< const std::string > args)
static CLIArgList Parse(i32 argc, char **argv)
std::span< const std::string > GetPositionals() const
bool Has(std::string_view name) const
std::span< const CLIOption > GetOptions() const
std::span< const std::string > GetAll() const
Definition app.h:36
Platform flags, packing macros and export markers.
#define REX_API
Definition platform.h:177
Definition cli.h:93
Definition cli.h:88
std::string_view name
Definition cli.h:89
Definition cli.h:25
std::string name
Definition cli.h:27
std::optional< std::string > value
Definition cli.h:29
Integer types, byte order wrappers and size literals.