dius
0.1.0
Loading...
Searching...
No Matches
main.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "di/cli/prelude.h"
4
#include "di/container/string/prelude.h"
5
#include "di/container/vector/prelude.h"
6
#include "di/format/prelude.h"
7
#include "di/meta/core.h"
8
#include "
dius/print.h
"
9
10
namespace
dius::main::detail
{
11
template
<
typename
T>
12
concept
HasHelp
=
requires
(T
const
& value) { value->help; };
13
}
14
15
#define DIUS_MAIN(Type, NS) \
16
auto main(int argc, char** argv) -> int { \
17
auto args = ::di::Vector<::di::TransparentStringView> {}; \
18
for (int i = 0; i < argc; i++) { \
19
char* arg = argv[i]; \
20
size_t len = 0; \
21
while (arg[len] != '\0') { \
22
len++; \
23
} \
24
args.push_back({ arg, len }); \
25
} \
26
\
27
auto as_span = args.span(); \
28
auto parser = ::di::get_cli_parser<Type>(); \
29
auto result = parser.parse(as_span, ::dius::std_err); \
30
using ParserResult = decltype(result); \
31
constexpr bool has_help = ::dius::main::detail::HasHelp<ParserResult>; \
32
if (!result) { \
33
if (result.error().success()) { \
34
return 0; \
35
} \
36
::dius::eprintln("{}: {}"_sv, ::di::Styled("error"_sv, di::FormatColor::Red), result.error()); \
37
if constexpr (has_help) { \
38
::dius::eprintln("\nRun with '{}' for a list of valid options"_sv, \
39
::di::Styled("--help"_sv, di::FormatColor::Cyan)); \
40
} \
41
return 2; \
42
} \
43
\
44
using Result = decltype(NS::main(*result)); \
45
if constexpr (::di::concepts::Expected<Result>) { \
46
auto main_result = NS::main(*result); \
47
if (!main_result) { \
48
::dius::eprintln("{}: {}"_sv, ::di::Styled("error"_sv, di::FormatColor::Red), \
49
main_result.error().message()); \
50
return 1; \
51
} \
52
} else { \
53
(void) NS::main(*result); \
54
} \
55
return 0; \
56
}
dius::main::detail::HasHelp
Definition
main.h:12
dius::main::detail
Definition
main.h:10
print.h
generic
dius
main.h
Generated by
1.16.0