From 0cc0aed3358b1a46a91dc89f1574dcd8f96bab13 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Sat, 27 May 2023 19:21:47 -0400 Subject: [PATCH] main: Show help text --- src/main.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index a99a8f4..278303e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -11,11 +12,59 @@ constexpr std::size_t ten_megabytes{(1 << 20) * 10}; +static void ShortHelp(const char *argv0) { + std::fprintf(stderr, "Usage: %s [INFILE] [OUTFILE]\n", argv0); +} + +static void PrintArg(const char *short_arg, const char *long_arg, + const char *text) { + std::fprintf(stderr, "%5s, %-20s %s\n", short_arg, long_arg, text); +} + +static void PrintHelp(const char *argv0) { + ShortHelp(argv0); + std::fprintf(stderr, + "Converts a TZif file INFILE from the RFC8536 format to a " + "Nintendo Switch compatible file OUTFILE.\nWith no arguments, " + "tzdb2nx can read and write from stdin/stdout, " + "respectively.\nGiving no arguments without input will print " + "usage information and exit the program.\n\nArguments:\n"); + PrintArg("-h", "--help", "Print this help text and exit"); +} + int main(int argc, char *argv[]) { int f{STDIN_FILENO}; const char *filename{"(stdin)"}; std::size_t filesize{ten_megabytes}; + const char *optstring = "h"; + int c; + const struct option longopts[] = { + { + "help", + no_argument, + nullptr, + 'h', + }, + { + nullptr, + 0, + nullptr, + 0, + }, + }; + + while ((c = getopt_long(argc, argv, optstring, longopts, nullptr)) != -1) { + switch (c) { + case 'h': + PrintHelp(argv[0]); + return -1; + default: + ShortHelp(argv[0]); + return -1; + } + } + if (argc > 1) { filename = argv[1]; f = open(filename, O_RDONLY); @@ -38,6 +87,7 @@ int main(int argc, char *argv[]) { const int result = poll(&fds, 1, 0); if (result == 0) { std::fprintf(stderr, "%s: No input\n", filename); + ShortHelp(argv[0]); return -1; } }