1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:47:45 +00:00

Man: Use ArgsParser to parse arguments

This commit is contained in:
howar6hill 2020-02-18 16:54:29 +08:00 committed by Andreas Kling
parent 7ce3f218af
commit 94ed183774
2 changed files with 17 additions and 22 deletions

View file

@ -12,12 +12,12 @@ $ man section page
## Description ## Description
`man` finds, loads and displays the so-called manual pages, `man` finds, loads and displays the so-called manual pages,
or man pages for short, from the Serenity manual. You're reading or man pages for short, from the SerenityOS manual. You're reading
the manual page for `man` program itself right now. the manual page for `man` program itself right now.
## Sections ## Sections
The Serenity manual is split into the following *sections*, or *chapters*: The SerenityOS manual is split into the following *sections*, or *chapters*:
1. Command-line programs 1. Command-line programs
2. System calls 2. System calls

View file

@ -26,6 +26,7 @@
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibMarkdown/MDDocument.h> #include <LibMarkdown/MDDocument.h>
#include <stdio.h> #include <stdio.h>
@ -45,26 +46,20 @@ int main(int argc, char* argv[])
unveil(nullptr, nullptr); unveil(nullptr, nullptr);
String name; const char* section = nullptr;
String section; const char* name = nullptr;
if (argc < 2 || argc > 3) { Core::ArgsParser args_parser;
fprintf(stderr, "Usage:\t%s <name>\n\t%s <section <name>\n", argv[0], argv[0]); args_parser.add_positional_argument(section, "Section of the man page", "section", Core::ArgsParser::Required::No);
exit(1); args_parser.add_positional_argument(name, "Name of the man page", "name");
}
if (argc == 2) { args_parser.parse(argc, argv);
name = argv[1];
} else {
section = argv[1];
name = argv[2];
}
auto make_path = [&](String s) { auto make_path = [name](const char* section) {
return String::format("/usr/share/man/man%s/%s.md", s.characters(), name.characters()); return String::format("/usr/share/man/man%s/%s.md", section, name);
}; };
if (section.is_null()) { if (!section) {
String sections[] = { const char* sections[] = {
"1", "1",
"2", "2",
"3", "3",
@ -74,15 +69,15 @@ int main(int argc, char* argv[])
"7", "7",
"8" "8"
}; };
for (auto& s : sections) { for (auto s : sections) {
String path = make_path(s); String path = make_path(s);
if (access(path.characters(), R_OK) == 0) { if (access(path.characters(), R_OK) == 0) {
section = s; section = s;
break; break;
} }
} }
if (section.is_null()) { if (!section) {
fprintf(stderr, "No man page for %s\n", name.characters()); fprintf(stderr, "No man page for %s\n", name);
exit(1); exit(1);
} }
} }
@ -104,7 +99,7 @@ int main(int argc, char* argv[])
auto buffer = file->read_all(); auto buffer = file->read_all();
String source { (const char*)buffer.data(), (size_t)buffer.size() }; String source { (const char*)buffer.data(), (size_t)buffer.size() };
printf("%s(%s)\t\tSerenity manual\n", name.characters(), section.characters()); printf("%s(%s)\t\tSerenityOS manual\n", name, section);
MDDocument document; MDDocument document;
bool success = document.parse(source); bool success = document.parse(source);