Luna/apps/cat.cpp
apio 65d3195caa
All checks were successful
continuous-integration/drone/push Build is passing
libos: Allow ArgumentParser users to specify they want leftover arguments
2023-05-13 13:22:10 +02:00

42 lines
906 B
C++

#include <luna/String.h>
#include <os/ArgumentParser.h>
#include <os/File.h>
using os::File;
static Result<void> do_cat(StringView path)
{
SharedPtr<File> f = TRY(File::open_input_file(path));
auto out = File::standard_output();
auto buf = TRY(Buffer::create_sized(4096));
while (1)
{
TRY(f->read(buf, 4096));
if (buf.is_empty()) break;
out->write(buf);
}
return {};
}
Result<int> luna_main(int argc, char** argv)
{
StringView filename;
Vector<StringView> files;
os::ArgumentParser parser;
parser.add_description("Concatenate files to standard output."_sv);
parser.add_system_program_info("cat"_sv);
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
parser.set_vector_argument(files);
parser.parse(argc, argv);
TRY(do_cat(filename));
for (auto file : files) TRY(do_cat(file));
return 0;
}