-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.cpp
More file actions
44 lines (37 loc) · 1.3 KB
/
echo.cpp
File metadata and controls
44 lines (37 loc) · 1.3 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
#include <cstdio>
#include <string_view>
#include <cfbox/args.hpp>
#include <cfbox/escape.hpp>
#include <cfbox/help.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "echo",
.version = CFBOX_VERSION_STRING,
.one_line = "display a line of text",
.usage = "echo [OPTIONS] [STRING]...",
.options = " -n do not output the trailing newline\n"
" -e enable interpretation of backslash escapes",
.extra = "",
};
} // namespace
auto echo_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'n', false},
cfbox::args::OptSpec{'e', false},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
bool no_newline = parsed.has('n');
bool interpret = parsed.has('e');
const auto& pos = parsed.positional();
for (std::size_t i = 0; i < pos.size(); ++i) {
if (i > 0) std::fputc(' ', stdout);
if (interpret) {
std::fputs(cfbox::util::process_escape(pos[i]).c_str(), stdout);
} else {
std::fwrite(pos[i].data(), 1, pos[i].size(), stdout);
}
}
if (!no_newline) std::fputc('\n', stdout);
return 0;
}