-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp.hpp
More file actions
93 lines (79 loc) · 2.55 KB
/
help.hpp
File metadata and controls
93 lines (79 loc) · 2.55 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
#pragma once
#include <cstdio>
#include <string_view>
#include <cfbox/applet_config.hpp>
#include <cfbox/term.hpp>
namespace cfbox::help {
struct HelpEntry {
std::string_view name;
std::string_view version;
std::string_view one_line;
std::string_view usage;
std::string_view options;
std::string_view extra;
};
namespace detail {
inline void write_sv(std::string_view sv) {
if (!sv.empty()) std::fwrite(sv.data(), 1, sv.size(), stdout);
}
} // namespace detail
inline auto print_help(const HelpEntry& entry) -> void {
// Title: "cfbox <name> -- <one_line>"
detail::write_sv(term::bold());
std::fwrite("cfbox ", 1, 6, stdout);
detail::write_sv(entry.name);
detail::write_sv(term::reset());
std::fwrite(" -- ", 1, 4, stdout);
detail::write_sv(entry.one_line);
std::fputc('\n', stdout);
std::fputc('\n', stdout);
// Usage
detail::write_sv(term::bold());
std::fwrite("Usage:", 1, 6, stdout);
detail::write_sv(term::reset());
std::fputc('\n', stdout);
std::fputc(' ', stdout);
std::fputc(' ', stdout);
detail::write_sv(entry.usage);
std::fputc('\n', stdout);
std::fputc('\n', stdout);
// Options
if (!entry.options.empty()) {
detail::write_sv(term::bold());
std::fwrite("Options:", 1, 8, stdout);
detail::write_sv(term::reset());
std::fputc('\n', stdout);
detail::write_sv(entry.options);
std::fputc('\n', stdout);
}
// Auto-append --help and --version
std::fwrite(" ", 1, 6, stdout);
detail::write_sv(term::cyan());
std::fwrite("--help", 1, 6, stdout);
detail::write_sv(term::reset());
std::fwrite(" display this help and exit\n", 1, 32, stdout);
std::fwrite(" ", 1, 6, stdout);
detail::write_sv(term::cyan());
std::fwrite("--version", 1, 9, stdout);
detail::write_sv(term::reset());
std::fwrite(" output version information and exit\n", 1, 38, stdout);
// Extra notes
if (!entry.extra.empty()) {
std::fputc('\n', stdout);
detail::write_sv(entry.extra);
std::fputc('\n', stdout);
}
}
inline auto print_version(const HelpEntry& entry) -> void {
std::fwrite("cfbox ", 1, 6, stdout);
detail::write_sv(entry.name);
std::fputc(' ', stdout);
detail::write_sv(entry.version);
std::fputc('\n', stdout);
}
inline auto print_cfbox_version() -> void {
std::fwrite("cfbox ", 1, 6, stdout);
std::fwrite(CFBOX_VERSION_STRING, 1, sizeof(CFBOX_VERSION_STRING) - 1, stdout);
std::fputc('\n', stdout);
}
} // namespace cfbox::help